Qt learning curve journal, part I

The goal – to create a cross platform application infrastructure for Linux, Windows and Mac that will serve as the basis for future cross platform application.

I first thought of using wxwidgets but noticed that there were problems getting it to compile on Mountain Lion OS/X. In addition, Qt seemed more equipped to handle to sort of functionality and support I require in the projects that will be developed. Although Nokia recently sold Qt to Digia, my assumption is that it will continue to be developed and supported by a wide community of developers.

In addition, a secondary goal is to evaluate Necessitas, the Qt port for the Android platform and check work in process for iOS and Windows Phone.

I’ll first focus on the process for Linux (tested on Ubuntu 12.04) and OS/X (tested on Mountain Lion).

Installation

Linux
First thing’s first – download the Qt libraries for Linux/X11, extract, cd to the folder and ./configure. You will be asked if you want to use the open source or commercial license version (since the open source license is LGPL, I assume choosing this option creates a shared library). after some additional questions, the configure process will begin to create the make file (its a big project so give it time). Finally, if all goes well you should see:

Qt is now configured for building. Just run 'make'.
Once everything is built, you must run 'make install'.
Qt will be installed into /usr/local/Trolltech/Qt-4.8.3

To reconfigure, run 'make confclean' and 'configure'.

Expect make to take a very long while (as in take a long lunch break). This is a huge project.

OS/X
From the location specified above, you can either download the two dmg files (library and debug) or download the same file as for Linux if you wish to go through the compilation process. In the latter case, there are some changes in the compilation method that are specified here. I opted for the easier method of simply downloading and installing the dmg files.

Another (and perhaps better) option on OS/X is to install homebrew and simply issue an

brew install qt

Compiling and running an example

Linux
On Linux the tutorial sample is located at qt-everywhere-opensource-src-4.8.3/examples/tutorials/gettingStarted/gsQt/part1 (under the directory you extracted it to).

To make the sample, issue:

qmake -project
qmake
make

OS/X
On OS/X the tutorial sample is located at /Developer/Examples/Qt/tutorials/gettingStarted/gsQt/part1
To make the sample, issue:

qmake -spec macx-g++
qmake
make

On my Mac, the make resulted in the following error:

g++: error: x86_64: No such file or directory
g++: error: unrecognized option '-arch'
g++: error: unrecognized option '-Xarch_x86_64'
make: *** [main.o] Error 1

if you get compilation errors during make, your compilers could be different from the standard versions for your Xcode. If you have Xcode 4.3 or newer, you should install the Command Line Tools for Xcode from within Xcode’s Download preferences (via menu: Xcode->Preferences…).

I learned the above from homebrew (which by the way is a great package manager for OS/X – bye bye fink and macports) after installing it and running homebrew doctor. Once I installed the command line tools for Xcode the make completed smoothly.

Recursively remove specific filename in Linux

The trick is to use the find command with the -exec or -execdir parameter. I needed to remove all the files named serials_dev.db3 from multiple backup directories. The following did the job:

find . -type f -name "serials_dev.db3" -exec rm -f {} ;

