今天看了一些QSettings的简单用法,可以用来保存程序的设置,使得程序每次启动都可以显示上次关闭时的状态。我这里实现了一个简单的文本编辑窗口,可以设置文本的字体,字体的颜色和背景色。每次关闭程序都保存程序的几何大小,位置和文本以及文本所设置的格式,方便启动程序后继续编辑。
文本编辑窗口
TextEditor继承了QTextEdit,主要实现文本编辑和文本格式设置。保存文本信息时直接用的html字符串形式保存,可以同时保存文本的格式。
[cpp] view plain copy
- 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();
- };
[cpp] view plain copy
- 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;
- }
[cpp] view plain copy
- //设置文本背景色
- 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);
- }
程序主窗口
[cpp] view plain copy
- 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;
- };
[cpp] view plain copy
- 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
时间: 2024-10-12 02:24:41