In my code, I create two buttons, and use QHBoxLayout to set these layout.
if my application is inherited from QDialog, run the application , it's ok.
if my application is herited from QMainWindow, run the application, it is wrong.
Code://example 1 class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); private: QPushButton *button1; QPushButton *button2; }; Dialog::Dialog(QWidget *parent) : QDialog(parent) { button1 = new QPushButton(tr("Button 1")); button2 = new QPushButton(tr("Button 2")); QHBoxLayout *hLayout = new QHBoxLayout; hLayout->addWidget(button1); hLayout->addWidget(button2); QHBoxLayout *vLayout = new QHBoxLayout; vLayout->addLayout(hLayout); setLayout(vLayout); } //example 2 class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); protected: void changeEvent(QEvent *e); private: Ui::MainWindow *ui; QPushButton *button1; QPushButton *button2; }; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { // ui->setupUi(this); button1 = new QPushButton(tr("Button 1"), this); //if it does not have "this", the application will not have Button button2 = new QPushButton(tr("Button 2"), this); QHBoxLayout *hLayout = new QHBoxLayout; hLayout->addWidget(button1); hLayout->addWidget(button2); QHBoxLayout *vLayout = new QHBoxLayout; vLayout->addLayout(hLayout); setLayout(vLayout); }

Reply With Quote

