Qt:C ++:恢复弹出对话框的几何 [英] Qt: C++: Restoring geometry of a pop-up dialog-box
问题描述
我试图保存弹出对话框的几何图形,然后在每次调用弹出对话框时(应用程序仍在运行时)将其还原.
I am trying to save the geometry of a pop-up dialog-box and then restore it back whenever I call the pop-up dialog box (while the application is still running).
但是我无法弄清楚.
代码运行无错误.但是,每次打开时,弹出窗口的位置都会保持垂直变化.除非我关闭整个应用程序,然后再次将其重新打开,否则弹出窗口将永远不会回到其在屏幕中央的原始位置.
The code runs without error. But the window of the pop-up keeps changing it position vertically everytime it is open. Unless I close the whole application and then re-opened it again, the pop-up never goes to its original position in the center of the screen.
我正在尝试使用QcloseEvent
,QSettings
和restoreGeometry
.但是有什么不对的,请帮忙.
I am trying to use QcloseEvent
, QSettings
and restoreGeometry
. But something is not right, please help.
这里是 MainWindow.cpp :
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_addmembersdialog.h"
#include "addmembersdialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mpAddMembersDialog = new AddMembersDialog;
connect(ui->testBtn,SIGNAL(clicked()),this,SLOT(openPopUpForm()));
}
void MainWindow::openPopUpForm(){
mpAddMembersDialog->readSettings();
mpAddMembersDialog->exec();
}
这里是 AddMembersDialog.cpp :
#include "addmembersdialog.h"
#include "ui_addmembersdialog.h"
#include <QMessageBox>
AddMembersDialog::AddMembersDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddMembersDialog)
{
ui->setupUi(this);
connect(ui->closeFormBtn,SIGNAL(clicked()),this,SLOT(Exit()));
}
void AddMembersDialog::Exit()
{
close();
}
void AddMembersDialog::closeEvent(QCloseEvent *event){
QSettings settings("DevSuda", "Muwassa");
settings.setValue("geometery", saveGeometry());
QDialog::closeEvent(event);
}
void AddMembersDialog::readSettings(){
QSettings settings("DevSuda", "Muwassa");
restoreGeometry(settings.value("QDialog/geometry").toByteArray());
}
推荐答案
请比较以下两行使用的密钥:
Please compare the key used for the following two lines:
settings.setValue("geometery", saveGeometry());
和
restoreGeometry(settings.value("QDialog/geometry").toByteArray());
"geometry"
与"QDialog/geometry"
.应该一样!
我还要在cpp文件中预定义您的密钥/组织/应用程序名称,例如:
also I would predefine your keys/organization/application name in the cpp file such as:
...
static const char * ksOrganization{"DevSuda"};
static const char * ksApp {"Muwassa"};
static const char * ksKey {"geometery"};
...
QSettings settings(ksOrganization, ksApp);
settings.setValue(ksKey, saveGeometry());
这将防止您在一个地方键入错误的密码...
This would prevent you from typing the key wrong in one place...
这篇关于Qt:C ++:恢复弹出对话框的几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!