Archive for the ‘Qt’ Category

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. }
  7.  
  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.

Example:

  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.