(C++) How to put a dialog on the screen's center?
February 24, 2017 · View on GitHub
(C++)
How to put a dialog on the screen's center?
#include <QDesktopWidget> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); //Put the dialog in the screen center const QRect screen = QApplication::desktop()->screenGeometry(); this->move( screen.center() - this->rect().center() ); }
Complete source code
Operating system: Ubuntu
IDE: Qt Creator 2.0.0
Project type: Qt4 GUI Application
Libraries used:
- Qt: version 4.7.0 (32 bit)
main.cpp
Not a single line is changed from the default created main.
#include <QtGui/QApplication> #include "dialog.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }
dialog.h
Not a single line is changed from the default created dialog.h.
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); protected: void changeEvent(QEvent *e); private: Ui::Dialog *ui; }; #endif // DIALOG_H
dialog.cpp
The only file where changed are added.
#include <QDesktopWidget> #include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); //Move the dialog away from the center this->setGeometry(0,0,this->width(),this->height()); //Put the dialog in the screen center const QRect screen = QApplication::desktop()->screenGeometry(); this->move( screen.center() - this->rect().center() ); } Dialog::~Dialog() { delete ui; } void Dialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } }