Search code examples
qtqwidgetqmainwindowqpushbutton

QPushButton Main Gui


I'm beginning with Qt and I do not understand something. Here is my code:

#include <QApplication>
#include <QtWebKitWidgets/QWebView>
#include <QMainWindow>
#include <QPushButton>

    QApplication a(argc, argv);
    QMainWindow *mWin = new QMainWindow();
    QPushButton *button = new QPushButton("Button");

    button->setGeometry(0,0,128,56);
    button->show();

    mWin->setGeometry(QRect(300,300,1024,640));
    mWin->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
    mWin->show();

    return a.exec();

When I launch the program, it makes a pushbutton, but not on the main GUI. It instead makes it in the upper lefthand corner of my scoeen. How do I make the pushbutton part of the main GUI?


Solution

  • Both QMainWindow and QPushButton derive from QWidget and generally, QWidgets can display themselves independently, so don't need to be attached to a parent, though it's likely you'll want to group and organise the widgets you're presenting to the user.

    For a beginner, I think that QMainWindow is a bit misleading as you don't actually need it to display widgets. If, for example, you wanted to just display the button, you only need create the QPushButton and call show: -

    QPushButton pButton = new QPushButton("Test");
    pButton->show();
    

    Though it's rare that you'll actually want to do this. In actuality, though it appears pointless, you can even just create a QWidget and display that: -

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QWidget w;
        w.show();
        return a.exec();
    }
    

    The QWidget can now be the parent to all other widgets, still without you defining a QMainWindow.

    It is likely that you will want other widgets to display and Qt uses a parent / child hierarchy system of linking them together. Amongst other things, an advantage here is that you don't need to keep track of the different widgets in order to release their memory. When you're finished, you just delete the top level item, which may be a QMainWidget, or QDialog or other QWidget based object. In the case of Windows, all you need to do is close them and their children are cleaned up too.

    Adding Widgets to a QMainWindow can be done in different ways, depending upon what you're trying to achieve. Eventually, you'll likely want to use layouts that allow grouping of widgets in logical arrangements, but to start with, if you look at the constructor of the QPushButton, you'll see that it takes an optional parameter of QWidget* : -

    QPushButton(QWidget * parent = 0)
    

    This allows you to attach the widget you're creating (QPushButton in this case) to a parent. Therefore, create the QMainWindow and then pass its pointer to the QPushButton.

    QMainWindow* pWindow = new QMainWindow; // create the main window
    QPushButton pButton = new QPushButton(pWindow); // create the push button
    
    pWindow->show(); // Display the main window and its children.