今天看了一些QSettings的简单用法,可以用来保存程序的设置,使得程序每次启动都可以显示上次关闭时的状态。我这里实现了一个简单的文本编辑窗口,可以设置文本的字体,字体的颜色和背景色。每次关闭程序都保存程序的几何大小,位置和文本以及文本所设置的格式,方便启动程序后继续编辑。

文本编辑窗口

TextEditor继承了QTextEdit,主要实现文本编辑和文本格式设置。保存文本信息时直接用的html字符串形式保存,可以同时保存文本的格式。
  1. class TextEditor:public QTextEdit
  2. {
  3. Q_OBJECT
  4. public:
  5. TextEditor(QWidget *parent = NULL);
  6. ~TextEditor();
  7. void SaveSettings();
  8. protected:
  9. void ReadSettings();
  10. void contextMenuEvent ( QContextMenuEvent * event );
  11. private slots:
  12. void SettingBackColorSlot();
  13. void SettingTextColorSlot();
  14. void SettingTextFontSlot();
  15. };
  1. TextEditor::TextEditor( QWidget *parent /*= NULL*/ ):QTextEdit(parent)
  2. {
  3. ReadSettings();
  4. }
  5. TextEditor::~TextEditor()
  6. {
  7. }
  8. void TextEditor::contextMenuEvent( QContextMenuEvent * event )
  9. {
  10. QMenu *pMenu = createStandardContextMenu();
  11. pMenu->addSeparator();
  12. QTextCursor cursor = this->textCursor();
  13. QString seletedText = cursor.selectedText();
  14. if (!seletedText.isEmpty())     //选中文本才可以设置文本样式
  15. {
  16. QMenu *pSubMenu = new QMenu(tr("设置"),pMenu);
  17. pMenu->addMenu(pSubMenu);
  18. QAction *pFontAct = new QAction(tr("字体"),pSubMenu);
  19. QAction *pTextColorAct = new QAction(tr("字体颜色"),pSubMenu);
  20. QAction *pBackColorAct = new QAction(tr("背景色"),pSubMenu);
  21. pSubMenu->addAction(pFontAct);
  22. pSubMenu->addAction(pTextColorAct);
  23. pSubMenu->addAction(pBackColorAct);
  24. connect(pFontAct,SIGNAL(triggered ()),this,SLOT(SettingTextFontSlot()));
  25. connect(pTextColorAct,SIGNAL(triggered ()),this,SLOT(SettingTextColorSlot()));
  26. connect(pBackColorAct,SIGNAL(triggered ()),this,SLOT(SettingBackColorSlot()));
  27. }
  28. pMenu->exec(event->globalPos());
  29. delete pMenu;
  30. }
  1. //设置文本背景色
  2. void TextEditor::SettingBackColorSlot()
  3. {
  4. QColor color = QColorDialog::getColor(Qt::white, this, "Select Color", QColorDialog::DontUseNativeDialog);
  5. if(color.isValid())
  6. {
  7. this->setTextBackgroundColor(color);
  8. }
  9. }
  10. //设置文本颜色
  11. void TextEditor::SettingTextColorSlot()
  12. {
  13. QColor color = QColorDialog::getColor(Qt::black, this, "Select Color", QColorDialog::DontUseNativeDialog);
  14. if(color.isValid())
  15. {
  16. this->setTextColor(color);
  17. }
  18. }
  19. //设置文本字体
  20. void TextEditor::SettingTextFontSlot()
  21. {
  22. bool ok;
  23. QFont font = QFontDialog::getFont(&ok, this);
  24. if (ok)
  25. {
  26. QTextCursor cur = this->textCursor();
  27. QString sltStr = cur.selectedText();
  28. this->cut();
  29. QTextCharFormat fmtText;
  30. fmtText.setFont(font);
  31. cur.insertText(sltStr,fmtText);
  32. }
  33. }
  34. //退出前保存文本信息
  35. void TextEditor::SaveSettings()
  36. {
  37. QSettings TextSettings("Mysoft","TextData");
  38. QString html = this->toHtml();
  39. TextSettings.setValue("text",html);
  40. }
  41. //启动时读取信息
  42. void TextEditor::ReadSettings()
  43. {
  44. QSettings TextSettings("Mysoft","TextData");
  45. QString html = TextSettings.value("text").toString();
  46. this->setHtml(html);
  47. }
 
 

程序主窗口

  1. class TextEdit : public QMainWindow
  2. {
  3. Q_OBJECT
  4. public:
  5. TextEdit(QWidget *parent = 0, Qt::WFlags flags = 0);
  6. ~TextEdit();
  7. protected:
  8. void closeEvent ( QCloseEvent * event ) ;
  9. void ReadSettings();
  10. private:
  11. TextEditor   *m_pCentralWidget;
  12. };
  1. TextEdit::TextEdit(QWidget *parent, Qt::WFlags flags)
  2. : QMainWindow(parent, flags)
  3. {
  4. m_pCentralWidget = new TextEditor(this);
  5. this->setCentralWidget(m_pCentralWidget);
  6. ReadSettings();
  7. }
  8. TextEdit::~TextEdit()
  9. {
  10. }
  11. void TextEdit::closeEvent( QCloseEvent * event )
  12. {
  13. QSettings dialogSettings("Mysoft","dialogData");   //保存窗口位置和大小
  14. dialogSettings.setValue("Rect",this->rect());
  15. QPoint pos = this->pos();
  16. dialogSettings.setValue("positionX",this->pos().x());
  17. dialogSettings.setValue("positionY",this->pos().y());
  18. m_pCentralWidget->SaveSettings();
  19. }
  20. void TextEdit::ReadSettings()
  21. {
  22. QSettings dialogSettings("Mysoft","dialogData");  //读取窗口位置和大小
  23. dialogSettings.setValue("Rect",this->rect());
  24. dialogSettings.setValue("position",this->pos());
  25. QRect rect = dialogSettings.value("Rect").value<QRect>();
  26. this->setGeometry(rect);
  27. int posX = dialogSettings.value("positionX").toInt();
  28. int posY = dialogSettings.value("positionY").toInt();
  29. this->move(QPoint(posX,posY));
  30. }

