Unsolved QHash insert problem
-
Hi everyone, I'm trying to compile my code through a project for my IT (which consistes to implement a typing test in C++). Here the tstatswpm.h of my class tstatswpm:
#ifndef TSTATSWPM_H #define TSTATSWPM_H using namespace std; #include <QWidget> #include <QPainter> #include <QMouseEvent> #include <QEvent> #include <stdlib.h> #include <time.h> #include <iostream> #include "Data/tuser.h" #include "Data/texercice.h" #include "Data/tresult.h" class TStatsWPM: public QWidget { public: TStatsWPM(TUser *user=0, QWidget *parent=0); protected: void paintEvent(QPaintEvent *e); void createRandomYPoints(); void mouseMoveEvent(QMouseEvent * e); void mousePressEvent(QMouseEvent *e); private: TUser *user_; float mean_ = 0; int ypoints_[]; QHash<QPoint *,QRect> rectpoint_; }; #endif // TSTATSWPM_H
Here the tstatswpm.cpp:
#include "tstatswpm.h" TStatsWPM::TStatsWPM(TUser *user, QWidget *parent): QWidget(parent), user_(user), rectpoint_(QHash<QPoint *,QRect>()) { setFocus(); setMouseTracking(true); } void TStatsWPM::createRandomYPoints(){ int val; srand (time(NULL)); this->ypoints_[0] = 300; for(int i = 1; i<19; i++){ val = rand() % 300 + 1; this->ypoints_[i] = 300 - val; } this->ypoints_[19] = 300; } void TStatsWPM::paintEvent(QPaintEvent *e){ createRandomYPoints(); QPainter p; QPen pen; p.begin(this); p.setBrush(Qt::cyan); p.setPen(Qt::NoPen); static QPoint *points = (QPoint*)malloc(sizeof(QPoint)*20); points[0] = QPoint(0,300); for(int i=1;i<19;i++){ if(i==1){ QPoint *point = new QPoint(0, ypoints_[i]); points[i] = *point; QRect rect(0,0,24,300); rectpoint_.insert(point,rect); }else{ QPoint *point = new QPoint(i*50, ypoints_[i]); points[i] = *point; QRect rect((i*50)-25,0,48,300); rectpoint_.insert(point,rect); } } points[19] = QPoint(18*50,300); p.drawPolygon(points,20); pen.setBrush(Qt::white); pen.setWidth(6); p.setPen(pen); p.drawPoints(points,20); p.end(); } void TStatsWPM::mouseMoveEvent(QMouseEvent *e){ cout << e->x() << "," << e->y() << endl; } void TStatsWPM::mousePressEvent(QMouseEvent *e){ cout << "click" << endl; }
The problem is that i can't insert in my QHash in lines:
rectpoint_.insert(point,rect);
Everything is compiling but the program crashed instantly. The loop,
for(int i=1;i<19;i++){ if(i==1){ QPoint *point = new QPoint(0, ypoints_[i]); points[i] = *point; QRect rect(0,0,24,300); rectpoint_.insert(point,rect); }else{ QPoint *point = new QPoint(i*50, ypoints_[i]); points[i] = *point; QRect rect((i*50)-25,0,48,300); rectpoint_.insert(point,rect); } }
is not going further than i = 1 and crashed at the insert (everything is working when i remove it).
Do you think the problem comes from my constructor ?TStatsWPM::TStatsWPM(TUser *user, QWidget *parent): QWidget(parent), user_(user), rectpoint_(QHash<QPoint *,QRect>()) { setFocus(); setMouseTracking(true); }
line :
rectpoint_(QHash<QPoint *,QRect>())
If someone has an idea about this I would be very grateful.
Pierre
-
Hello,
Well, I think you have multitude of problems with that code, and I also believe it has nothing to do with theQHash
itself. The most obvious thing I see is it that you're writing beyond your memory block and you're making a bloody mess of the heap. Look here:class TStatsWPM: public QWidget { // ... int ypoints_[]; //< !!! } void TStatsWPM::createRandomYPoints() { // ... this->ypoints_[0] = 300; //< !!! for(int i = 1; i<19; i++) { // ... this->ypoints_[i] = 300 - val; // ... } }
Who's supposed to initialize the
TStatsWPM::ypoints_
member before you start writing onto it? I don't see such code in your example. And this:static QPoint *points = (QPoint*)malloc(sizeof(QPoint)*20);
Is very dubious, to say the least. What's the problem with using
QHash<QPoint, QRect>
? Not to mention that no one is cleaning up thatmalloc
-ated memory ...To sum up, use
QPoint
objects, don't create static arrays (or objects) withmalloc
. If you want to have an array, usenew
instead. Free up the memory you're allocating.Kind regards.