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

Buttons aren't showing

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorqstackedwidget
4 Posts 2 Posters 1.9k Views
  • 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 24 Nov 2015, 15:43 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 24 Nov 2015, 19:16 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 26 Nov 2015, 14:03 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

        M 1 Reply Last reply 26 Nov 2015, 14:19
        0
        • A AlaaM
          26 Nov 2015, 14:03

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 26 Nov 2015, 14:19 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

          1/4

          24 Nov 2015, 15:43

          • Login

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