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. Buttons aren't showing
Qt 6.11 is out! See what's new in the release blog

Buttons aren't showing

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorqstackedwidget
4 Posts 2 Posters 2.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.
  • A Offline
    A Offline
    AlaaM
    wrote on last edited by AlaaM
    #1

    I'm trying to create a simple application with 4 buttons using QStackedWidget (1 screen, 4 buttons).
    But the screen is showing empty (without the buttons). Here's the code:

    main.cpp:

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        MainWindow mainWindow;
    
        mainWindow.showFullScreen();
        return app.exec();
    }
    

    mainwindow.cpp:

    #include "mainwindow.h"
    
    QStackedWidget *stackedWidget;
    QVBoxLayout *vLayout;
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
    
        // Create widgets to populate the pages
        for(int i=0; i<BUTTONS_NUMBER; i++)
        {
            widgets[i] = new QWidget;
        }
    
        // Create buttons to navigate between the pages
        int x1Point = (WINDOW_WIDTH-ButtonWidth)/2;
        for(int i=0; i<BUTTONS_NUMBER; i++)
        {
            buttons[i] = new QPushButton(widgets[i]);
            buttons[i]->setText(tr("&abutton"));
            buttons[i]->setGeometry(QRect(QPoint(x1Point, ButtonHeight*0), QSize(ButtonWidth, ButtonHeight)));
            buttons[i]->show();
        }
    
        // Create stacked widget for the pages
        stackedWidget = new QStackedWidget;
        for(int i=0; i<BUTTONS_NUMBER; i++)
        {
            stackedWidget->addWidget(widgets[i]);
        }
        stackedWidget->setCurrentIndex(0);
    
        vLayout = new QVBoxLayout;
        vLayout->addWidget(stackedWidget);
    
    //    setLayout(vLayout); //if enabled, it generates the error: "QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout"
    }
    

    while both stackedWidget and vLayout are global variables declared in a separate *.h file as externs:

    extern QStackedWidget *stackedWidget;
    extern QVBoxLayout *vLayout;
    

    and both widgets and buttons are private variables of MainWindow.
    Here's mainwindow.h:

    namespace Ui {
        class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        //...
        ~MainWindow(){}
    private slots:
        //...
    private:
        QWidget* widgets[BUTTONS_NUMBER];
        QPushButton* buttons[BUTTONS_NUMBER];
        const int ButtonWidth = 200;
        const int ButtonHeight = 50;
    };
    

    Only an empty window is shown (after executing mainWindow.showFullScreen() in main.cpp. But I need to show also the buttons.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      hi
      QMainwindow already have layout to handle docking so it a bit special.
      This code does show buttons.

      MainWindow::MainWindow(QWidget* parent)
        : QMainWindow(parent) {
        resize(500, 500);
      
        // Create widgets to populate the pages
        for(int i = 0; i < BUTTONS_NUMBER; i++) {
          widgets[i] = new QWidget;  
        }
      
        // Create buttons to navigate between the pages
        int x1Point = (width() - ButtonWidth) / 2;
        for(int i = 0; i < BUTTONS_NUMBER; i++) {
          buttons[i] = new QPushButton(widgets[i]);
          buttons[i]->setText(tr("&abutton") + QString::number(i)); // give number for test
          buttons[i]->setGeometry(QRect(QPoint(x1Point, ButtonHeight * 0), QSize(ButtonWidth, ButtonHeight)));
          buttons[i]->show();
        }
      
        // Create stacked widget for the pages
        stackedWidget = new QStackedWidget();
      
        for(int i = 0; i < BUTTONS_NUMBER; i++) {
          stackedWidget->addWidget(widgets[i]);
        }
        stackedWidget->setCurrentIndex(0);
      
        setCentralWidget(stackedWidget);
      
      }
      
      
      1 Reply Last reply
      1
      • A Offline
        A Offline
        AlaaM
        wrote on last edited by
        #3

        So I'm only missingsetCentralWidget(stackedWidget);
        Adding it worked. Thanks
        BTW, I have a typo: ButtonHeight*0 should be ButtonHeight*i

        mrjjM 1 Reply Last reply
        0
        • A AlaaM

          So I'm only missingsetCentralWidget(stackedWidget);
          Adding it worked. Thanks
          BTW, I have a typo: ButtonHeight*0 should be ButtonHeight*i

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @AlaaM
          Well , i did see it but button start in 0,0 perfect valid so did not think much about it.
          Just added + QString::number(i) so when testing by changing
          stackedWidget->setCurrentIndex(0);
          I could see it was indeed another button :)

          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