2017-12-26 21 views
0

Qtを使用していますが、QMainWindowウィンドウを中央に配置する方法がわかりません。私はこのコードを書いたが、うまくいきません。前もって感謝します。QMainWindowを中央に配置するには?

QRect screenGeometry = QApplication::desktop()->screenGeometry(); 
int x = (screenGeometry.width() - w->width())/2; 
int y = (screenGeometry.height() - w->height())/2; 
w->move(x, y); // w is a QMainWindow pointer 

私はこの取得:

enter image description here

+1

それが何を意味する:*ではなく、作品を行いますか*。?あなたは、あなたが得るものと取得したいものの写真を示すことができます。 – eyllanesc

+1

解決策はここにあります:http://www.qtcentre.org/threads/3399-set-QMainWindow-in-the-enterfo-my-desktop – Asesh

+0

@eyllanescありがとう、私は答えを更新しました。 –

答えて

0

はみんなにありがとう。私はすでに、このコードを使用して私の問題を解決しました。

w->setFixedSize(400, 400); 
int width = w->frameGeometry().width(); 
int height = w->frameGeometry().height(); 
QDesktopWidget wid; 
int screenWidth = wid.screen()->width(); 
int screenHeight = wid.screen()->height(); 
w->setGeometry((screenWidth/2)-(width/2),(screenHeight/2)-(height/2),width,height); 
w->show(); 

私はこれを取得:

enter image description here

-2

size()方法からウィンドウサイズを取得します。

QDesktopWidget *desktop = QApplication::desktop(); 
int screenWidth, width; 
int screenHeight, height; 
int x, y; 
QSize windowSize; 
QRect rec = desktop->screenGeometry(); 
screenWidth = rec.width(); // get width of screen 
screenHeight = rec.height(); // get height of screen 

windowSize = size(); // size of our application window 
width = windowSize.width(); 
height = windowSize.height(); 

// little computations 
x = (screenWidth - width)/2; 
y = (screenHeight - height)/2; 

// move window to desired coordinates 
move (x, y); 

参考リンク:http://www.qtcentre.org/threads/3399-set-QMainWindow-in-the-center-of-my-desktop

+0

これは**正確に** OPが何をしているのかです。あなたが質問やコードを理解していないときには、質問に答えようとしないでください。 – IInspectable

関連する問題