PDA

View Full Version : Center dialog on screen problems



lalesculiviu
1st November 2009, 09:53
Hello. I am using this code to center a dialog on the screen:



void centerWidgetOnScreen(QWidget* widget)
{
QRect rect = QApplication::desktop()->availableGeometry(widget);

widget->move(rect.center() - widget->rect().center());
}


(where widget is in fact a QDialog*).

But the dialog appears a bit to the right (maybe 10 pixels) and too low (maybe 30 pixels, considering also the task bar).

What am I doing wrong?

I also tried this:



QRect frect=widget->frameGeometry();
frect.moveCenter(QApplication::desktop()->availableGeometry(widget).center());
//frect.moveCenter(QDesktopWidget().availableGeometr y(widget).center());
widget->move(frect.topLeft());


But does not work under Linux. By the way, which should I use, QApplication::desktop() or QDesktopWidget()?

FinderCheng
1st November 2009, 14:15
If it is a subclass of QDialog, why not use the parent parameter of QDialog's constractor? If the parent is a QWidget, then this dialog will center at this parent widget, if is null, this dialog will be at the center of screen.