Can't add items to QMap
I have a problem. I assume I'm doing something really dumb. I'm new to C++ and Qt.
I have a class SavedVar. I'm trying to add a key/value to a private member Qmap. When I call SavedVar::addVar(), the program crashes. SavedVar::addvar() is receiving 'id' and 'notes' correctly.
#include <QObject>
#include <QMap>
#include "variation.h"
#include <QTableWidget>
#include "mainwindow.h"
class SavedVar : public QObject
{
Q_OBJECT
public:
explicit SavedVar(QObject *parent = 0);
void addVar(QString id, QString notes);
void saveVar(QString varText );
void removeSavedVar(QList <QTableWidgetItem*> &varList);
QString generateSaveFile();
void loadVarFile(QString filename);
const QMap<QString, QString>*getVmapPtr();
private:
QMap <QString, QStri开发者_StackOverflowng> vmap;
};
void SavedVar::addVar(QString id, QString notes)
{
vmap[id]= notes;
}
I'm struggling to understand the debug output in Qt creator:
0 QBasicAtomicInt::operator!= qbasicatomic.h 75 0x4116e3
1 QMap<QString, QString>::detach qmap.h 202 0x41ede2
2 QMap<QString, QString>::operator[] qmap.h 526 0x41d241
3 SavedVar::addVar savedvar.cpp 17 0x44afc9
4 SavedVar::loadVarFile savedvar.cpp 82 0x44b4b8
5 MainWindow::load_data mainwindow.cpp 190 0x40d5fe
Cheers,
Neil
Maybe you didn't initialize the QMap object by doing so :
vmap = new(QMap <QString, QString>);
Then you can manipulate your object with insert() and remove().
Have you tryed with the QMap method insert ?
void SavedVar::addVar(QString id, QString notes)
{
vmap.insert(id, notes);
}
精彩评论