PDA

View Full Version : Problem with QVector



ReasyEasyPeasy
17th October 2015, 20:02
Hey,
i got a problem with a QVector. I used a QVector before and didnt have problems like this. I hope someone can explain me what I'm doing wrong.
Header:

#ifndef MYQTABLEWIDGETITEM_H
#define MYQTABLEWIDGETITEM_H
#include <QTableWidgetItem>
#include <QVector>
class MyQTableWidgetItem;

struct Connections{
MyQTableWidgetItem *connectedItem;
int connection;
};

class MyQTableWidgetItem : public QTableWidgetItem
{
public:
MyQTableWidgetItem();
void setConnection(MyQTableWidgetItem *connectedItem, int connection);
private:
QVector<Connections> ConnectedItems;
bool virgin;

signals:

public slots:

};

#endif // MYQTABLEWIDGETITEM_H

source:

#include "myqtablewidgetitem.h"
#include <QDebug>
MyQTableWidgetItem::MyQTableWidgetItem()
{
this->virgin = true;
}

void MyQTableWidgetItem::setConnection(MyQTableWidgetIt em *connectedItem, int connection)
{

this->virgin = false;
Connections newConnection;
newConnection.connectedItem = connectedItem;
newConnection.connection = connection;
this->ConnectedItems.push_back(newConnection);

}

I call this function:

this->m_leftItem->setConnection(this->m_topItem,this->counter);
I get no errors but the following message:


Signal name : SIGSEGV
Signal meaning: Segmentation fault

anda_skoa
17th October 2015, 20:35
You'll need to check the backtrace of the crash.
Maybe "m_leftItem" is not a valid pointer.

Btw, no need to prefix member access with "this->", that is always implicit in C++

Cheers,
_

ReasyEasyPeasy
17th October 2015, 20:55
Hmmm what do you mean with not valid? If I do something like "this->m_LeftItem.text();" its working and I get the text which was saved there.
thats what I get from the Debugger.
0 QVector<Connections>::append qvector.h 598 0x409d29
1 QVector<Connections>::push_back qvector.h 224 0x409ead
2 MyQTableWidgetItem::setConnection myqtablewidgetitem.cpp 36 0x403a74
3 SymbolButton::setCounterOnClick symbolbutton.cpp 68 0x403510
4 SymbolButton::changeSymbol symbolbutton.cpp 18 0x402aab
5 SymbolButton::qt_static_metacall moc_symbolbutton.cpp 92 0x4043e6
6 QMetaObject::activate C:/Qt/5.4/mingw491_32/bin/Qt5Cored.dll 3717 0x6b95a575
7 QMetaObject::activate C:/Qt/5.4/mingw491_32/bin/Qt5Cored.dll 3582 0x6b959eba
8 QAbstractButton::clicked C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 298 0x9f720c3
9 QAbstractButtonPrivate::emitClicked C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 534 0x9d353a9
10 QAbstractButtonPrivate::click C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 527 0x9d35344
11 QAbstractButton::mouseReleaseEvent C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 1132 0x9d36643
12 QWidget::event C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 8657 0x9c73ecf
13 QAbstractButton::event C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 1089 0x9d364b8
14 QPushButton::event C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 673 0x9dc3942
15 QApplicationPrivate::notify_helper C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 3720 0x9c3f4dd
16 QApplication::notify C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 3280 0x9c3d61c
17 QCoreApplication::notifyInternal C:/Qt/5.4/mingw491_32/bin/Qt5Cored.dll 935 0x6b92f330
18 QCoreApplication::sendSpontaneousEvent C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 231 0x9f9ee85
19 QApplicationPrivate::sendMouseEvent C:/Qt/5.4/mingw491_32/bin/Qt5Widgetsd.dll 2751 0x9c3c1a1
... <Mehr>
Always if I want to put my struct in the QVector my program crash.

anda_skoa
18th October 2015, 10:26
When something like that crashes it usually means that "this" is not a valid instance.
For example an uninitialized pointer or an already deleted object.

In your SymbolButton::setCounterOnClick(), how do you get access to the item on which you then call setConnection?

Cheers,
_

ReasyEasyPeasy
19th October 2015, 19:05
I got it thanks!