0

我有一个简单的 Qt 应用程序,它主要基于 QTableWidget,在那里我必须显示从服务器获取的数据,并且用户必须能够修改和保存数据。

我仍在设置我的应用程序,问题是如果我与 QTableWidget 交互,我会得到一个 SEGFAULT。

错误: 错误信息

我已经尽可能地减少了代码

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

维度i.col

200;200;200;200

您在此处看到的代码取自我编写的另一个应用程序,我只做了一些小的更改,但即使我直接从其他应用程序导入所有代码并且没有更改任何内容,它仍然会崩溃。

另一个应用程序运行良好,并且每天都在使用。

控制台输出:

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.

我已经尝试清理项目,运行 qmake 并在网上搜索类似我的问题,但无济于事,根据我的经验,这意味着这是一个愚蠢的错误,但我仍然不知道为什么在与 QTableWidget 交互时它会崩溃。

4

0 回答 0