View Full Version : set QMainWindow in the center of my desktop
raphaelf
23rd August 2006, 14:13
Hi everybody,
QT: 4.1.3
OS: WinXP PRO
Compiler: MINGW
I am trying to set my app in the midle of my desktop. How can i do tha?Should i use setCentralWidget??
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setStyle("plastique");
app.setPalette( app.style()->standardPalette() );
MainWindow m;
m.setMinimumSize(451,600);
m.setMaximumSize(451,600);
// m.setCentralWidget();
m.show();
return app.exec();
}
jpn
23rd August 2006, 14:17
QDesktopWidget provides access to screen geometries.
sector
23rd August 2006, 16:05
This gives you what you want (copy to constructor of your "MainWindow"):
QDesktopWidget *desktop = QApplication::desktop();
int screenWidth, width;
int screenHeight, height;
int x, y;
QSize windowSize;
screenWidth = desktop->width(); // get width of screen
screenHeight = desktop->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;
y -= 50;
// move window to desired coordinates
move ( x, y );
jpn
23rd August 2006, 16:41
QRect r = widget->geometry();
r.moveCenter(QApplication::desktop()->availableGeometry().center());
widget->setGeometry(r);
Be aware that constructor of a widget may not always be a reliable place to ask widget for it's geometries...
ball
24th August 2006, 02:53
In the QWidget subclass object construtor, add this line:
this->move(QApplication::desktop()->screen()->rect().center()-this->rect().center());
raphaelf
24th August 2006, 12:23
Hi everybody!!
Now i have a lot of solutions :p
It works :p
Thanks to all
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setStyle("plastique");
app.setPalette( app.style()->standardPalette() );
MainWindow m;
m.setMinimumSize(451,600);
m.setMaximumSize(451,600);
QRect r = m.geometry();
r.moveCenter(QApplication::desktop()->availableGeometry().center());
m.setGeometry(r);
m.show();
return app.exec();
}
8Observer8
24th July 2020, 20:29
These functions are obsolete:
QApplication::desktop()->screenGeometry()
QApplication::desktop()->availableGeometry()
QDesktopWidget::screen()
Use QScreen instead:
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QScreen>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
move(QGuiApplication::screens().at(0)->geometry().center() - frameGeometry().center());
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.