void MoveToCenter(QMainWindow& w) { // move window to center. if (QApplication::desktop()->screenCount() <= 1) { w.move((QApplication::desktop()->width() - w.width())/2, (QApplication::desktop()->height() - w.height())/2); } else { QRect rect = QApplication::desktop()->screenGeometry(0); w.move((rect.width()-w.width())/2, (rect.height()-w.height())/2); } }
void EditDialog::showEvent(QShowEvent*) { // Whenever the dialog is shown, position it at the center of the parent dialog QMainWindow* parentDialog = qobject_cast<QMainWindow*>(parent()); if(parentDialog) { move(parentDialog->x() + parentDialog->width() / 2 - width() / 2, parentDialog->y() + parentDialog->height() / 2 - height() / 2); } }
int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow mainWindow; auto lineEdit = new QLineEdit("testing"); auto centralWidget = new QWidget(); auto layout = new QVBoxLayout(); centralWidget->setLayout(layout); mainWindow.setCentralWidget(centralWidget); auto textWidget = new QTextEdit("testing top level editor"); textWidget->setReadOnly(true); textWidget->setVisible(false); layout->addWidget(lineEdit); layout->addWidget(textWidget); TopLevelTextEditorModifier modifier; auto emojiIcon = IconFactory::createChatEmojiIcon(Qt::red); auto action = lineEdit->addAction(emojiIcon, QLineEdit::LeadingPosition); QObject::connect(action, &QAction::triggered, [&](){ textWidget->setVisible(!textWidget->isVisible()); mainWindow.resize(textWidget->isVisible() ? 400 : 300, mainWindow.height()); }); modifier.modify(lineEdit, true); mainWindow.show(); return app.exec(); }