0

I have a simple Qt application which is mainly based around a QTableWidget, in there I have to display data that I get from a server and the user must be able to modify and save the data.

I'm still setting up my application, the problem is that I get a SEGFAULT if I interact with the QTableWidget at all.

The error: error message

I've reduced the code as much as possible

cila.cpp

#include "cila.h"
#include "ui_cila.h"

#include <QFile>
#include <QIODevice>
#include <QMessageBox>
#include <QInputDialog>


// ---  CORE  --- //
CILA::CILA(QWidget *parent) : QMainWindow(parent), ui(new Ui::CILA) {
    ui->setupUi(this);

    // --- LINKS --- //
    //QAction* salvaLayout = CILA::findChild<QAction*>("actionSalva_Layout");
    QTableWidget* tableLavori = CILA::findChild<QTableWidget*>("tableWidget");

    // --- CONNECTIONS --- //
    //connect(salvaLayout, &QAction::triggered, this, &CILA::saveDimCol);
    connect(tableLavori->horizontalHeader(),
            &QHeaderView::sectionResized,
            [this](int i, int od, int nd) {if (i != 4) dimColonne[i] = nd;});

    // --- INITIALIZATION --- //
    numeroColonne = tableLavori->columnCount()-1;

    convertDimCol(getDimCol());
    setDimCol();

}

CILA::~CILA() {
    delete ui;
}
// ---  END CORE  --- //


// ---  COLONNE  --- //
void CILA::saveDimCol() {
    qDebug() << "---== CILA::saveDimCol() ==---";
    QFile file("dimensioni.col");
    if (!file.open(QIODevice::WriteOnly)) {
        QMessageBox::information(0, "Error", file.errorString());
        return;
    }

    QTextStream out(&file);
    QString dimensioni = "";

    for (unsigned int i = 0; i < numeroColonne; i++) {
        dimensioni += QString(std::to_string(tableLavori->columnWidth(i)).c_str());
        if (i != 3) dimensioni += ";";
    }

    qDebug() << "ABOUT TO WRITE: " << dimensioni;
    out << dimensioni;
    qDebug() << "---== return CILA::saveDimCol() ==---";
}

void CILA::setDimCol() {
    qDebug() << "---== CILA::setDimCol() ==---";

    qDebug() << "TEST";
    qDebug() << tableLavori->columnCount(); // Now it crashes here, these lines where introduced to test what was wrong with QTableWidget
    for (unsigned int i = 0; i < numeroColonne; i++)
        qDebug() << "WIDTH OF COLUMN" << i << "IS" << tableLavori->columnWidth(i);
    qDebug() << "END OF TEST";

    for (unsigned int i = 0; i < numeroColonne; i++){
        qDebug() << "i =" << i << ";" << i << "<" << numeroColonne << "| dimColonne[i] =" << dimColonne[i];
        tableLavori->setColumnWidth(i, dimColonne[i]); // Original crash point was here
        qDebug() << "Succesfully set column" << i << "to width" << dimColonne[i];
    }
    qDebug() << "---== return CILA::setDimCol() ==---";
}

QList<int> CILA::getDimCol() {
    qDebug() << "---== CILA::getDimCol() ==---";
    QList<int> dimensioniColonne;
    QFile file("dimensioni.col");  // The content of this file is "200;200;200;200"
    if (!file.exists()) {
        for (unsigned int i = 0; i < numeroColonne; i++)
            dimensioniColonne.append(tableLavori->columnWidth(i));
        qDebug() << "ERROR; FILE DOES NOT EXIST";
        return dimensioniColonne;
    }

    if (!file.open(QIODevice::ReadOnly)) {
        QMessageBox::information(0, "Error", file.errorString());
        qDebug() << "ERROR; FILE IS READONLY";
        return dimensioniColonne;
    }

    QTextStream in(&file);

    QString out = in.readLine();
    qDebug() << "Content read from file: " << out;

    for (const QString &_item : out.split(";")) {
        dimensioniColonne.append(_item.toInt());
        qDebug() << "_ITEM (=" << _item << ") has been appended to the returning list";
    }

    qDebug() << "RETURNING VALUE QLIST<INT> =" << dimensioniColonne;
    qDebug() << "---== return CILA::getDimCol() ==---";
    return dimensioniColonne;
}

