スポンサーリンク

Qt: ボタンを横並びに配置する

当サイトは、アフィリエイト広告を利用しています

環境:QT5.5

画面を右と左にわけて、画面右に横並びに3つのボタンを配置する。

ボタン1は左側に、ボタン2と3は右側によせています。

リンク
https://doc.qt.io/qt-5/search-results.html?q=QHBoxLayout
https://doc.qt.io/qt-5/search-results.html?q=QVBoxLayout
http://doc.qt.io/qt-5/qpushbutton.html

インクルードファイル

#include 
#include 
#include 

Header File


private:
    Ui::ClassA *ui;

    // Window Object
    QHBoxLayout *mainLayout;
    QVBoxLayout *leftLayout;
    QVBoxLayout *rightLayaut;
    QPushButton *button1;
    QPushButton *button2;
    QPushButton *button3;
//===========================================================
//	コンストラクタ
//===========================================================
ClassA::ClassA(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ClassA)
{
    ui->setupUi(this);

    // ボタン
    button1 = new QPushButton;
    button1->setText(tr("ボタン1"));
    button1->setEnabled(false);
    button2 = new QPushButton;
    button2->setText(tr("ボタン2"));
    button2->setEnabled(false);
    button3 = new QPushButton;
    button3->setText(tr("ボタン3"));

    // レイアウト
    rightBottom->addWidget(button1);
    rightBottom->addStretch();
    rightBottom->addWidget(button2);
    rightBottom->addSpacing(20);
    rightBottom->addWidget(button3);
    rightLayaut->addLayout(rightTop);
    rightLayaut->addLayout(rightBottom);

    // 画面全体
    mainLayout = new QHBoxLayout;
    leftLayout = new QVBoxLayout;
    rightLayaut = new QVBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayaut);
    ui->centralwidget->setLayout(mainLayout);


コメント

タイトルとURLをコピーしました