私はQtでそのようなコードを持っています。Qtのウィンドウの中央にプログラムでQWidgetを設定するには?
wdgCenter = new QWidget(wdgMain);
wdgCenter->move(wdgCenter->parentWidget()->geometry().center());
wdgCenter->resize(QSize(170, 115));
welcomeLbl = new QLabel("Welcome to the Notes. Log in first.", wdgCenter);
welcomeLbl->setGeometry(QRect(QPoint(0, 0), QSize(175, 20)));
loginLbl = new QLabel("Login", wdgCenter);
loginLbl->setGeometry(QRect(QPoint(0, 30), QSize(50, 20)));
pswdLbl = new QLabel("Password", wdgCenter);
pswdLbl->setGeometry(QRect(QPoint(0, 60), QSize(50, 20)));
loginLine = new QLineEdit (wdgCenter);
loginLine->setGeometry(QRect(QPoint(60, 30), QSize(110, 20)));
passwordLine = new QLineEdit (wdgCenter);
passwordLine->setGeometry(QRect(QPoint(60, 60), QSize(110, 20)));
logInBtn = new QPushButton ("Log In", wdgCenter);
logInBtn->setGeometry(QRect(QPoint(0, 90), QSize(50, 25)));
createBtn = new QPushButton ("Create", wdgCenter);
createBtn->setGeometry(QRect(QPoint(60, 90), QSize(50, 25)));
この結果は、最初の画像で確認できます。
私は、ウィンドウの中央に私のwdgCenter
を移動したいです。私はこのようにQGridLayout
を試しました。
QGridLayout* gLayout = new QGridLayout(wdgMain);
gLayout->addWidget(welcomeLbl);
gLayout->setAlignment(welcomeLbl,Qt::AlignHCenter);
gLayout->addWidget(loginLbl, 1, 0 );
gLayout->setAlignment(loginLbl,Qt::AlignHCenter);
gLayout->addWidget(pswdLbl, 2, 0);
gLayout->setAlignment(pswdLbl,Qt::AlignHCenter);
gLayout->addWidget(loginLine, 1, 1);
gLayout->setAlignment(loginLine,Qt::AlignHCenter);
gLayout->addWidget(passwordLine, 2, 1);
gLayout->setAlignment(passwordLine,Qt::AlignHCenter);
gLayout->addWidget(logInBtn, 3, 0);
gLayout->setAlignment(logInBtn,Qt::AlignHCenter);
gLayout->addWidget(createBtn, 3, 1);
gLayout->setAlignment(createBtn,Qt::AlignHCenter);
結果は次の図のようになります。
私もQHBoxLayout
とQHBoxLayout
を試してみましたが、彼らは単に行のまたは1つの列の中で私の私のオブジェクトを描画します。 Qtのデザイナで
私はこのような垂直方向と水平方向のスペーサーを使ってこれをしなかった:
しかし、私はプログラム的にこれを行う必要があります。
どのように私のwdgCenter
を中心に設定しますか?
は、間隔を使用して、同じレイアウト構造を再構築します。または、サブウィジェットを作成し、そのサイズのヒントを最小または固定に設定し、それを任意のレイアウト(QVBoxLayout、QHBoxLayoutなど)に中央揃えで配置します。 – peppe
@peppe、私は2つのラベルの例を表示できますか? – user3360601