[SOLVED] Simple QMainWindow subclassing not showing anything



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

    MainWindow::MainWindow()
    {
    setWindowTitle("Info");

    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");
    qFormLayoutForm->addRow(qPushButtonOk);

    qHBoxLayoutMain->addLayout(qFormLayoutForm);
    

    }
    @

    main.cpp
    @#include "MainWindow.h"

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

    I thought mainWindow.show() 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":http://doc.qt.digia.com/qt/qmainwindow.html#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();
    setCentralWidget(centralWidget);

    qHBoxLayoutMain = new QHBoxLayout();
    centralWidget->setLayout(qHBoxLayoutMain);@
    

    and now it works. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.