2016-08-13 3 views
0

私は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))); 

この結果は、最初の画像で確認できます。

default picture

私は、ウィンドウの中央に私の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); 

結果は次の図のようになります。

enter image description here

私もQHBoxLayoutQHBoxLayoutを試してみましたが、彼らは単に行のまたは1つの列の中で私の私のオブジェクトを描画します。 Qtのデザイナで

私はこのような垂直方向と水平方向のスペーサーを使ってこれをしなかった:

enter image description here

しかし、私はプログラム的にこれを行う必要があります。

どのように私のwdgCenterを中心に設定しますか?

+0

は、間隔を使用して、同じレイアウト構造を再構築します。または、サブウィジェットを作成し、そのサイズのヒントを最小または固定に設定し、それを任意のレイアウト(QVBoxLayout、QHBoxLayoutなど)に中央揃えで配置します。 – peppe

+0

@peppe、私は2つのラベルの例を表示できますか? – user3360601

答えて

1

簡単な方法は、Qt Designerを使用してUIを設計し、ソースコードを生成させることです。 UIの自動生成ソースコードは、のようにui_<your class name>.hという.hというファイルにあります。このファイルは<your class name>.cppファイルに自動的に含まれているため、そこに見つけることができます。

のQtデザイナーの概要:

enter image description here

結果:

enter image description here

+0

よろしくお願いいたします。私は私のものを見るでしょう。 – user3360601

関連する問題