Here are the relevant parts from the find man page:

       -exec command ;
              Execute command; true if 0 status is returned.  All following
              arguments to find are taken to be arguments  to  the  command
              until  an  argument  consisting  of  `;' is encountered.  The
              string `{}' is replaced by the current file name  being  pro‐
              cessed  everywhere it occurs in the arguments to the command,
              not just in arguments where it is alone, as in some  versions
              of  find.   Both  of  these  constructions  might  need to be
              escaped (with a `') or quoted to protect them from expansion
              by  the  shell.  See the EXAMPLES section for examples of the
              use of the -exec option.  The specified command is  run  once
              for each matched file.  The command is executed in the start‐
              ing directory.   There are unavoidable security problems sur‐
              rounding use of the -exec action; you should use the -execdir
              option instead.

       -exec command {} +
              This variant of the -exec action runs the  specified  command
              on  the  selected  files,  but  the  command line is built by
              appending each selected file name at the end; the total  num‐
              ber  of invocations of the command will be much less than the
              number of matched files.  The command line is built  in  much
              the  same  way that xargs builds its command lines.  Only one
              instance of `{}' is allowed within the command.  The  command
              is executed in the starting directory.

       -execdir command ;

       -execdir command {} +
              Like  -exec, but the specified command is run from the subdi‐
              rectory containing the matched file, which  is  not  normally
              the  directory  in  which you started find.  This a much more
              secure method for invoking commands, as it avoids race condi‐
              tions  during  resolution  of the paths to the matched files.
              As with the -exec action, the `+' form of -execdir will build
              a command line to process more than one matched file, but any
              given invocation of command will only list files  that  exist
              in  the  same subdirectory.  If you use this option, you must
              ensure that your $PATH environment variable does  not  refer‐
              ence  `.';  otherwise,  an attacker can run any commands they
              like by leaving an appropriately-named file in a directory in
              which  you  will  run  -execdir.   The same applies to having
              entries in $PATH which are empty or which  are  not  absolute
              directory names.

A collection of older information

This is the previous content of the nocurve.com site, where the idea was to store information that will assist in saving precious time when trying to develop something or fix something.

Geany

This is the home page

One crazy issue I had on Windows was that for some reason at some point the Geany window disappeared. The task was active, clicking the icon would bring it to focus but would not show the Window. It was obviously hidden or thrown out of the desktop view. I assumed the window saved it’s state with some insane coordinates but couldn’t get it back to show in the desktop.

Finally found the answer here – sure helps when you know some Windows basic techniques…

“I have installed Geany 0.19.1 on Windows 7, I run the Geany.exe shortcut and it appears running on my windows taskbar, if I hit Alt+Tab I see the Geany icon. But… no window is shown. Where is the Geany window? How to fix it?”

“Press Alt-Space, then M, then the arrow keys to move the window into view.” (you can also use the mouse instead of the arrow keys to speed up the process)

DIV Charts

Attachment “jsdivcharts.jpeg” not found
Free (LGPL) DIV based JavaScript Charts. This provides to main advantages to other JavaScript charting libraries:

  • It uses DIVs instead of Canvas or VML, which means it will work virtually on any browser (including the Android native Web browser which as of this writing does not support these technologies).
  • It is really simple to use. The charting library does all the thinking for you 🙂
  • Currently supported: Stacked Bar Charts, Bar charts (Which is just a Stacked Bar Chart with one group) Compound charts (stacked bars and lines), tooltips, Pie Charts, automatic Y axis step calculation, default color support

This charting library builds upon the amazing javascript wz_graphics library created by Walter Zorn. Unfortunately, I only recently learned that Walter Zorn passed away in May 2009. May this little contribution be a tribute to his work.

To view a demo of the DIVCharts library and download it, just go here

Change Google Chrome’s default language on Mac

To change the Google Chrome application language to English on Mac OS/X, open a terminal window and at the command prompt type the following.

defaults write com.google.Chrome AppleLanguages '(en-US)'

The next time you’ll open Chrome it will be in English (actually, its recommended to close Chrome before doing this).

Long Path to Short 8.3 MS-DOS path conversion

Every once in a (long) while I require this. Being surprised by the lack of such a simple utility I quickly made one for public use. You can download the VS2008 C++ project and source code from here. If you just want the executable, you can get it here (just drag the file in the long path to the upper field and click the OK button to get the short path inthe lower field.

Access MSSQL from PHP

On Ubuntu

First, install the LAMP (Linux,Apache,MySQL,PHP) package as follows:

$sudo apt-get install lamp-server^

Note that the ‘^’ at the end is not a typo !

Next, install MSSQL support for PHP (taken from here)

$sudo apt-get install php5-sybase
$sudo /etc/init.d/apache2 restart

On Windows

First install one of the available WAMP (Windows,Apache,MySQL,PHP) packages (I use this)

Next, download and install Microsoft’s PHP extension for MSSQL (from here)

The above setup contains quite a few candidates to be the PHP extension. Make sure you copy the relevant extension dll (in my case it was php_pdo_sqlsrv_53_ts_vc6.dll ) from Microsoft’s setup to the php extensions folder (in my case it is located at c:wampbinphpphp5.3.0ext ) and use either php.ini or the wampserver UI to add it to the list of extension dll’s PHP loads at startup.

Connecting to the MSSQL database

After you’ve done the above, you can now connect to the MSSQL server. The following code will work on both Ubuntu and Windows:

$serverName = "mymssqlserver.com"; 
$database   = "mydbname";
$uid        = "username";
$pwd        = "password";

try
{
    if ( stripos(php_uname(), "linux") === false )
    {
        $g_conn = new PDO( "sqlsrv:server=$serverName;Database=$database", $uid, $pwd);
    }
    else
    {
        $g_conn = new PDO( "dblib:host=$serverName;dbname=$database", $uid, $pwd);
    }
    
    $g_conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 
}
catch( PDOException $e )
{
    die( "Error connecting to SQL Server" ); 
}

// You can now use $g_conn to execute pdo queries.

Simultaneous VPN and Internet Access on Ubuntu

This might be already solved in newer (>11.04) versions of Ubuntu.

The following assumes you installed a VPN connection in Ubuntu, but when you connect to the VPN you can’t access the public Internet. It took me some time until I found an answer that worked here:

“First, I determined what ip address ranges were being used on the VPN, in my case 192.168.32.* and 192.168.16.*. Then I connected to my vpn normally and sshed to a server on the network. I ran route on that machine and got the gateway address and metric being used. Then I added routes for the two ip address ranges to the routes field in the IPv4 settings tab and clicked on “Use this connection only for resources on its network” and “Ignore automatically obtained routes”. Then poof like magic it worked.”

ssh – automatic remote login

Since I don’t do this often enough to remember – here’s the procedure summarized (for Linux and OS/X, on Windows use putty):

me@here is username me at ip/hostname here
user@there is username user at ip/hostname there

me@here$ cd
me@here$ ssh-keygen -t rsa

press return a few times, don’t enter passphrase. This will create a public/private RSA key pair in your .ssh folder

Create the .ssh directory on the remote device (if it doesn’t already exist), and then add the local public rsa key to it’s list of authorized keys:

me@here$ ssh user@there mkdir -p .ssh
user@there's password:
me@here$ cat .ssh/id_rsa.pub | ssh user@there 'cat >> .ssh/authorized_keys'
user@there's password:

the next time you ssh user@there you will not be prompted for a password.

Note that some systems might require the following:

  • Placing the public key in .ssh/authorized_keys2
  • Changing permissions of .ssh to 700
  • Changing permissions of .ssh/authorized_keys2 to 640


git ssh keys on Windows

If you install git on Windows, the ssh key will be in directory

c:Program Files (x86)Git.ssh

If you have linux tools for windows installed, the .ssh folder it will use will be in your user directory

Lua with nginx

Openresty is an excellent piece of work that enables combining Lua scripting with the power of nginx, thereby giving you Lua as a server side scripting engine. Installing it is simple – just get the stable or latest version from the site, open it, ./configure, make and sudo make install.

On a clean Ubuntu, you will need to add the openssl and pcre libraries before doing the above so simply run:

sudo apt-get install libpcre3-dev
sudo apt-get install libssl-dev

and then follow the above steps.

Extensive documentation and sample snippets for openresty are available on the nginx site on the page for the HttpLuaModule

On a fresh Ubuntu, you might get all kinds of locale warnings or errors and get giberish when viewing foreign language files in the console – the solution:

sudo apt-get install language-pack-en-base
sudo dpkg-reconfigure locales

Calculating Hebrew Holidays

hebcal is a nice utility that compiles on all major platforms and calculates the Hebrew holidays using a perpetual calendar.

iconv is an amazing piece of work that can convert between zillions of encoding formats and is also available for all major platforms.

By default, just running hebcal displays the current year’s Hebrew holidays in English. Adding the parameter -8 displays them as 8-bit Hebrew (ISO-8859-8-Logical), which was gibberish on my console. To display the Hebrew holidays in UTF-8 Hebrew I just did the following:

hebcal -8  | iconv -f ISO-8859-8 -t UTF-8

Works like a charm.

By the way, if you want to convert the encoding of a file (say cal.txt) using iconv but you have no idea what the current encoding is, from a linux console, just issue the following

file -i cal.txt

Note that for non-unix platforms where you don’t have the file command you can try and use chared or simply drag the file to your favorite Internet browser. If it is displayed correctly check what encoding was detected by the browser and if not, play around with the encodings until it does…

After you find the current encoding, you can then check if iconv supports it and the parameter iconv requires by issuing:

iconv -l

Ubuntu – Accessing sqlite3 from Lua

I thought this would be trivial but ended up fighting with various solutions until I found one that worked without requiring additional configurations.

So first you might need to install the sqlite3 development libraries (which are needed to compile the Lua sqlite3 connector):

sudo apt-get install libsqlite3-dev

install luarocks if you don’t have it

sudo apt-get install luarocks

and finally, install lsqlite3:

sudo luarocks install lsqlite3

Web page is at this site and you can find some usage examples here