QTabWidget not displayed



  • Hi!

    I just wanted to display the QTabWidget within the MainWindow. But this doesn´t work and I do not even get an error message after compilation. The MainWindow is displayed, but without QTabWidget.

    Here are the files:

    1. mainwindow.h
      @
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QTabWidget>

    class QTabWidget;
    class QWidget;

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    QMainWindow *mainWindow;
    QTabWidget *tabWidget;
    };

    class FirstTab : public QWidget
    {
    Q_OBJECT
    public:
    FirstTab(QWidget *parent = 0);
    };

    class SecondTab : public QWidget
    {
    Q_OBJECT
    public:
    SecondTab(QWidget *parent = 0);
    };

    #endif // MAINWINDOW_H
    @

    1. mainwindow.cpp
      @
      #include "mainwindow.h"
      #include <QtGui>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    tabWidget = new QTabWidget;
    tabWidget->addTab(new FirstTab(), tr("Start"));
    tabWidget->addTab(new SecondTab(), tr("App"));

    setWindowTitle(tr("TabApp"));
    

    }

    FirstTab::FirstTab(QWidget *parent)
    : QWidget(parent)
    {
    QLabel *welcomeLabel = new QLabel(tr("Welcome to the first Tab!"));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(welcomeLabel);
    setLayout(mainLayout);
    

    }

    SecondTab::SecondTab(QWidget *parent)
    : QWidget(parent)
    {
    QLabel *welcome2Label = new QLabel(tr("Welcome to the second Tab!"));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(welcome2Label);
    setLayout(mainLayout);
    

    }

    MainWindow::~MainWindow()
    {
    delete mainWindow;
    }
    @

    1. main.cpp:
      @
      #include <QApplication>
      #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    If you see the mistake, please tell me!



  • You need to set the tab widget as the central widget of the main window:

    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {
    tabWidget = new QTabWidget;
    tabWidget->addTab(new FirstTab(), tr("Start"));
    tabWidget->addTab(new SecondTab(), tr("App"));
    setCentralWidget( tabWidget );

    setWindowTitle(tr("TabApp"));
    

    }
    @

    Note that you can also do this from within Qt designer or Qt-creator's designer plugin. Not a bad idea to learn how to do it directly in code too though.



  • Hi ZapB, thanks!

    It runs! :-)

    Yes, I know I can design it, but I got the impression I learn it better, when I write it step by step. Of course I would be faster with designing.

    Thanks anyway!



  • No problem. Glad it works.



  • Note that the code that Designer produces is also quite readable. If you want to know how to set up a GUI manually, you can just read the generated .h file and reproduce/adapt the contents.



  • Thanks Andre!

    I got the impression that I can choose. Either I write everything on my own. Or I design everything with the Qt-Designer and add the functionality later.

    Is there a possibility to combine both? -> I mean, if start with my written example above and if I´m just interested to design my "SecondTab" how could I load it into the Qt-Designer to design it? Or is it too late if I once started to write it just in the editor?



  • Yes of course. You can decompose it down to whatever level you like. For example you could hand-code the widgets that appear on each tab. Then use designer to add a QWidget to each tab and then "promote" them to your custom hand-written widgets.

    You can mix and match as you please. Rule of thumb, if you think that a widget may be of use somewhere else or maybe moved then don't bury it too deeply directly in designer. Instead pull it out as its own class. That makes moving/reusing it much easier.



  • The only thing you can not do, is design a ui in Designer, adapt the generated .h file manually, and then expect to retain your changes if you then change the .ui file again and the .h file is re-generated. That is why there there is a warning in that file against doing that. But you can pretty much use any other form of mixing and matching, including just creating a basic layout in designer and copying over the generated code as a base for hand-coding and dumping the .ui file from your project again.



  • Thanks for your answers. But I´m sorry, it didn´t work so far.

    I tried to include in the mainwindow.h:
    @namespace Ui {
    class MainWindow;
    }
    ....

    private:
    Ui::MainWindow *ui;
    @

    in main.cpp:
    @
    #include <QtGui/QApplication>
    @

    in mainwindow.cpp:
    @
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    tabWidget = new QTabWidget;
    tabWidget->addTab(new FirstTab(), tr("Start"));
    tabWidget->addTab(new SecondTab(), tr("App"));
    setCentralWidget( tabWidget );
    
    setWindowTitle(tr("TabApp"));
    

    }
    @
    and I designed/clicked a groupBox with 3 radiobuttons.
    But they weren´t displayed.

    My first approach was to directly include it in SecondTab.
    I tried in mainwindow.h:
    @
    class SecondTab : public QWidget
    {
    Q_OBJECT
    public:
    SecondTab(QWidget *parent = 0);
    private:
    Ui::SecondTab *ui;
    };
    @

    in mainwindow.cpp:
    @
    SecondTab::SecondTab(QWidget *parent)
    : QWidget(parent),
    ui(new Ui::SecondTab)
    {
    ui->setupUi(this);

    QLabel *welcome2Label = new QLabel(tr("Welcome to the second Tab!"));
    
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(welcome2Label);
    setLayout(mainLayout);
    

    }
    @
    This didn´t work at all...

    If you see the mistake, please tell me.



  • You have to put your layout in the "ui" form. Now you have to layouts, one in the ui and the second you create in the constructor.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.