Place QMessageBox on middle of screen
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:
Code:
QObject::tr("Test QMessageBox Size"),
this, Qt::FramelessWindowHint);
message.exec();
sss.sprintf(" Test QMessageBox Size w=%d h=%d",message.width(),message.height());
qDebug() << sss ;
2.After i get the message output "sss"
Quote:
"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()
Code:
QObject::tr("Test QMessageBox Size"),
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!
Re: Place QMessageBox on middle of screen
Try setting the parent to 0 and not moving the window at all.
Code:
QObject::tr("Test QMessageBox Size"),
0, Qt::FramelessWindowHint);
message.exec();
Re: Place QMessageBox on middle of screen
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
and after looking for hours , there's an idea through my head ,
i'm corrected the code by adding the .show() function
Code:
QObject::tr("Test QMessageBox Size"),
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 ?
Re: Place QMessageBox on middle of screen
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:
Code:
QObject::tr("Test QMessageBox Size"),
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.
Re: Place QMessageBox on middle of screen
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~
Re: Place QMessageBox on middle of screen
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:
Code:
QSize mSize
= m.
sizeHint();
// here's what you want, not m.width()/height() 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.
Re: Place QMessageBox on middle of screen
Thank you Spitfire ,
that's exactly what i want ,
i've try that and it works perfectly~;)