Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Simple QMainWindow subclassing not showing anything
QtWS25 Last Chance

[SOLVED] Simple QMainWindow subclassing not showing anything

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jarzka
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      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.

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jarzka
        wrote on last edited by
        #3

        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. :)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved