Design Qt GUI programmatically

I am trying to create a graphical application.

The QMainWindow main window contains 9 fixed-size shortcuts, as well as the size of the main window.

I tried to do this programmatically without the Qt GUI Designer. The project was built without errors, but I do not see a single shortcut or layout displayed in the main window. it's just empty.

Here is my source code:

 WCwindow::WCwindow() { // initialize widgets with text CAM111 = new QLabel("CAM 01"); CAM121 = new QLabel("CAM 02"); CAM131 = new QLabel("CAM 03"); CAM211 = new QLabel("CAM 04"); CAM221 = new QLabel("CAM 05"); CAM231 = new QLabel("CAM 06"); CAM311 = new QLabel("CAM 07"); CAM321 = new QLabel("CAM 08"); CAM331 = new QLabel("CAM 09"); CAM111->setFixedSize(wcW,wcH); CAM121->setFixedSize(wcW,wcH); CAM131->setFixedSize(wcW,wcH); CAM211->setFixedSize(wcW,wcH); CAM221->setFixedSize(wcW,wcH); CAM231->setFixedSize(wcW,wcH); CAM311->setFixedSize(wcW,wcH); CAM321->setFixedSize(wcW,wcH); CAM331->setFixedSize(wcW,wcH); QGridLayout *layout = new QGridLayout; layout->addWidget(CAM111,0,0); layout->addWidget(CAM121,0,1); layout->addWidget(CAM131,0,2); layout->addWidget(CAM211,1,0); layout->addWidget(CAM221,1,1); layout->addWidget(CAM231,1,2); layout->addWidget(CAM311,2,0); layout->addWidget(CAM321,2,1); layout->addWidget(CAM331,2,2); setLayout(layout); setWindowTitle("Camera Window"); setFixedSize(1000, 800); } 

of course, the class is initialized and called in main.cpp:

 int main(int argc, char *argv[]) { QApplication app(argc, argv); WCwindow *WCwin = new WCwindow; WCwin->show(); return app.exec(); } 

What mistake do I have?

+7
source share
2 answers

The code below works fine. The problem was code that you did not show. When you use QMainWindow , as you eventually centralWidget , you need to set its centralWidget using the new widget that you create.

 // main.cpp #include <QVector> #include <QMainWindow> #include <QLabel> #include <QGridLayout> #include <QApplication> class WCwindow : public QMainWindow { public: WCwindow(); private: QVector<QLabel*> cams; QLabel* cam(int r, int c) const { return cams[r*3 + c]; } }; WCwindow::WCwindow() { QGridLayout *layout = new QGridLayout; for (int i = 1; i < 10; ++ i) { QLabel * const label = new QLabel(QString("CAM %1").arg(i, 2, 10, QLatin1Char('0'))); label->setFixedSize(200, 50); layout->addWidget(label, (i-1) / 3, (i-1) % 3); cams << label; } QWidget * central = new QWidget(); setCentralWidget(central); centralWidget()->setLayout(layout); setWindowTitle("Camera Window"); setFixedSize(1000, 800); } int main(int argc, char *argv[]) { QApplication app(argc, argv); WCwindow win; win.show(); return app.exec(); } 
+4
source

Is WCwindow subclass of QMainWindow ? In this case, I would advise you to remove the layout from the window in the graphics editor by clicking the "Split Layout" button in the upper panel, and then use the following:

 //setup all your labels and layout ... //creating a QWidget, and setting the WCwindow as parent QWidget * widget = new QWidget(this); //set the gridlayout for the widget widget->setLayout(layout); //setting the WCwindow central widget setCentralWidget(widget); 
+1
source

All Articles