2016-04-19 6 views
0

私は窓と一組のプッシュボタンを持っています。このウィンドウが私の「メインメニュー」になります。ボタンを配置した後、これらのボタンをこのウィンドウのサイズに固定したいと思います。したがって、ウィンドウを変更してそのサイズを変更する必要があります(たとえば、ユーザーによって)。ウィンドウのサイズが変更された場合、ボタンのサイズを自動的に変更する方法はありますか?

どうすればよいですか?

答えて

2

レイアウトを使用する必要があります。文書によると、

Qtのレイアウトシステムは、彼らが利用可能なスペースを有効に利用すること ことを保証するために、ウィジェット内 自動的に配置子ウィジェットのシンプルで強力な方法を提供します。

例:

main.cppに

#include "widget.h" 
#include <QApplication> 

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

    return a.exec(); 
} 

widget.h

#ifndef WIDGET_H 
#define WIDGET_H 

#include <QWidget> 

class Widget : public QWidget 
{ 
    Q_OBJECT 

public: 
    explicit Widget(QWidget *parent = 0); 
    ~Widget(); 

private: 

}; 

widget.cpp

#include "widget.h" 
#include <QPushButton> 
#include <QHBoxLayout> 

Widget::Widget(QWidget *parent) : 
    QWidget(parent) 
{ 
    QPushButton *button1 = new QPushButton("One"); 
    QPushButton *button2 = new QPushButton("Two"); 

    //Horizontal layout with two buttons 
    QHBoxLayout *hblayout = new QHBoxLayout; 
    hblayout->addWidget(button1); 
    hblayout->addWidget(button2); 

    QPushButton *button3 = new QPushButton("Three"); 
    QPushButton *button4 = new QPushButton("Four"); 

    //Vertical layout with two buttons 
    QVBoxLayout *vblayout = new QVBoxLayout; 
    vblayout->addWidget(button3); 
    vblayout->addWidget(button4); 

    //We add our two layouts (horizontal & vertical) to the 
    //following vertical layout. 
    QVBoxLayout *mainLayout = new QVBoxLayout; 
    mainLayout->addLayout(hblayout); 
    mainLayout->addLayout(vblayout); 

    this->setLayout(mainLayout); 
} 

Widget::~Widget() 
{ 

} 
関連する問題