Buttons aren't showing
-
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
andvLayout
are global variables declared in a separate*.h
file asexterns
:extern QStackedWidget *stackedWidget; extern QVBoxLayout *vLayout;
and both
widgets
andbuttons
are private variables ofMainWindow
.
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. -
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); }
-
@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 :)