Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

You're creating a button and a window, but you're not associating them together. Therefore, the button is not part of the main UI. In order to add the button to the main window, you should add the following line-

mWin->setCentralWidget(button);

When creating user interfaces with Qt, it's better to avoid setting the geometry explicitly. Here is a nice page which describes the layouting in Qt.

Also, you seem to be missing a main function.

You are not setting parent child relationship, you need QPushbutton as a child of QMainwindow. Then you can see QPushbutton on QMainwindow.

Please try this, I am not sure what exactly you want to achive..but below code will give some hind about how to proceed..

  QApplication a(argc, argv);
    QMainWindow *mWin = new QMainWindow();
    QPushButton *button = new QPushButton(mWin); // setting parent as QMainwindow
    button->setGeometry(0,0,128,56);
   // button->show();
//mWin->setCentralWidget( button );

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

;

Create a central widget for the main window and then use a layout to place the push button on the central widget. For example:

QMainWindow *mWin = new QMainWindow();

// Place the button in a layout
QHBoxLayout *layout = new QHBoxLayout;
QPushButton *button = new QPushButton("Button");
layout->addWidget(button);

// Set up a central widget for the main window
QWidget *centralWidget = new QWidget();
centralWidget->setLayout(layout);
mWin->setCentralWidget(centralWidget);

If you've placed your button into a layout this way (or as a central widget itself, as one of the other answers has indicated), you don't need to set the geometry on the button, nor do you need to call setVisible or show on the button explicitly (but you still need to on the main window).

Consider looking through some of Qt's examples, such as the application example for more information.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top