Qt adding custom widget to layout

I realized that I cannot add any custom widgets to any layouts, so I made this example. This ex creates two buttons - the usual "works1" in the right place in mainWindow, but the custom widget appears after it in a separate window.

I also have a message in win output saying

QWindowsWindow :: setGeometry: Unable to set 75x23 + 520 + 285 geometry in 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 the geometry 100x100 + 520 + 285 to 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:

};
+2
source share

All Articles