将Qt应用程序的状态保存到文件 [英] Saving the state of a Qt application to a file

查看:453
本文介绍了将Qt应用程序的状态保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有些是新手,我无法保存和加载工作. 该程序有一些单选按钮和旋转框,我希望能够将这些位置和值保存到文件中,并在以后再次将其打开. 这是节省的地方:

I am somewhat of a newbie and I can't get the saving and loading to work. The program has some radio buttons and spinboxes, I want to be able to save those positions and values to a file, and be able to open it again later. Here is the saving:

void MainWindow::on_saveButton_clicked()
{
 QString fileName = QFileDialog::getSaveFileName(this,
     tr("Salvesta Projekt"), "",
     tr("Latid Pindalaks (*.lp);;All Files (*)"));

 if (fileName.isEmpty())
     return;
 else {
     QFile file(fileName);
     if (!file.open(QIODevice::WriteOnly)) {
         QMessageBox::information(this, tr("Unable to open file"),
             file.errorString());
         return;
     }
     QDataStream out(&file);
     out.setVersion(QDataStream::Qt_4_8);

     QByteArray MyArray = MainWindow::saveState();

     out << (MyArray);
      }
 }

这是加载中

void MainWindow::on_loadButton_clicked()
{
 QString fileName = QFileDialog::getOpenFileName(this,
     tr("Ava Projekt"), "",
     tr("Latid Pindalaks (*.lp);;All Files (*)"));

 if (fileName.isEmpty())
     return;
 else {

     QFile file(fileName);

     if (!file.open(QIODevice::ReadOnly)) {
         QMessageBox::information(this, tr("Pole võimalik faili laadida"),
             file.errorString());
         return;
     }

     QDataStream in(&file);
     in.setVersion(QDataStream::Qt_4_8);

     in >> (MyArray);

     MainWindow::restoreState(MyArray);
 }
}

我知道我做错了什么,所以很好的例子将不胜感激.

I know i'm doing something very wrong, so a good example would be very appreciated.

推荐答案

QMainWindow 文档指出:

保存此主窗口的工具栏 dockwidgets 的当前状态.

这意味着您必须自己保存其他窗口小部件(单选按钮等)的状态.

This means that you must save and the state of the other widgets (radio buttons and whatnot) yourself.

这篇关于将Qt应用程序的状态保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