void CILA::convertDimCol(QList<int> dimCol) {
    qDebug() << "---== CILA::convertDimCol() ==---";
    qDebug() << "PARAMS:  -1- QList<int> dimCol =" << dimCol;
    for (unsigned int i = 0; i < numeroColonne; i++) {
        qDebug() << "----------------------------------------";
        qDebug() << "i =" << i << ";" << i << "<" << numeroColonne;
        qDebug() << "dimColonne[" << i << "] = dimCol[" << i << "] (=" << dimCol[i] << ")";
        dimColonne[i] = dimCol[i];
    }
    qDebug() << "---== return CILA::convertDimCol() ==---";
}
// ---  END COLONNE  --- //

cila.h

#ifndef CILA_H
#define CILA_H

#include <QMainWindow>
#include <QTableWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class CILA; }
QT_END_NAMESPACE

class CILA : public QMainWindow
{
    Q_OBJECT

public:
    CILA(QWidget *parent = nullptr);
    ~CILA();
    QTableWidget* tableLavori;
    unsigned int numeroColonne;
    int dimColonne[4];

private:
    Ui::CILA *ui;
    //---COLONNE---//
    void setDimCol();
    void saveDimCol();
    QList<int> getDimCol();
    void convertDimCol(QList<int> dimCol);
    //---END_COLONNE---//
};
#endif // CILA_H

dimensioni.col

200;200;200;200

The code that you are seeing here was taken from another application that I wrote, I did just a few minor changes, but even if I directly imported all the code from my other application and did not changed anything it would still crash.

The other application runs just fine and is being used daily.

Output from the console:

15:08:11: Starting /home/davide/Qt-Projs/build-CILA-Desktop_Qt_6_1_1_GCC_64bit-Debug/CILA ...
---== CILA::getDimCol() ==---
Content read from file:  "200;200;200;200"
_ITEM (= "200" ) has been appended to the returning list
_ITEM (= "200" ) has been appended to the returning list
_ITEM (= "200" ) has been appended to the returning list
_ITEM (= "200" ) has been appended to the returning list
RETURNING VALUE QLIST<INT> = QList(200, 200, 200, 200)
---== return CILA::getDimCol() ==---
---== CILA::convertDimCol() ==---
PARAMS:  -1- QList<int> dimCol = QList(200, 200, 200, 200)
----------------------------------------
i = 0 ; 0 < 4
dimColonne[ 0 ] = dimCol[ 0 ] (= 200 )
----------------------------------------
i = 1 ; 1 < 4
dimColonne[ 1 ] = dimCol[ 1 ] (= 200 )
----------------------------------------
i = 2 ; 2 < 4
dimColonne[ 2 ] = dimCol[ 2 ] (= 200 )
----------------------------------------
i = 3 ; 3 < 4
dimColonne[ 3 ] = dimCol[ 3 ] (= 200 )
---== return CILA::convertDimCol() ==---
---== CILA::setDimCol() ==---
TEST
15:08:11: The program has unexpectedly finished.
15:08:11: The process was ended forcefully.
15:08:11: /home/davide/Qt-Projs/build-CILA-Desktop_Qt_6_1_1_GCC_64bit-Debug/CILA crashed.

I already tried to clean the project, run qmake and search for issues like mine online but with no avail, from my experience this means that this is a stupid mistake but I still have no idea on why it crashes when I interact with QTableWidget.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 2
    You re-declare and initialize the variable `tableLavori` locally with the `CILA` ctor. Hence the member variable of the same name is left uninitialized causing the segfault when accessed. – G.M. Jul 13 '21 at 13:28
  • @G.M. It was indeed a stupid mistake, thank you for helping. – MrJohnSmith35 Jul 14 '21 at 11:42

0 Answers0