Hello!
I'm writing a class using Qt collections classes (i.e. QVector, QMultiHash, QMultiMap). The first class I tried to use was QMultiHash. After getting crash similar to described below, I tried using QMultiMap, got same crash and now tried QVector class.
Back to subject. The class i'm writing looks like this:
//graph.h
{
Q_OBJECT
public:
~Graph();
signals:
void maxLevel(int);
public slots:
Node* addNode(const int level=0);
private slots:
private:
void calculateGeometry();
};
//graph.h
class Graph :public QObject, public QGraphicsItemGroup
{
Q_OBJECT
QVector< QVector <Node*> > nodes;
public:
Graph(QObject* parent=0);
~Graph();
signals:
void maxLevel(int);
public slots:
Node* addNode(const int level=0);
private slots:
void removeNode(QObject *arg);
private:
void calculateGeometry();
};
To copy to clipboard, switch view to plain text mode
And the crash happens in this function.
//graph.cpp
Node* Graph::addNode(const int level)
{
Node* ret=NULL;
if(level<0||level>nodes.count())return ret; //crash here
if(level==nodes.count()) nodes.insert(level,QVector<Node*>());
Node* node=new Node();
ret=node;
node->setText(trUtf8("Level %1").arg(level));
connect(node,
SIGNAL(destroyed
(QObject*)),
this,
SLOT(removeNode
(QObject*)));
addToGroup(node);
nodes[level].append(node);
calculateGeometry();
emit maxLevel(nodes.count());
return ret;
}
//graph.cpp
Node* Graph::addNode(const int level)
{
Node* ret=NULL;
if(level<0||level>nodes.count())return ret; //crash here
if(level==nodes.count()) nodes.insert(level,QVector<Node*>());
Node* node=new Node();
ret=node;
node->setText(trUtf8("Level %1").arg(level));
connect(node,SIGNAL(destroyed(QObject*)),this,SLOT(removeNode(QObject*)));
addToGroup(node);
nodes[level].append(node);
calculateGeometry();
emit maxLevel(nodes.count());
return ret;
}
To copy to clipboard, switch view to plain text mode
A pointer to an object of this class is used inside of a class, derived from QMainWindow.
Being called from it's constructor it works just fine.
MainWindow
::MainWindow(QWidget *parent
): ui(new Ui::MainWindow)
{
//........
Graph* g=new Graph();
g->addNode();
//........
}
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//........
Graph* g=new Graph();
g->addNode();
//........
}
To copy to clipboard, switch view to plain text mode
But when addNode() is called later (not inside constructor, via signal/slot) it crashes on the string I specified. In debug mode it stops on this line inside QVector:
//qvector.h
//........
inline int count() const { return d->size; }
//........
//qvector.h
//........
inline int count() const { return d->size; }
//........
To copy to clipboard, switch view to plain text mode
As I said, the same thing happened with QMultiHash and QMultiMap. I'm using Qt 4.8.1 with QtCreator 2.4.1 under Linux Mint 13 (Maya) 64 bit (kernel 3.2.0-23-generic.
Is this an issue of my code or Qt? (I tend to think that this is my code's fault, but I can't find source of the problem. Please help! BTW, The code provided compiles without a single error.
Bookmarks