PDA

View Full Version : Place QMessageBox on middle of screen



lllturtle
23rd November 2011, 05:34
hello every one,
i'm meeting problem with QMessageBox ,
i can't locate the box window to the middle of the screen,
it's change position depend on the message box size,

below is my code , but it not work after i need to change language
till now, i'm doing the stupid thing is:
1.Run the program first time, get the real messagebox:

QMessageBox message(QMessageBox::Information, "",
QObject::tr("Test QMessageBox Size"),
QMessageBox::NoButton ,
this, Qt::FramelessWindowHint);

message.exec();

QString sss ;
sss.sprintf(" Test QMessageBox Size w=%d h=%d",message.width(),message.height());
qDebug() << sss ;

2.After i get the message output "sss"

"Test QMessageBox Size w=242 h=103"
, setFixedSize(242, 103) of the message box
and move the message on the middle of the screen before exec()

QMessageBox message(QMessageBox::Information, "",
QObject::tr("Test QMessageBox Size"),
QMessageBox::NoButton ,
this, Qt::FramelessWindowHint);

message.setFixedSize(242, 103);
message.move((window_w-message.width())/2,(window_h-message.height())/2);

message.exec();
which window_w and window_h are the window width, height.


it's worked well , but now i need to involve the multi-language using .qm file ,
after translate to other language, the box size changed by the text ,
(the length of text changed , and the box size changed)
therefore the messagebox not on the middle of screen again,
is there any way to let the messagebox always on the middle of screen no matter the MessageBox size?

thanks for your patience ,
any response is appreciate!

gouyoku
23rd November 2011, 10:19
Try setting the parent to 0 and not moving the window at all.


QMessageBox message(QMessageBox::Information, "",
QObject::tr("Test QMessageBox Size"),
QMessageBox::NoButton ,
0, Qt::FramelessWindowHint);

message.exec();

lllturtle
24th November 2011, 06:21
hi gouyoku,

thanks for your reply ,
but if the parent is 0, message box may show on random place? (can't control)
i found the article here (http://lists.trolltech.com/qt-interest/1999-10/msg00382.html)

and after looking for hours , there's an idea through my head ,

i'm corrected the code by adding the .show() function

QMessageBox message(QMessageBox::Information, "",
QObject::tr("Test QMessageBox Size"),
QMessageBox::NoButton ,
this, Qt::FramelessWindowHint);

// message.setFixedSize(242, 103);
message.show();
message.move((window_w-message.width())/2,(window_h-message.height())/2);

message.exec();


In this way , i can get the correct message box size
and dynamically move to the center of screen, no matter what language .
(there will be wrong size if your content(setText()) include both "\n" and another very long string(will automatically change line)

but i'm not sure is it a good way to use .show() and .exec() at the same time ????
can anyone answer me ?

gouyoku
24th November 2011, 08:01
It is not a random place. It is decided by the window manager and dialogs without parent windows tend to show up in the middle of the screen. If you have no faith in window managers you can do this:

QMessageBox message(QMessageBox::Information, "",
QObject::tr("Test QMessageBox Size"),
QMessageBox::NoButton ,
QDesktopWidget().screen(), Qt::FramelessWindowHint); // will show QMessageBox in the center of the default screen
message.exec();
Can't give you a definite answer about show() and exec() one after another but I wouldn't do that. It makes the program go through show() twice.

lllturtle
24th November 2011, 08:59
Thanks , I've try to set the parent (0 or QDesktopWidget().screen() )
but it's not in the middle of screen , i don't know why ,
it's change position depend on the content , anyway thank you~

Spitfire
24th November 2011, 10:11
Remember that if you won't parent the dialog it will have an extra icon on the task bar - sometimes that's desired, but most of the times - not.

as to the solution - the simplest is always the best:
QMessageBox m( QMessageBox::Information, "test", "Put here whatever you want.", QMessageBox::NoButton, this );

QSize mSize = m.sizeHint(); // here's what you want, not m.width()/height()
QRect screenRect = QDesktopWidget().screen()->rect();
m.move( QPoint( screenRect.width()/2 - mSize.width()/2,
screenRect.height()/2 - mSize.height()/2 ) );

m.exec();
You have to use sizeHint() to know the widgtet size if it hasn't been shown yet.

lllturtle
25th November 2011, 01:36
Thank you Spitfire ,
that's exactly what i want ,
i've try that and it works perfectly~;)