Quantcast
Channel: Redino blog
Viewing all 80 articles
Browse latest View live

Qt QWidget add menu bar

$
0
0

I want to add menu bar to my window (which is a QWidget instance), I searched on the Google and found that most articles are recommending to use QMainWindow instead of QWidget. Using QMainWindow is a good choice, but in my program the main window is based on QWidget and the code base is huge, so adding menu bar to QWidget is a better choice here.

First we need create a window (QWidget) and set its layout (we use QVBoxLayout here)

window = QWidget()
vbox = QVBoxLayout()
window.setLayout(vbox)

Then create a menu bar and add it to the VBox layout.

menu_bar = QMenuBar()
vbox.addWidget(menu_bar)

Now you will see a blank menu bar in your window

Next we need add some menus to our menu bar

file_menu = menu_bar.addMenu('File')
edit_menu = menu_bar.addMenu('Edit')

addMenu function will take the parameter as menu name and return the menu as return value. We need keep the menu instances because we need add actions (menu items) to them.

exit_action = QAction('Exit', window)
exit_action.triggered.connect(exit)
file_menu.addAction(exit_action)

In above code we create a QAction (menu item) with title “Exit”, and connect its “triggered” signal to exit function (sys.exit), it means when this menu item is clicked, the exit function will be called. Finally add this menu item to the “File” menu.

 

We can also connect QAction’s triggered signal to custom defined function

def redoClicked():

  msg_box = QMessageBox()

  msg_box.setText('Redo will be performed')

  msg_box.exec_()

redo_action = QAction('Redo', window)

redo_action.triggered.connect(redoClicked)

edit_menu.addAction(redo_action)

Here we add a “Redo” menu item under “Edit” menu. When user clicks “Redo”, a message box will be popped up saying “Redo will be performed”

 

Following is the complete code:

from PySide.QtCore import *
from PySide.QtGui import *
import sys

def redoClicked():
  msg_box = QMessageBox()
  msg_box.setText('Redo will be performed')
  msg_box.exec_()

app = QApplication(sys.argv)

window = QWidget()
vbox = QVBoxLayout()

menu_bar = QMenuBar()
file_menu = menu_bar.addMenu('File')
edit_menu = menu_bar.addMenu('Edit')
exit_action = QAction('Exit', window)
exit_action.triggered.connect(exit)
file_menu.addAction(exit_action)
redo_action = QAction('Redo', window)
redo_action.triggered.connect(redoClicked)
edit_menu.addAction(redo_action)

window.setLayout(vbox)
vbox.addWidget(menu_bar)
window.show()
app.exec_()

 

The post Qt QWidget add menu bar appeared first on Redino blog.


Drupal search without reindex

$
0
0

In Drupal, if you want to let your published node appear in search results, you need to re-index the search first (Administration->Configuration->Search Settings). And the Drupal cron job file (cron.php) will be executed to re-index the site at a specific time on every minute, every hour or every day, etc.

But recently I’m building a project outsourcing site, I need the newly posted project can be searched by other users immediately after it’s published. I searched on Google and Drupal for a while, finally I found this module: Auto-Index.

This module will automatically re-index the nodes every time a new node is created or an existing node is edited. And there is no more configuration needed after installing.

The post Drupal search without reindex appeared first on Redino blog.

Unable to find vcvarsall.bat when installing gmpy using pip

$
0
0

GMPY is a multiple-precision arithmetic module, which is a extension module written by C. I tried to install it by using pip, then I got following error message:

Unable to find vcvarsall.bat

It’s because it’s trying to find a c compiler (Visual C++ or MinGW) to compile this extension but failed. To fix this error, one method is to install a C compiler (Visual C++ or MinGW). And another easier way is to download an windows installer: https://code.google.com/p/gmpy/downloads/list

The post Unable to find vcvarsall.bat when installing gmpy using pip appeared first on Redino blog.

Drupal user properties (status, hostname, etc)

$
0
0

In Drupal, user_load($uid) function will return an user object. And Drupal has defined a global user object as well (it represents current logged in user).

The user object has following properties:

status -> active/blocked
uid -> user id
mail -> email address
created -> created timestamp
login -> login timestamp
hostname -> ip address
init -> the email address provided at initial registration
roles -> roles assigned to this user

The difference between “mail” property and “init” property is that mail can be changed after registration, but init is the first used email address and cannot be changed. If you want to send email to an user, you need to know his email address ($user->mail).

