zoukankan      html  css  js  c++  java
  • QSettings保存程序设置

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

    文本编辑窗口

    TextEditor继承了QTextEdit,主要实现文本编辑和文本格式设置。保存文本信息时直接用的html字符串形式保存,可以同时保存文本的格式。
    [cpp] view plain copy
     
    1. class TextEditor:public QTextEdit  
    2. {  
    3.     Q_OBJECT  
    4. public:  
    5.     TextEditor(QWidget *parent = NULL);  
    6.     ~TextEditor();  
    7.   
    8.     void SaveSettings();  
    9.   
    10. protected:  
    11.     void ReadSettings();  
    12.     void contextMenuEvent ( QContextMenuEvent * event );  
    13. private slots:  
    14.     void SettingBackColorSlot();  
    15.     void SettingTextColorSlot();  
    16.     void SettingTextFontSlot();  
    17. };  

    [cpp] view plain copy
     
    1. TextEditor::TextEditor( QWidget *parent /*= NULL*/ ):QTextEdit(parent)  
    2. {  
    3.     ReadSettings();  
    4. }  
    5.   
    6. TextEditor::~TextEditor()  
    7. {  
    8.   
    9. }  
    10.   
    11. void TextEditor::contextMenuEvent( QContextMenuEvent * event )  
    12. {  
    13.     QMenu *pMenu = createStandardContextMenu();  
    14.     pMenu->addSeparator();  
    15.       
    16.     QTextCursor cursor = this->textCursor();  
    17.     QString seletedText = cursor.selectedText();  
    18.     if (!seletedText.isEmpty())     //选中文本才可以设置文本样式  
    19.     {  
    20.         QMenu *pSubMenu = new QMenu(tr("设置"),pMenu);  
    21.         pMenu->addMenu(pSubMenu);  
    22.         QAction *pFontAct = new QAction(tr("字体"),pSubMenu);  
    23.         QAction *pTextColorAct = new QAction(tr("字体颜色"),pSubMenu);  
    24.         QAction *pBackColorAct = new QAction(tr("背景色"),pSubMenu);  
    25.         pSubMenu->addAction(pFontAct);  
    26.         pSubMenu->addAction(pTextColorAct);  
    27.         pSubMenu->addAction(pBackColorAct);  
    28.   
    29.         connect(pFontAct,SIGNAL(triggered ()),this,SLOT(SettingTextFontSlot()));  
    30.         connect(pTextColorAct,SIGNAL(triggered ()),this,SLOT(SettingTextColorSlot()));  
    31.         connect(pBackColorAct,SIGNAL(triggered ()),this,SLOT(SettingBackColorSlot()));  
    32.     }  
    33.     pMenu->exec(event->globalPos());  
    34.     delete pMenu;  
    35. }  
    [cpp] view plain copy
     
    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. }  
     
     

    程序主窗口

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

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

  • 相关阅读:
    java学习笔记——基于Robot类的屏幕分享
    Java实例——基于jsoup的简单爬虫实现(从智联获取工作信息)
    Java实例练习——基于UDP协议的多客户端通信
    java实例练习——基于TCP/IP协议的多客户端通信
    我个人的Java学习经验(一家之言)
    PHP mac localhost 环境下发送邮件
    php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
    php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
    程序员应该知道的13个设计技巧
    程序员应该知道的13个设计技巧
  • 原文地址:https://www.cnblogs.com/findumars/p/5176100.html
Copyright © 2011-2022 走看看