Posts Tagged ‘qt’

QMdiArea tabs with close button

Saturday, March 6th, 2010

If you want to build an application with multiple document interface and tabs, you don’t have the close buttons in the tabs by default. That’s why I’ve come up with a workaround to get those buttons in every tab. Here’s a code example:

  1. Q_FOREACH (QTabBar* tab, ui>mdiArea>findChildren())
  2. {
  3.    tab>setTabsClosable(true);
  4.    connect(tab, SIGNAL(tabCloseRequested(int)),
  5.                this, SLOT(closeTab(int)));
  6. }
  8. void MainWindow::closeTab(int i)
  9. {
  10.    QMdiSubWindow *sub = ui>mdiArea>subWindowList()[i];
  11.    QWidget *win = sub>widget();
  12.    win>close();
  13.    ui>mdiArea>setActiveSubWindow(sub);
  14.    ui>mdiArea>closeActiveSubWindow();
  15. }

Filtering relational tables in Qt Sql

Friday, February 19th, 2010

I have a database which I’m using in my program which has foreign keys with same column name as in the table which it’s related. So I tried to set a filter to the related tables using setFilter(“relationtable.fieldName = ‘value'”), but it’s not working. I found out that Qt makes aliases to the relation table. Qt’s reference manual says “The alias is is the relation’s table name and display column name joined by an underscore (e.g. tablename_columnname)”.

I tried that but I think there’s somekind of bug because the query still says it’s an unknown column. However, I looked through the Qt’s Sql source code and found a solution.

For the relation table name you should use name like relTblAl_2, where the number after the underscore is the foreign key column in the main table.


  1. relTableModel>setTable("MainTable");
  2. relTableModel>setRelation(2, QSqlRelation("RelationTable", "ID", "Name"));
  3. relTableModel>setFilter("relTblAl_2.Name = 'Example'");

In this example table named RelationTable is aliased as relTblAl_2. By using the alias you can filter the values in the relational table.

SpeedTest – Qt game

Saturday, February 6th, 2010

It’s just basic clicking game made in a couple of hours but it was  good way to find out how to do a cross-platform project with Qt.

It builds with no problems for Symbian and Windows platform and probably to others as well with some tweaks. Especially great was the Qt’s interface for symbian touch screen devices. Qt handles the touches as mouse clicks so it’s easy to do programs/games which uses the touch screen devices.

Only drawback for developing with Qt for Symbian is the lack of the smart installer atm(release date Q1/2010?). It means, that you have to include the Qt installer to the .sis file which increases the size of the program. In the future Qt is included to the mobile phones so we’ll just have to wait for that.

Project information:  SpeedTest

Locking screen orientation to landscape in Qt for Symbian

Sunday, January 31st, 2010

In my current project I’m programming a game for Symbian which will run only in landscape mode. So somehow I needed to lock the application to use only the landscape mode as some mobiles change the orientation automatically. I found a code snippet for that from Nokia’s forum.

The cone.lib, eikcore.lib, and avkon.lib Symbian libraries must be added to your Qt project:

  1. symbian: {
  2.     LIBS += lcone leikcore lavkon
  3. }

You also need some extra header files added to your source file:

  1. #ifdef Q_OS_SYMBIAN
  2. #include <eikenv.h>
  3. #include <eikappui.h>
  4. #include <aknenv.h>
  5. #include <aknappui.h>
  6. #endif

Create a window to your app and include the  following code before the window is shown. It will lock the orientation to landscape mode:

  1. CAknAppUi* appUi = dynamic_cast (CEikonEnv::Static()>AppUi());
  2. TRAPD(error,
  3. if (appUi) appUi>SetOrientationL(CAknAppUi::EAppUiOrientationLandscape);
  4. );

Now the orientation is locked and it will not change manually by user or automatically.