The post Drupal user properties (status, hostname, etc) appeared first on Redino blog.

No module named Crypto.PublicKey

$
0
0

When I tried to import the “paramiko” module, I got following error:

No module named Crypto.PublicKey

That means we need to install pycrypto library. Run following command to install pycrypto library by using pip:

pip install pycrypto

In the installation I got a different error message:

error: Unable to find vcvarsall.bat

That means we need a C compiler to compile this extension. But we can also download a pre-built extension for windows: http://www.voidspace.org.uk/python/modules.shtml#pycrypto

 

After installing this extension and tried to import “paramiko” module, I got the error “No module named Crypto.PublicKey” again. We should rename “crypto” directory under “Lib/site-packages” to “Crypto”, then importing paramiko will work.

The post No module named Crypto.PublicKey appeared first on Redino blog.

Access PHP built-in web server from other devices

$
0
0

We often use following command to start the PHP built-in web server.

php -S localhost:port

But in this way only the machine which starts the server can access it, any other computers cannot access it.
To solve this problem, we can use 0.0.0.0 as the binding IP address

php -S 0.0.0.0:8000

Then other devices are able to access our server as well. Using 0.0.0.0 as the IP addres will make the server bind all available IP addresses on the machine.

The post Access PHP built-in web server from other devices appeared first on Redino blog.

Set sorting layer in unity c#

$
0
0

Sorting layer is a property of SpriteRenderer, it’s used to specify which object is at top, and which object is at bottom.

If you want to change the sorting layer in script, you can use following code:

SpriteRenderer sprite = GetComponent<SpriteRenderer>();

sprite_renderer.sortingOrder = ..;

sprite_renderer.sortingLayerName = ..;

 

 

The post Set sorting layer in unity c# appeared first on Redino blog.

The URL is not accessible when installing Magento

$
0
0

When installing Magento, we may get such error “The URL is not accessible”.

In some earlier versions of Magento, we can fix this problem by change code of app/code/core/Mage/Install/Model/Installer/Config.php
Just change

$client = new Varien_Http_Client($url . $prefix);

to

$client = new Varien_Http_Client($url . 'index.php/' . $prefix);

Will solve the problem. It’s because that the URL rewriting doesn’t work for some reasons. (Magento uses .htaccess files to add URL rewriting rules)

 

But in Magento 1.9, checking the “Skip the base url validation” option will solve this problem

The post The URL is not accessible when installing Magento appeared first on Redino blog.


Fabric.js set text color

$
0
0

Fabric.js is a powerful javascript canvas library which wraps html5 canvas operation, and introduces objects (text, shape, path, etc.) to developer.

