Qt adding custom widget to a layout

2019-02-26 06:36发布

I realized that I cant add any custom widgets to any layouts so I made this example. This ex creates two buttons - the regular "works1" in the correct place in the mainWindow but the custom widget shows up after it, in a separate window.

I also have a message in the output win saying

QWindowsWindow::setGeometry: Unable to set geometry 75x23+520+285 on QWidgetWindow/'QPushButtonClassWindow'. Resulting geometry: 116x23+520+285 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215). QWindowsWindow::setGeometry: Unable to set geometry 100x100+520+285 on QWidgetWindow/'QPushButtonClassWindow'. Resulting geometry: 116x100+520+285 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMainWindow w;

    QWidget *topWidget = new QWidget;
    w.setCentralWidget(topWidget);

    QHBoxLayout *mainLayout = new QHBoxLayout(topWidget);

    QPushButton *myButton = new QPushButton("works1");
    mainLayout->addWidget(myButton);

    MyCustomWidget *newCustom = new MyCustomWidget;
    newCustom->show();
    mainLayout->addWidget(newCustom);

    w.show();

    return a.exec();
}


MyCustomWidget::MyCustomWidget(QWidget *parent) : QWidget(parent)
{
    QPushButton *myButton = new QPushButton("works2");
    myButton->setVisible(1);
    myButton->resize(100, 100);
}


class MyCustomWidget : public QWidget
{
//    Q_OBJECT
public:
    MyCustomWidget(QWidget *parent = 0);

signals:

public slots:

};

0条回答
登录 后发表回答