QSettings uncorrect saving
-
if call method for reading settings after method for saving,while program working,then QMap saved.But if restart program, then settings don't saving
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QtWidgets>
#include "mainnotes.h"
#include <QtCore>typedef QMap<int,MainNotes*> QMapNotes;
Q_DECLARE_METATYPE(QMapNotes)class MainWindow : public QWidget
{
Q_OBJECTprivate:
//QMap<long,MainNotes*> localeforNotes;
QMapNotes localeforNotes;
QMap<long,QString> proxyLocal;long idNote; QString mainThemeNote; QPushButton *createButton; QPushButton *deleteButton; QPushButton *changeButton; QBoxLayout *buttonLayout; QHBoxLayout *horizontalLayout; QVBoxLayout *verticalLayout; MainNotes *newNote; QSplitter *mainNoteSplitter; QSettings mainSettings; void createButtonClick(); void deleteButtonClick(); void changeButtonClick();
public:
explicit MainWindow(QWidget *parent = 0);
virtual ~MainWindow();void readSettings(); void writeSettings();
signals:
public slots:
void deleteNote();
void changeSettings();
void createNote();
void buttonActivate();};
static QDataStream &operator>>(QDataStream &in, QMapNotes ¬es)
{return in;
}
#endif // MAINWINDOW_H
//////////////////////////////
mainwindow.cpp////////// In constructor of class
qRegisterMetaType<QMapNotes>("QMapNotes"); qRegisterMetaTypeStreamOperators<QMapNotes>("QMapNotes");
/////////
///// method for saving
void MainWindow::writeSettings()
{mainSettings.beginGroup("/Settings");
mainSettings.setValue("/notes",QVariant::fromValue<QMapNotes>(localeforNotes));
mainSettings.endGroup();
}///method for reading settings
void MainWindow::readSettings()
{
mainSettings.beginGroup("/Settings");QVariant savingSettings = mainSettings.value("/notes");
localeforNotes = savingSettings.value<QMapNotes>();
QMapNotes::iterator it = localeforNotes.begin();
while(it != localeforNotes.end()) { mainNoteSplitter->insertWidget(it.key(),it.value()); it++; } mainSettings.endGroup();
}
-
This post is deleted! -
Hi,
Seems you are storing pointers to your MainNotes and your QDataStream operator>> doesn't do anything. That won't help much on the save/load part.
-
@SGaist I am sorry How i can do correctly save of QMap with my QTextEdit's objects,that if program restart that i have QMap with objects inside?
-
You must provide both operators for your custom type then you need to use Q_DECLARE_METATYPE to make it known and qRegisterMetaTypeStreamOperators to make the stream operators known. You will then be able to store and load your class.
You don't need to do anything particular for QMap, it's already handled for you however your must also call qRegisterMetaTypeStreamOperators for your QMapNotes (note that you shouldn't prefix your classes with Q, that will make thing confusing for other people reading your code)
-
Okay,I am understand,but qRegisterMetaTypeStreamOperators I must using for input or output my QMap,right?
-
qRegisterMetaTypeStreamOperators register both operators. You will need both
-
Ok,Thx, I try to do it