[SOLVED] Simple QMainWindow subclassing not showing anything
-
wrote on 23 Oct 2012, 17:51 last edited by
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?
- qHBoxLayoutMain->show()
-
wrote on 23 Oct 2012, 18:23 last edited by
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. -
wrote on 23 Oct 2012, 18:26 last edited by
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. :)
3/3