http://blog.csdn.net/hai200501019/article/details/11179967

QSettings保存程序设置的更多相关文章

  1. C# 对象实例化 用json保存 泛型类 可以很方便的保存程序设置

    参考页面: http://www.yuanjiaocheng.net/webapi/test-webapi.html http://www.yuanjiaocheng.net/webapi/web-a ...

  2. C# 对象实例化 用json保存 泛型类 可以很方便的保存程序设置

    用于永久化对象,什么程序都行,依赖NewtonSoft.用于json序列化和反序列化. using Newtonsoft.Json; using System; using System.Collec ...

  3. Android下使用Properties文件保存程序设置

    原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码.    读取.properties文件中的配置: String strValue ...

  4. 【转】Android下使用Properties文件保存程序设置

    原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码.    读取.properties文件中的配置:  String strValue ...

  5. 程序启动读取和关闭时保存应用程序设置(QSettings)

    保存应用程序设置(QSettings)1. QSettings 类 QSettings 提供保存应用程序当前设置的接口,可以方便地保存程序的状态,例如窗口大小和位置,选项的选中状态等等.在 Windo ...

  6. PDF 补丁丁 0.6.0.3363 版发布(修复无法保存应用程序设置的问题)

    本测试版修复了上一测试版无法保存应用程序设置的问题,以及导出导入信息文件的若干小问题.

  7. win7无法保存打印机设置(错误0x000006d9)解决方法

    解决win7打印机共享出现‘无法保存打印机设置’操作无法完成(错误0x000006d9),接下来与大家分享下解决方法, 找到windows firewall服务,启用即可 ============== ...

  8. linux系统学习笔记:无死角理解保存的设置用户ID,设置用户ID位,有效用户ID,实际用户ID

    一.基本概念 实际用户ID(RUID):用于标识一个系统中用户是谁,一般是在登录之后,就被唯一的确定,就是登录的用户的uid. 有效用户ID(EUID):用于系统决定用户对系统资源的权限,也就是说当用 ...

  9. 【.net 深呼吸】自己动手来写应用程序设置类

    在开始装逼之前,老周先说明一件事.有人说老周写的东西太简单了,能不能写点复杂点.这问题就来了,要写什么东西才叫“复杂”?最重要的是,写得太复杂了,一方面很多朋友看不懂,另一方面,连老周自己也不知道怎么 ...

随机推荐

  1. 百度复制SQL语句

    本词条从基础知识.判断对象和应用技巧等方面,介绍了SQL(Structured Query Language)结构化查询语言的应用方法. 目录 1基础 ▪ 创建数据库▪ 删除数据库▪ 备份sql se ...

  2. Enze fourth day(循环语句 一)

    哈喽,大家好.又到了总结知识的时间了.今天在云和学院自学了一下循环语句,下面是自己总的一些知识点. 先补充一下选择结构中的switch语句. 理论:switch语句是一种多分支选择语句,当需要测试大量 ...

  3. 【前端】使用weinre对手机、微信浏览器页面调试

    官方网站:http://people.apache.org/~pmuellr/weinre-docs/latest/ windows下安装以及使用: 1.安装nodejs 下载nodejs引擎,32b ...

  4. Swift和OC 混编

    1.首先创建一个Swift工程 2.导入或者创建一个OC文件(.h和.m) 3.再创建一个桥连接文件 4.然后文件样子为 5.在桥接链接里面导入头文件 6.通过targets->->bui ...

  5. Jsp中使用EL表达式对字符串进行操作

    用fn函数:<%@ taglib prefix="fn" uri="http://Java.sun.com/jsp/jstl/functions" %&g ...

  6. linux c: 静态库和动态库的生成和使用

    场景: main函数需要两个接口,一个求和函数,一个打印函数. int sum(int i, int j); 求两个int数字的和. void show(int i, char* name); 打印i ...

  7. Python之路Day16

    主要内容:Django基础进阶之:Django 流程.Django URL.Django Views.Django Models.Django Template.Django Admin Django ...

  8. 谷歌三大核心技术(一)The Google File System中文版

    谷歌三大核心技术(一)The Google File System中文版  The Google File System中文版 译者:alex 摘要 我们设计并实现了Google GFS文件系统,一个 ...

  9. HDoj-1527-取石子游戏

    取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  10. c++中的对象引用(object reference)与对象指针的区别

    ★ 相同点: 1. 都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:引用是某块内存的别名. ★ 区别: 1. 指针是一个实体,而引用仅是个别名: 2. 引用使用时无需解引用(*),指针需 ...