Important: Please read the Qt Code of Conduct -

[SOLVED] Simple QMainWindow subclassing not showing anything

  • MainWindow.cpp
    @#include "MainWindow.h"


    qHBoxLayoutMain = new QHBoxLayout(this); // makes this layout a child of MainWindow

    qFormLayoutForm = new QFormLayout();
    qLineEditFirstName = new QLineEdit();
    qFormLayoutForm->addRow("First name:", qLineEditFirstName);
    qLineEditLastName = new QLineEdit();
    qFormLayoutForm->addRow("Last name:", qLineEditLastName);
    qPushButtonOk = new QPushButton("OK");



    @#include "MainWindow.h"

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

    I thought would do this:

    • qHBoxLayoutMain->show()
      ** qFormLayoutForm->show()
      *** qLineEditFirstName->show()
      *** qLineEditLastName->show()
      *** qPushButtonOk->show()

    But everything I see is an empty window. What's wrong with this?

  • Is MainWindow class derived from QMainWindow? (if not tell us the base class too, if it's derived from QMainWindow you need to use "centralWidget": to construct your ui into)
    Also look at the output console in Creator to see if you got some layout errors.

  • Yes, MainWindow is derived from QMainWindow. And yes I just noticed that I need the central widget and it's missing. My bad.

    I added this to my code:
    @ centralWidget = new QWidget();

    qHBoxLayoutMain = new QHBoxLayout();

    and now it works. :)

Log in to reply