Using QTableWidget from other function avoid segfault
-
Hello,
I try to create separate function in class for create a QTableWidget
fileview_widget.h
public: void initImageListWidget(QStringList labels, int columns); private: ... QTableWidget *filesTable; ...
fileview_widget.cpp
void FileViewWidget::initImageListWidget(QStringList labels, int columns) { grid->removeWidget(imageListWidget); QTableWidget *filesTable = new QTableWidget(0, columns); filesTable->setHorizontalHeaderLabels(labels); filesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed); filesTable->setShowGrid(false); filesTable->setColumnCount(columns); filesTable->setEditTriggers(QAbstractItemView::NoEditTriggers); filesTable->setFocusPolicy(Qt::NoFocus); grid->addWidget(filesTable,0,1,2,1); }
but when i use this widget from other function for insert new row i get segfault
while (cnt < 128) { filesTable->insertRow(cnt); <- there are segfault cnt++; }
segfault trace
#0 0x00007f71d5087140 in QMetaObject::cast(QObject const*) const () from /usr/lib/libQt5Core.so.5 [Current thread is 1 (Thread 0x7f71d1c33840 (LWP 288252))] (gdb) bt #0 0x00007f71d5087140 in QMetaObject::cast(QObject const*) const () from /usr/lib/libQt5Core.so.5 #1 0x00007f71d5f16e01 in QTableWidget::insertRow(int) () from /usr/lib/libQt5Widgets.so.5 #2 0x00005607e3bcece4 in FileViewWidget::openFile (this=0x7ffc0644ad50, fileInfo=...) at fileview_widget.cpp
if i create QTableWidget in this function - all work fine. Can you help with this problem ?
-
@Bolsvet said in Using QTableWidget from other function avoid segfault:
QTableWidget *filesTable = new QTableWidget(0, columns);
This is shadowing, strange your compiler doesn't give you a warning. it should be
filesTable = new QTableWidget(0, columns);
-
@VRonin His warning level may be too low or the warning is simply ignored. Sometimes hungary notation may help a bit.
private:
QTableWidget * m_pFilesTable{};in .cpp it is a member variable and does not need to be redeclared.
m_pFilesTable = new QTableWidget(0, columns); -