在我的应用程序中,我正在使用C ++和QT进行开发,我正在尝试使用用户导入的图像和小部件来保存GUI的状态,以便当用户退出并且应用程序再次启动时,它会“确切地说,用户离开时图像和小部件完全存储在用户放置的相同位置。
图像存储在QLabels中。我一直试图使用我已经看过的QSettings示例代码,但它根本不工作。有关如何保存整个GUI状态的任何想法,用户可以直接导入小部件和图像。干杯:)
这是我迄今为止开发的源代码
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// Load state of GUI
read_settings();
ui->setupUi(this);
// Set up the window size
this->setWindowTitle(QString::fromUtf8("Raspberry PI GUI v1.0"));
this->resize(800, 400);
//-------------------------------------------
// Setting up buttons on the main screen
//-------------------------------------------
// Add label
button = new QPushButton("Add Graphic", this);
button->setGeometry(QRect(QPoint(10, 20), QSize(200, 50)));
button->show();
// Add LED
button = new QPushButton("Add LCD", this);
button->setGeometry(QRect(QPoint(10, 80), QSize(200, 50)));
button->show();
QObject::connect(button, &QPushButton::clicked, this, &MainWindow::input_led);
// Add Next Window
button = new QPushButton("New Window", this);
button->setGeometry(QRect(QPoint(10, 140), QSize(200, 50)));
button->show();
//QObject::connect(button, SIGNAL(pressed()), this, SLOT(input_newwindow()));
QObject::connect(button, &QPushButton::clicked, this, &MainWindow::input_newwindow);
// Add plot
button = new QPushButton("Add Plot", this);
button->setGeometry(QRect(QPoint(10, 200), QSize(200, 50)));
button->show();
QObject::connect(button, &QPushButton::clicked, this, &MainWindow::input_plot);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::close_event(QCloseEvent *event)
{
write_settings();
event->accept();
}
void MainWindow::write_settings()
{
QSettings settings("reaffer Soft", "reafferApp");
settings.beginGroup("MainWindow");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.endGroup();
}
void MainWindow::read_settings()
{
QSettings settings("reaffer Soft", "reafferApp");
settings.beginGroup("MainWindow");
resize(settings.value("size", QSize(800, 400)).toSize());
move(settings.value("pos", QPoint(200, 200)).toPoint());
settings.endGroup();
}
void MainWindow::input_label()
{
Label *label = new Label(this);
label->setText("New Graphic");
label->show();
}
void MainWindow::input_led()
{
LED *led = new LED(this);
led->show();
}
void MainWindow::input_newwindow()
{
this->hide();
QMainWindow *newwindow = new QMainWindow();
newwindow->resize(800, 400);
newwindow->show();
// Need to set up and get working correctly
// Back button
QPushButton *back_button = new QPushButton(newwindow);
back_button->setText("Back");
back_button->setGeometry(QRect(QPoint(10, 80), QSize(200, 50)));
back_button->show();
QObject::connect(back_button, &QPushButton::pressed, newwindow, &QMainWindow::hide);
// Need to go back to previous screen
this->show();
// Forward button
QPushButton *forward_button = new QPushButton(newwindow);
forward_button->setText("Next");
forward_button->setGeometry(QRect(QPoint(50, 140), QSize(200, 50)));
forward_button->show();
// Create a new screen
QObject::connect(forward_button, &QPushButton::pressed, this, &QMainWindow::show);
this->hide();
}
void MainWindow::input_plot()
{
QMainWindow *windowplot = new QMainWindow();
windowplot->resize(800, 400);
windowplot->show();
}
void MainWindow::ButtonClicked()
{
}
答案 0 :(得分:0)
您在哪里实际保存自定义小部件的状态?我只看到你保存并加载主窗口本身的位置和大小,而不是它的孩子!?顺便说一句,您可以使用QWidget::saveGeometry()保存基本位置和尺寸,并使用QWidget::restoreGeometry加载工具栏的状态,并将QMainWindow
与QMainWindow::saveState的停靠窗口小部件一起加载,并将其加载{分别为{3}}。
如果您想保存自定义标签的位置和图片路径,请使用类似的方法:
QSettings settings("MyCompany", "MyApp");
settings.beginGroup("label1"); // your widget id
settings.setValue("geometry", label1->saveGeometry());
settings.setValue("imagePath", label1->text()); // or wherever you save the image
settings.endGroup();
这至少是关于如何解决问题的简单想法,更多细节需要提供更多信息。
答案 1 :(得分:0)
好了,现在我知道我只是试图加载主窗口本身的位置和大小,而不是保存它的孩子的位置。
我仍然坚持使用读/写QSettings代码保存保存自定义标签的位置和图像路径的位置。我想我的写设置代码是正确的,但不是读取设置。
void MainWindow::write_settings()
{
QSettings settings("reaffer Soft", "reafferApp");
settings.beginGroup("MainWindow");
settings.setValue("size", size());
settings.setValue("pos", pos());
settings.setValue("label", label->saveGeometry()); // Save label size
settings.setValue("imagePath" label->size()); // Save the image loaded
settings.endGroup();
qDebug() << "Write Settings";
}
为了将图像导入标签,我使用的是另一个cpp文件。该代码如下所示。
<强> Label.cpp 强>
#include "label.h"
//---------------------------------------
// Deconstructor
//---------------------------------------
Label::~Label()
{
}
void Label::mousePressEvent(QMouseEvent *event)
{
// Move the coordinates on the main window
m_nMouseClick_X_Coordinate = event->x();
m_nMouseClick_Y_Coordinate = event->y();
}
void Label::mouseMoveEvent(QMouseEvent *event)
{
//-------------------------------------------------------------
// Allow the user to drag the graphics on the Display
//-------------------------------------------------------------
move(event->globalX()-m_nMouseClick_X_Coordinate-m_pParentWidget->geometry().x(),
event->globalY()-m_nMouseClick_Y_Coordinate-m_pParentWidget->geometry().y());
}
void Label::mouseDoubleClickEvent(QMouseEvent *event)
{
//--------------------------------
// Open file dialog
//--------------------------------
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images(*.png, *.dxf, *.jpg"));
dialog.setViewMode(QFileDialog::Detail);
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Images"),
"/home",
tr("Image Files (*.png *.jpg *.bmp)"));
if (!fileName.isEmpty())
{
QImage image(fileName);
Label::setPixmap(fileName);
Label::adjustSize();
}
}