Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTabWidget not displayed
Forum Updated to NodeBB v4.3 + New Features

QTabWidget not displayed

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 8.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    newe1
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      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.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • N Offline
        N Offline
        newe1
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #4

          No problem. Glad it works.

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • N Offline
              N Offline
              newe1
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                ZapB
                wrote on last edited by
                #7

                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.

                Nokia Certified Qt Specialist
                Interested in hearing about Qt related work

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    newe1
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Scylla
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved