×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    36

    About QBoxLayout Problem

    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);
    }

  2. #2
    Nokia Developer Expert
    Join Date
    Jul 2010
    Location
    Tampere
    Posts
    2

    Re: About QBoxLayout Problem

    I think it's because you're supposed to use the central widget of QMainWindow to place your widgets.

Similar Threads

  1. QtCreator design mode missing QBoxLayout?
    By hurenkam in forum [Archived] Qt SDKs and Tools
    Replies: 4
    Last Post: 2010-09-13, 16:46

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×