QSettings保存程序设置
今天看了一些QSettings的简单用法,可以用来保存程序的设置,使得程序每次启动都可以显示上次关闭时的状态。我这里实现了一个简单的文本编辑窗口,可以设置文本的字体,字体的颜色和背景色。每次关闭程序都保存程序的几何大小,位置和文本以及文本所设置的格式,方便启动程序后继续编辑。
文本编辑窗口
- class TextEditor:public QTextEdit
- {
- Q_OBJECT
- public:
- TextEditor(QWidget *parent = NULL);
- ~TextEditor();
- void SaveSettings();
- protected:
- void ReadSettings();
- void contextMenuEvent ( QContextMenuEvent * event );
- private slots:
- void SettingBackColorSlot();
- void SettingTextColorSlot();
- void SettingTextFontSlot();
- };
- TextEditor::TextEditor( QWidget *parent /*= NULL*/ ):QTextEdit(parent)
- {
- ReadSettings();
- }
- TextEditor::~TextEditor()
- {
- }
- void TextEditor::contextMenuEvent( QContextMenuEvent * event )
- {
- QMenu *pMenu = createStandardContextMenu();
- pMenu->addSeparator();
- QTextCursor cursor = this->textCursor();
- QString seletedText = cursor.selectedText();
- if (!seletedText.isEmpty()) //选中文本才可以设置文本样式
- {
- QMenu *pSubMenu = new QMenu(tr("设置"),pMenu);
- pMenu->addMenu(pSubMenu);
- QAction *pFontAct = new QAction(tr("字体"),pSubMenu);
- QAction *pTextColorAct = new QAction(tr("字体颜色"),pSubMenu);
- QAction *pBackColorAct = new QAction(tr("背景色"),pSubMenu);
- pSubMenu->addAction(pFontAct);
- pSubMenu->addAction(pTextColorAct);
- pSubMenu->addAction(pBackColorAct);
- connect(pFontAct,SIGNAL(triggered ()),this,SLOT(SettingTextFontSlot()));
- connect(pTextColorAct,SIGNAL(triggered ()),this,SLOT(SettingTextColorSlot()));
- connect(pBackColorAct,SIGNAL(triggered ()),this,SLOT(SettingBackColorSlot()));
- }
- pMenu->exec(event->globalPos());
- delete pMenu;
- }
- //设置文本背景色
- void TextEditor::SettingBackColorSlot()
- {
- QColor color = QColorDialog::getColor(Qt::white, this, "Select Color", QColorDialog::DontUseNativeDialog);
- if(color.isValid())
- {
- this->setTextBackgroundColor(color);
- }
- }
- //设置文本颜色
- void TextEditor::SettingTextColorSlot()
- {
- QColor color = QColorDialog::getColor(Qt::black, this, "Select Color", QColorDialog::DontUseNativeDialog);
- if(color.isValid())
- {
- this->setTextColor(color);
- }
- }
- //设置文本字体
- void TextEditor::SettingTextFontSlot()
- {
- bool ok;
- QFont font = QFontDialog::getFont(&ok, this);
- if (ok)
- {
- QTextCursor cur = this->textCursor();
- QString sltStr = cur.selectedText();
- this->cut();
- QTextCharFormat fmtText;
- fmtText.setFont(font);
- cur.insertText(sltStr,fmtText);
- }
- }
- //退出前保存文本信息
- void TextEditor::SaveSettings()
- {
- QSettings TextSettings("Mysoft","TextData");
- QString html = this->toHtml();
- TextSettings.setValue("text",html);
- }
- //启动时读取信息
- void TextEditor::ReadSettings()
- {
- QSettings TextSettings("Mysoft","TextData");
- QString html = TextSettings.value("text").toString();
- this->setHtml(html);
- }
程序主窗口
- class TextEdit : public QMainWindow
- {
- Q_OBJECT
- public:
- TextEdit(QWidget *parent = 0, Qt::WFlags flags = 0);
- ~TextEdit();
- protected:
- void closeEvent ( QCloseEvent * event ) ;
- void ReadSettings();
- private:
- TextEditor *m_pCentralWidget;
- };
- TextEdit::TextEdit(QWidget *parent, Qt::WFlags flags)
- : QMainWindow(parent, flags)
- {
- m_pCentralWidget = new TextEditor(this);
- this->setCentralWidget(m_pCentralWidget);
- ReadSettings();
- }
- TextEdit::~TextEdit()
- {
- }
- void TextEdit::closeEvent( QCloseEvent * event )
- {
- QSettings dialogSettings("Mysoft","dialogData"); //保存窗口位置和大小
- dialogSettings.setValue("Rect",this->rect());
- QPoint pos = this->pos();
- dialogSettings.setValue("positionX",this->pos().x());
- dialogSettings.setValue("positionY",this->pos().y());
- m_pCentralWidget->SaveSettings();
- }
- void TextEdit::ReadSettings()
- {
- QSettings dialogSettings("Mysoft","dialogData"); //读取窗口位置和大小
- dialogSettings.setValue("Rect",this->rect());
- dialogSettings.setValue("position",this->pos());
- QRect rect = dialogSettings.value("Rect").value<QRect>();
- this->setGeometry(rect);
- int posX = dialogSettings.value("positionX").toInt();
- int posY = dialogSettings.value("positionY").toInt();
- this->move(QPoint(posX,posY));
- }
http://blog.csdn.net/hai200501019/article/details/11179967
QSettings保存程序设置的更多相关文章
- C# 对象实例化 用json保存 泛型类 可以很方便的保存程序设置
参考页面: http://www.yuanjiaocheng.net/webapi/test-webapi.html http://www.yuanjiaocheng.net/webapi/web-a ...
- C# 对象实例化 用json保存 泛型类 可以很方便的保存程序设置
用于永久化对象,什么程序都行,依赖NewtonSoft.用于json序列化和反序列化. using Newtonsoft.Json; using System; using System.Collec ...
- Android下使用Properties文件保存程序设置
原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码. 读取.properties文件中的配置: String strValue ...
- 【转】Android下使用Properties文件保存程序设置
原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码. 读取.properties文件中的配置: String strValue ...
- 程序启动读取和关闭时保存应用程序设置(QSettings)
保存应用程序设置(QSettings)1. QSettings 类 QSettings 提供保存应用程序当前设置的接口,可以方便地保存程序的状态,例如窗口大小和位置,选项的选中状态等等.在 Windo ...
- PDF 补丁丁 0.6.0.3363 版发布(修复无法保存应用程序设置的问题)
本测试版修复了上一测试版无法保存应用程序设置的问题,以及导出导入信息文件的若干小问题.
- win7无法保存打印机设置(错误0x000006d9)解决方法
解决win7打印机共享出现‘无法保存打印机设置’操作无法完成(错误0x000006d9),接下来与大家分享下解决方法, 找到windows firewall服务,启用即可 ============== ...
- linux系统学习笔记:无死角理解保存的设置用户ID,设置用户ID位,有效用户ID,实际用户ID
一.基本概念 实际用户ID(RUID):用于标识一个系统中用户是谁,一般是在登录之后,就被唯一的确定,就是登录的用户的uid. 有效用户ID(EUID):用于系统决定用户对系统资源的权限,也就是说当用 ...
- 【.net 深呼吸】自己动手来写应用程序设置类
在开始装逼之前,老周先说明一件事.有人说老周写的东西太简单了,能不能写点复杂点.这问题就来了,要写什么东西才叫“复杂”?最重要的是,写得太复杂了,一方面很多朋友看不懂,另一方面,连老周自己也不知道怎么 ...
随机推荐
- SQL执行效率总结
1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况 p4: 2.4 内存: 1 G os: windows 2003 数据库: ms sql server 2000 目的: 查询 ...
- Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型
Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型 在这一节中,你将添加用于管理数据库中电影的类.这些类是ASP.NET MVC应用程序的模型部分. 你将使用.NET Framewo ...
- SSIS 连接 PostgreSQL
因为工作需要,得把psql的表放到SQL Server, 找到一个PGNP(http://www.pgoledb.com/) 的适配器,不过一看要300$就没有去尝试了. 官方倒是有ODBC的驱动. ...
- 初始Android-配置环境
最近闲来无事自学了一下Android,今天没事想整理一下思绪,简单的介绍一下我自己对环境配置的认识,仅供参考,欢迎提出意见. 1.首先打开Eclipse,然后安装ADT,准备好ADTjar包或者zip ...
- 详解虚拟机(windows)下搭建SVN服务器
安装前的准备 1.虚拟机的用户名最好是英文 2.严格按照步骤做,否则有可能不成功 3.如果安装失败,在虚拟机下的控制板完全下载VisualSVN-Server-2.7.7,重新安装 软件下载地址: h ...
- IOS开发:UIAlertView使用
链接地址:http://www.2cto.com/kf/201307/231841.html UIAlertView是什么就不介绍了 1.基本用法 1 UIAlertView *view = [[UI ...
- Android Studio does not point to a valid jvm
环境变量 JAVA_HOME的值,去掉后面的分号,一般情况下就可以启动
- sorl6.0+jetty+mysql
sorl6.0+jetty+mysql搭建solr服务 1.下载solr 官网:http://lucene.apache.org/solr/ v2.目录结构如下 v3.启动solr(默认使用jetty ...
- 设计模式总结4--singleton pattern
单例模式 保证每个类只有一个实例,并提供一个全局访问点 第一步 构造方法私有化第二步 公有化静态方法获取的实例 懒汉式 public class Bank{ private Bank(){} pri ...
- unity3d大型手游 可以打包obb文件
用unity3d开发手游,有个很大的问题就是apk的size太大, 如果超过50M,一般很多平台就不会肯上线. 一个好的方法是把app打成apk + obb数据包的方式. 1. 编译成obb数据包的方 ...