Qt World Summit: Submit your Presentation

Newbie Tabpage delete

  • Hi all,

    I'm a developer that mainly works with Powerbuilder and .NET/C# on Windows but with some C experience in the past. I was looking for a development framework/tools to migrate some personal applications to Linux and come to QT. At the moment, after reading some tutorials, books and done some test, I'm very impressed with the possibilities and how easy it's to do some thinks with QT.

    Now I have started to port a simple aplication to QT and have some questions.

    My main window has a ListWidged on the left side and a TabWidget on the CentralWidget.

    On the ListWidged I have my menu options and if the user clics on one item, I open a new tabpage on the TabWidged with the form. I allow only one tab for every item on le ListWidged and the user can close the tabpage.

    I have already coded this functionality but I'm sure it's not optimal, therefore I need your help to improve my learning curve.

    First question: Is it necessary to delete/destroy my tabpage after closing it with remove tab? Are my code OK?

    To open the forms, I have declared a pointer in my mainwindow for every different form I can open. Is there a general solution/function to add a new form to the tab. Please look at my code. I want to avoid the openFormAccount and the switch on item changed slot, and have a general openForm method. How can I do it?

    This is my code:
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "formaccounts.h"
    #include <QListWidgetItem>

    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    Ui::MainWindow ui;

    private slots:
    void listWidgetCurrentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
    void openFormAccounts();
    void removeTab(int index);

    #endif // MAINWINDOW_H

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QString>
    #include <QStringList>
    #include <QDebug>

    MainWindow::MainWindow(QWidget parent) :
    ui(new Ui::MainWindow)
    // connect(ui->listWidget, SIGNAL(currentItemChanged(QListWidgetItem
    ,QListWidgetItem*)), this, SLOT(listWidgetCurrentItemChanged(QListWidgetItem*,QListWidgetItem*)));
    formAccounts = 0;

    delete ui;

    void MainWindow::openFormAccounts()
    if(formAccounts == 0)
    formAccounts = new FormAccounts();
    ui->tabWidget->addTab(formAccounts, "Accounts");



    void MainWindow::listWidgetCurrentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)

    case 1:


    void MainWindow::removeTab(int index)

    QString classname = ui->tabWidget->widget(index)->metaObject()->className();
    if (classname == "FormAccounts")
        delete formAccounts;
        formAccounts = 0;


    I think the mainwidow.ui is not necessary. There are the controls and the connections between slots an signals.

    A lot of thanks,

    Alex B.

Log in to reply