There is a common method set() for object, it can be used to set property of an object. But if you want to change text color, text.set(‘color’, ‘#0f0′) will not work.

To set color of text, we need modify the fill attribute or use setColor() method (only available for text object)

text.set({fill: '#000'})

Or

text.setColor('...')

 

 

The post Fabric.js set text color appeared first on Redino blog.

HTML set checkbox as readonly

$
0
0

Sometimes we don’t want to make some form fields non-editable to users, like Product ID, Order Created Date, Post Author, etc.

For HTML text input and textarea controls, we can add “readonly” attribute to disallow users to modify their value, but this attribute will not work for checkbox control. So how should we disable the checkbox (or set it as readonly)? The “disabled” attribute will do the job

<input type="checkbox" checked disabled>
<input type="radio" disabled>

(It works for both radio box and checkbox)

 

You will see following checkbox and radio button is disabled:


The post HTML set checkbox as readonly appeared first on Redino blog.

WebServlet cannot be resolved to a type

$
0
0

WebServlet annotation is introduced in Servlet 3.0, it’s used to simplify the servlet creation (we don’t need to define it and its URL mapping in web.xml)

The WebServlet annotation usage is

@WebServlet(URL)

e.g.

@WebServlet("/servlet/GetMailFileOwner")
public class GetMailFileOwner extends HttpServlet {
...

Then the user can visit /servlet/GetMailFileOwner to access this servlet

 

But today I met a problem with using WebServlet annotation. First I added WebServlet on the Linux machine (Eclipse Luna), and switched to another Windows machine (MyEclipse), then I tried to rebuild the project, I got following error:

WebServlet cannot be resolved to a type

On the Windows machine, I use the default servlet.jar which is added when I create the new Dynamic Web Project. But I replaced the default servlet.jar file with Tomcat 8.0 libraries.

So the solution is to add Tomcat 8.0 libraries to Java Build Path (We need first download Tomcat 8.0 and set it up for Eclipse, otherwise Eclipse will not find this Tomcat 8.0 libraries)

The post WebServlet cannot be resolved to a type appeared first on Redino blog.

QWebEngine crash with exception code 0×80000003

$
0
0

QWebEngine is a new web browser engine introduced in Qt 5.4, it’s used to replace the old QWebView. QWebView is based on WebKit, while QWebEngine is based on Chromium browser, so it’s faster and supports more features.

I wrote a very simple program to test QWebEngine, following is the code:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString url("http://google.com");
WebEngineView *view = new WebEngineView();
view->load(url);
this->setCentralWidget(view);
}

Compiling and running is smooth. But when I tried to run this demo on another machine, it crashed, and an error dialog is shown with the message “An unknown software exception (0×80000003) has occurred at 0x023c5b7e. Click OK to terminate the program.

By chekcing the log file (debug.log) I found some helpful information:

[ERROR:icu_util.cc(154)] Couldn’t mmap D:\Qt\Qt5.4.0\5.4\msvc2013_64\icudtl.dat
[FATAL:content_main_runner.cc(719)] Check failed: base::i18n::InitializeICU()

We can see the icudtl.dat path is not correct, because it doesn’t exist at all.

To fix it we need to create a new file named “qt.conf” under the application directory, and fill it with following content:

[Paths]
Data=.
Prefix=.

 

(This bug is posted at https://bugreports.qt.io/browse/QTBUG-42083)

The post QWebEngine crash with exception code 0×80000003 appeared first on Redino blog.

Check MySQL case-insensitive query or table name

$
0
0

I tried to install Umbrao CMS, it supports MySQL database but it need MySQL to support case-insensitive query and table name.

 

Following is way to check whether case-insensitive query is supported:

show variables where variable_name=’lower_case_table_names’;

or check lower_case_table_names option in my.ini (MySQL configuration file)

 

If this option’s value … Read the rest

The post Check MySQL case-insensitive query or table name appeared first on Redino blog.

mysql auto backup

$
0
0

First create a shell script which is used to backup database, here we named the script as mysql_backup.sh:

current_date=`date +"%y-%m-%d"`
mysqldump -u.. -p.. db_name > db_name$current_date.sql

In above command, we use date command to generate current date, and use it as part of filename.

After that we need to make this file executable

chmod +x mysql_backup.sh

 

To make this backup script run automatically, we should create a cronjob. Type following command to open cronjob editor

crontab -e

Write following text and save it

0 0 * * * /home/usernane/mysql_backup.sh

Above command means the cronjob will run at 0:00 everyday

 

 

The post mysql auto backup appeared first on Redino blog.

WPF ModernUI Tutorial Part 1

$
0
0

Introduction

ModernUI is a nice WPF theme and a styling framework which let you build a modern, beautiful application easily. Are you poor in designing skill and have problem with designing GUI for your WPF applicaiotn? If so, you have to try ModernUI, it is the best choice for you. In web design, there is Bootstrap for building great UI. And for WPF, it is Modern UI.

 

Screenshots

Mordern UI Screenshots

Mordern UI Screenshots

WPF ModernUI Screenshots

WPF ModernUI Screenshots

 

Installation

The installation is really easy, just search “ModernUI” in Manage NuGet Package Window, and select “ModernUI for WPF” item and click “Install”

Install ModernUI for WPF in NuGet Package Manager

Install ModernUI for WPF in NuGet Package Manager

Or run following command in Package Manager Console

 Install-Package ModernUI.WPF

 

Create a ModernUI Window

First create a new WPF Application Project named “ModernUISample”. After creating project, select MainWindow.xaml, change the XAML as following:

<mui:ModernWindow x:Class="ModernUISample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI" Title="MainWindow">

The ModernWindow class is defined under FirstFloor.ModernUI.Windows.Controls, so add following line to MainWindow.xaml.cs to import this namespace

using FirstFloor.ModernUI.Windows.Controls;

Then let MainWindow inherit from class ModernWindow

public partial class MainWindow : ModernWindow

 

Click “Debug” button to run the program, we will see a black rectangle with a light blue border is shown. What’s wrong with it? It’s because that we have not applied the theme files to our application.

Open App.xaml, and add following code:

 

<Application.Resources>
  <ResourceDictionary>
    <!-- WPF 4.0 workaround -->
    <Style TargetType="{x:Type Rectangle}" />
    <!-- end of workaround -->
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
      <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

 

Run the program again, you will see a modern window is shown

Modern UI blank window

The post WPF ModernUI Tutorial Part 1 appeared first on Redino blog.


WPF Dragablz Tutorial Part 2: Set Custom Tab Host Window

$
0
0

Introduction

Dragablz is a WPF Tab Control which can let us to drag and drop tabs, drag tabs out as floating window and docking support, etc. It’s a pretty good control, but its documentation is too few.

In Dragablz’s official tutorial, we use default InterTabController for TabablzControl, and InterTabClient property is not specified for InterTabController, tearing a tab directly will create a window which is same as the container window of the TabItem. In this tutorial we will show you how to create a custom Tab Host Window.

 

Step 1 – Implement IInterTabClient

To set a custom Tab Host window, we should implement the IInterTabClient interface, which contains two method signature

INewTabHost<System.Windows.Window> GetNewHost(IInterTabClient interTabClient, object partition, TabablzControl source)

TabEmptiedResponse TabEmptiedHandler(TabablzControl tabControl, System.Windows.Window window)

Only the first method GetNewHost will be used here, so we can ignore the TabEmptiedHandler method.

Create a new class named MainInterTabClient.cs and make it implement IInterTabClient interface, then modify the GetNewHost method like following

public INewTabHost<System.Windows.Window> GetNewHost(IInterTabClient interTabClient, object partition, TabablzControl source)
{
     return null;
}

The return value is the new host for dragged out tab, next we will create a new window for it.

 

Step 2 – Create a new Tab Host Window

Add a new window named TabHostWindow

Visual Studio add new window

Visual Studio add new window

In XAML View, add following code

<dragablz:TabablzControl Name="TabsContainer">
     <dragablz:TabablzControl.InterTabController>
           <dragablz:InterTabController></dragablz:InterTabController>
     </dragablz:TabablzControl.InterTabController>
</dragablz:TabablzControl>

 

Step 3 – Add Window into TabClient

Go back to MainInterTabClient.cs, update the GetNewHost method

public INewTabHost<System.Windows.Window> GetNewHost(IInterTabClient interTabClient, object partition, TabablzControl source)
{
     var view = new TabHostWindow();
     return new NewTabHost<TabHostWindow>(view, view.TabsContainer);
}

 

Step 4 – Create a ViewModel for MainWindow

Next we need create a ViewModel class to bind newly created InterTabClient, name this class MainWindowViewModel.

class MainWindowViewModel
{
    public IInterTabClient InterTabClient { get; set; }
    public MainWindowViewModel()
    {
        InterTabClient = new MainInterTabClient();
    }
}

 

Step 5 – Bind ViewModel

Now we need bind the ViewModel we created just now, add following line into your MainWindow constructor:

this.DataContext = new MainWindowViewModel();

Then modify XAML of your MainWindow

<dragablz:TabablzControl>
     <dragablz:TabablzControl.InterTabController>
           <dragablz:InterTabController InterTabClient></dragablz:InterTabController>
     </dragablz:TabablzControl.InterTabController>
</dragablz:TabablzControl>

 

Finally, we finish it! Drag a tab out you will see the tab will be inside a new clean window.

The post WPF Dragablz Tutorial Part 2: Set Custom Tab Host Window appeared first on Redino blog.

IKVM.NET Convert JAR to .NET (C#, VB.NET) DLL

$
0
0

Why Convert JAR to .NET DLL

Imagine your company builds an online file converter, and its backend programming language is Java because of its position in web development. And in this project you build a file parsing library (jar format certainly) which is used to parse different types of files.

After this project is done, your boss decides to create a Windows desktop version for this software. Now the best choice is .NET, but how can you reuse your java parsing library? That’s why IKVM.NET existed.

IKVM.NET Introduction

IKVM.NET is a .NET implementation of Java which allows us to run Java code in .NET (C#, VB.NET, etc.) easily. It includes following three components:

  • JVM in .NET (which means you can run java byte code in .NET)
  • Java class library implementation
  • A compiler converting java byte code to .NET IL

There are many famous Java projects converted to .NET using IKVM.NET already, such as Tika, Curator, etc. Even Mono has include it as a built-in component.

Start Converting

ikvmc libpst.jar -target:library

This will generate a DLL file named libpst.dll from libpst.jar in current directory.

Without -target:library option, it will generate an exe file.

 

The post IKVM.NET Convert JAR to .NET (C#, VB.NET) DLL appeared first on Redino blog.

C# Int (Integer) Infinity

$
0
0

Background

I’m implement a data structure which is used to convert a time range to human-readable text, e.g. 0-7 days to “Within 1 Week”, 7-30 days to “1 Week to 1 Month”. Here I use Tuple<int,int> to represent the time range but not two integers, because we can define a Dictionary<Tuple<int, int>, String> to represent a map from time range to text.

Dictionary<Tuple<int, int>, String> timeRanges = new Dictionary<Tuple<int, int>, String>();

timeRanges.Add(new Tuple<int, int>(0, 7), "Within 1 Week");

timeRanges.Add(new Tuple<int, int>(7, 14), "1 Week to 2 Weeks");

But how could we represent “Over 2 Weeks”? Apparently the second parameter of the Tuple should be infinity.

Infinity

To represent infinity for an integer, we can use Int.MaxValue

So we can solve above problem using following code

timeRanges.Add(new Tuple<int, int>(14, Int.MaxValue), "Over 2 Weeks");

 

The post C# Int (Integer) Infinity appeared first on Redino blog.

Apache Derby Database Browser

$
0
0

Background

Apache Derby is an embed database implemented in pure Java. It’s implemented in pure Java, thus it’s more compatible with Java.

There are mainly three database browsers for Derby

Since the first two softwares are commercial, in this tutorial we will introduce how to use SQuirreL.

 

Prerequisites

  • JRE (or JDK)

Download JRE (or JDK) from java.com and install it. Don’t forget to add its bin path to environment variable.

  • SQuirreL

Download SQuirreL from installation page and install it using following command

java -jar squirrel-sql-<version>-install.jar

(or you can double click the jar file if you’re under Windows)

  • Derby Client and Derby Embedded Driver jar files

Download the zip file from this link, then extract derby.jar and derbyclient.jar from the zip file. Next copy these two files into SQuirreL’s lib folder.

If the two jar files are copied to correct path, you will see in SQuirreL’s Drivers panel Apache Derby Client and Apache Derby Embedded are enabled.

SQuirreL Drivers Panel

SQuirreL Drivers Panel

 

 

Create a connection to Derby database

In Alias Panel, click Add Alias button

SQuirrel add alias

SQuirrel add alias

The Name field is not important, and you can fill in with anything. In URL field you enter the server address and database name, e.g. jdbc:derby://localhost/derby_test

 

And if you want to connect to embedded file database,  you need set JDBC Connection URL like following:

jdbc:derby:PATH

Assume the database is located at D:\derby_test, then the JDBC URL should be

jdbc:derby:D:\derby_test

SQuirreL add embed derby database

SQuirreL add embed derby database

 

 

The post Apache Derby Database Browser appeared first on Redino blog.

WPF Set StartupUri in code

$
0
0

Introduction

StartupUri is a property of Application class, which sets the first UI to show when application starts. By default it’s set in App.xaml file, e.g.

<Application x:Class="MEFCalculator.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">

In above code the StartupUri value is MainWindow.xaml, and using default value is OK for most of times.

 

Why Set StartupUri in Code?

But what if you want to show other windows according to different conditions? Assume our application can open .proj files, so the file name will be passed in as command line arguments.  Now we want to show a file chooser dialog at startup if user run our application directly (without double clicking .proj files), and open another window (call it Project Explorer) when user start application by double clicking .proj file.

In this case we need to check command line arguments, then set corresponding StartupUri in code.

 

How to Do it

First in App.xaml.cs, add an override method OnStartup for Application class.

protected override void OnStartup(StartupEventArgs e)
{
     
}

This method will be called at startup of application.

 

Next we set the StartupUri according to whether filename is passed in.

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    String[] args = System.Environment.GetCommandLineArgs();
    // opened by double clicking project file (passed in filename as command line arguments)
    if (args.Length > 1)
    {
            this.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
    }
    // open application directly
    else
    {
            this.StartupUri = new Uri("UI/Windows/DashboardWindow.xaml", UriKind.Relative);
    }
}

In above code we call System.Environment.GetCommandLineArgs method to get command line arguments. You can see the StartupUri is actually an Uri object, its UriKind is UriKind.Relative. (If we don’t specify the UriKind parameter, we will get an UriFormatException)

 

 

The post WPF Set StartupUri in code appeared first on Redino blog.

Viewing all 80 articles
Browse latest View live




Latest Images