• 5451阅读
  • 4回复

qsettings的使用? [复制链接]

上一主题 下一主题
离线shifan
 
只看楼主 倒序阅读 楼主  发表于: 2009-08-15
//modifyDialog.h
#ifndef MODIFYDIALOG_H
#define MODIFYDIALOG_H
#include<qtabdialog.h>
#include<qsettings.h>
class QListBox;
class QPushButton;
class QVBox;
class QHBox;
class modifyDialog:public QTabDialog
{
    Q_OBJECT
public:
    modifyDialog(QWidget *parent=0,const char *name=0,bool modal=true,WFlags f=0);
public slots:
    void addButton1Clicked();
    void addButton2Clicked();
    void deleteButton2Clicked();
    void deleteButton1Clicked();
    void somethingSelected(const QString &);
private:
    QListBox *fieldList;
    QListBox *specialList;
    QPushButton *addButton1;
    QPushButton *addButton2;
    QPushButton *deleteButton1;
    QPushButton *deleteButton2;
    QVBox *verticalBar1;
    QVBox *verticalBar2;
    QHBox *horizontalBar1;
    QHBox *horizontalBar2;
    QStringList specialListCon;    
    QString item;
    QSettings *settings;
};
#endif


//modifyDialog.cpp
#include<qinputdialog.h>
#include<iostream>
#include<qmessagebox.h>
#include<qhbox.h>
#include<qvbox.h>
#include"modifyDialog.h"
#include<qlistbox.h>
#include<qpushbutton.h>
const QString special("/special/");
using namespace std;
modifyDialog::modifyDialog(QWidget *parent,const char *name,bool modal,WFlags f)
    :QTabDialog(parent,name,modal,f)
{
    settings=new QSettings(QSettings::Ini);
    settings->setPath("liucong.com","TinyFinance");
    settings->insertSearchPath(QSettings::Unix,"/root/Desktop/cpp/finance");
    item="";
    this->setOkButton("OK");
    this->setApplyButton("APPLY");
    this->setCancelButton("CANCEL");

    horizontalBar1= new QHBox(this);
    fieldList=new QListBox(horizontalBar1);
    verticalBar1=new QVBox(horizontalBar1);
    addButton1=new QPushButton("add",verticalBar1);
    deleteButton1=new QPushButton("delete",verticalBar1);

    horizontalBar2= new QHBox(this);
    specialList=new QListBox(horizontalBar2);
    verticalBar2=new QVBox(horizontalBar2);
    addButton2=new QPushButton("add",verticalBar2);
    deleteButton2=new QPushButton("delete",verticalBar2);

    this->addTab(horizontalBar1,"field");
    this->addTab(horizontalBar2,"special");
    
    connect(fieldList,SIGNAL(highlighted(const QString &)),this,SLOT(somethingSelected(const QString &)));
    connect(addButton1,SIGNAL(clicked()),this,SLOT(addButton1Clicked()));
    connect(addButton2,SIGNAL(clicked()),this,SLOT(addButton2Clicked()));
    connect(deleteButton1,SIGNAL(clicked()),this,SLOT(deleteButton1Clicked()));
    connect(deleteButton2,SIGNAL(clicked()),this,SLOT(deleteButton2Clicked()));

    fieldList->setSelectionMode(QListBox::Single);
    specialList->setSelectionMode(QListBox::Single);

    fieldList->insertStringList(settings->entryList("/special"));
}

void modifyDialog::somethingSelected(const QString &selected)
{
cout<<"Selected is(in somethingSelected)"<<selected<<endl;
    specialListCon=settings->readListEntry(special+selected);
    specialList->clear();
    specialList->insertStringList(specialListCon);
    item=selected;
}

void modifyDialog::addButton1Clicked()
{
    QString addItem=QInputDialog::getText("Insert a item","What is the item you want?");
cout<<"in addButton1Clicked"<<"/special/+addItem:"<<"/special/"+addItem<<endl;
    if(addItem!="")
    {
        if(!settings->writeEntry("/special/"+addItem,QStringList()))
            QMessageBox::information(this,"Sorry","The operation failed!");
        else
        {
            fieldList->clear();
            fieldList->insertStringList(settings->entryList("/special"));
        }
    }
}

void modifyDialog::addButton2Clicked()
{
    QString addItem=QInputDialog::getText("Insert a item","What is the item you want?");
cout<<"in addButton2Clicked"<<"addItem:"<<addItem<<endl;
    if(addItem!="")
    {
        specialListCon.push_back(addItem);
        if(!settings->writeEntry(special+item,specialListCon))
            QMessageBox::information(this,"Sorry","The operation failed!");
        else
        {
            specialList->clear();
            specialList->insertStringList(settings->readListEntry(special+item));
        }
    }
}

void modifyDialog::deleteButton2Clicked()
{
    QString addItem=specialList->currentText();
cout<<"in deleteButton2Clicked"<<"addItem:"<<addItem<<endl;
    specialListCon.remove(addItem);
    if(!settings->writeEntry(special+item,specialListCon))
        QMessageBox::information(this,"Sorry","The operation failed!");
    else
    {
        specialList->clear();
        specialList->insertStringList(settings->readListEntry(special+item));
    }
}

void modifyDialog::deleteButton1Clicked()
{
    QString addItem=fieldList->currentText();
cout<<"in deleteButton1Clicked"<<"addItem:"<<addItem<<endl;
    if(!settings->removeEntry(special+addItem))
        QMessageBox::information(this,"Sorry","The operation failed!");
    else
    {
        fieldList->clear();
        fieldList->insertStringList(settings->entryList("/special"));
    }
}

为什么我用qsettings写的配置,下一次启动程序就不能够调用出来呢?
看了很多资料,好像大家也就是这样用的啊,为什么我的就不能保存配置呢?
而且运行完程序后我也没有在我指定的文件夹里找到保存的文件,难道操作
失败了?但是完全没有报错,而且单次操作时各项功能运行的很好啊。。。。
离线gotomall
只看该作者 1楼 发表于: 2009-08-16
unix 下用ini?
离线gotomall
只看该作者 2楼 发表于: 2009-08-16
QSettings::InvalidFormat    16    Special value returned by registerFormat().
你看看你系统支持那种格式。
话说你到底用了什么系统?
离线shifan
只看该作者 3楼 发表于: 2009-08-16
引用第1楼gotomall于2009-08-16 02:22发表的  :
unix 下用ini?

我把它改成Native,但是依旧不行。
离线shifan
只看该作者 4楼 发表于: 2009-08-16
引用第2楼gotomall于2009-08-16 02:23发表的  :
QSettings::InvalidFormat    16    Special value returned by registerFormat().
你看看你系统支持那种格式。
话说你到底用了什么系统?

我用的是RHEL 5.

QSettings::Format
QSettings::Native - Store the settings in a platform dependent location
QSettings::Ini - Store the settings in a text file

这两种格式应该都支持啊。。。
快速回复
限100 字节
 
上一个 下一个