Solved: QTabObject with QTableWidgetItem crash on delete



  • Hello,

    I have QTab object and in one tab, i have QTable on it. When the form unloads, it crashes.
    The form is called from main as pointer

    my h file
    @
    #ifndef REPORTWIN_H
    #define REPORTWIN_H

    #include <QDialog>
    #include "tmain.h" //1
    #include <QCloseEvent>

    namespace Ui {
    class reportwin;
    }

    class reportwin : public QDialog
    {
    Q_OBJECT

    public:
    explicit reportwin(QWidget *parent = 0);
    ~reportwin();
    TMain *mainparentform; //2

    protected:
    void closeEvent(QCloseEvent *event); //3

    private slots:

    private:
    Ui::reportwin *ui;
    void preparegraph(int graphtype);
    void initializeModel(QSqlTableModel *model);
    QPointer <QTableWidget> table;
    QHBoxLayout *layout;

    };

    #endif // REPORTWIN_H

    @

    here is how i create the table

    @
    layout=new QHBoxLayout(this);
    table = new QTableWidget(rows, cols, this);
    QStringList Header;
    Header << " Date & Time" << " Number " << " Average Typing Speed " << " % Correct " << " % completed";
    for (int r=0; r<rows; ++r)
    {
    for (int c = 0; c < cols; ++c)
    {
    QString character = Header.at(c);
    table->setHorizontalHeaderItem(c, new QTableWidgetItem(character));

        QTableWidgetItem *dateitem = new QTableWidgetItem();
        dateitem->setText(practisedata.datestring.at(r));
        table->setItem( r, 0, dateitem );
        QTableWidgetItem *number = new QTableWidgetItem( practisedata.log1_practisetestnumber.at(r));
        table->setItem( r, 1, number )
    
    }
    }
    table->setItemPrototype(table->item(rows -1, cols - 1));
    table->setGeometry(QRect(55,90,660,300));
    //table->show();
    layout->addWidget(table);
    this->ui->Training_tab1->setLayout(layout);
    

    @

    here is my main file
    @
    QPointer <reportwin> report = new reportwin(0);
    report->mainparentform=this;

        //report->deleteLater();
        this->hide();
        report->exec&#40;&#41;;
        report->done(1&#41;;
    

    // //delete report; // still not working, data still exist even after call
    // QObjectCleanupHandler *cleaner=new QObjectCleanupHandler;
    // cleaner->add(report);
    // cleaner->deleteLater();

        qDebug() << " exit report" << report.isNull();
    

    @

    I tried delete later but it crashes, i tried the object cleaner but same result. I tried to delete the pointer but the same. I understand from the QT crash report that it has difficulty deleting the QTab or the QTableWidget

    Any help is appreciated



  • you should not call "delete report" since report is no pointer but a simple (stack) instance of QPointer. QPointer is an object that holds a pointer and behaves like one.
    you could try to call "delete report->data();" but its probably better to use QSopedPointer. This class deletes the pointer it holds automatically in its destructor or when you call "reset()"



  • I tried delete report->data(); but same result .. Can you show me how to use QScopedpointer? because i never used it before ..
    thanks



  • I use this now
    @
    QScopedPointer<reportwin> report(new reportwin(0));
    @
    I removed the delete phrase but then it crashes also
    @
    Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
    0 QtGui 0x0000000100cdcaf9 QTableModel::~QTableModel() + 41
    1 QtCore 0x00000001004bc74f QObjectPrivate::deleteChildren() + 95
    2 QtGui 0x00000001007b3ec9 QWidget::~QWidget() + 713
    3 QtGui 0x0000000100cd8a66 QTableWidget::~QTableWidget() + 38
    4 QtCore 0x00000001004bc74f QObjectPrivate::deleteChildren() + 95
    5 QtGui 0x00000001007b39b9 QWidget::~QWidget() + 713
    6 QtCore 0x00000001004bc74f QObjectPrivate::deleteChildren() + 95
    7 QtGui 0x00000001007b3ec9 QWidget::~QWidget() + 713
    8 QtGui 0x0000000100bad8a9 QStackedWidget::~QStackedWidget() + 41
    9 QtCore 0x00000001004bc74f QObjectPrivate::deleteChildren() + 95
    10 QtGui 0x00000001007b3ec9 QWidget::~QWidget() + 713
    11 QtGui 0x0000000100bbac76 QTabWidget::~QTabWidget() + 38
    12 QtCore 0x00000001004bc74f QObjectPrivate::deleteChildren() + 95
    13 QtGui 0x00000001007b39b9 QWidget::~QWidget() + 713
    14 QtCore 0x00000001004bc74f QObjectPrivate::deleteChildren() + 95
    15 QtGui 0x00000001007b3ec9 QWidget::~QWidget() + 713
    16 QtGui 0x0000000100bad8a9 QStackedWidget::~QStackedWidget() + 41
    17 QtCore 0x00000001004bc74f QObjectPrivate::deleteChildren() + 95
    18 QtGui 0x00000001007b3ec9 QWidget::~QWidget() + 713
    19 QtGui 0x0000000100bbac76 QTabWidget::~QTabWidget() + 38
    20 QtCore 0x00000001004bc74f QObjectPrivate::deleteChildren() + 95
    21 QtGui 0x00000001007b3ec9 QWidget::~QWidget() + 713
    22 QtGui 0x0000000100b4af49 QFrame::~QFrame() + 41
    @



  • I found the mistake ..

    there must be an empty unfilled row at the end of the table, otherwise it crashes
    @
    rows=rows+1;
    @
    @
    table = new QTableWidget(rows, cols, this); // note to avoid crashing: there must be 1 empty row at the end
    QStringList Header;
    Header << " Date & Time" << " Number " << " Average Typing Speed " << " % Correct " << " % completed";
    //QTableWidgetItem *dateitem[rows];
    qDebug() << "rows=" << rows;
    for (int r=0; r<rows-1; r++)
    {
    for (int c = 0; c <cols; c++)
    {
    QString character = Header.at(c);
    table->setHorizontalHeaderItem(c, new QTableWidgetItem(character));
    if (r<=rows-1)
    {
    QTableWidgetItem *dateitem = new QTableWidgetItem();
    dateitem->setText(practisedata.datestring.at(r));
    table->setItem( r, 0, dateitem );
    QTableWidgetItem *number = new QTableWidgetItem( practisedata.log1_practisetestnumber.at(r));
    }

    }
    }
    table->setItemPrototype(table->item(rows -1, cols - 1));
    table->setGeometry(QRect(55,90,660,300));
    //table->show();
    layout->addWidget(table);
    this->ui->Training_tab1->setLayout(layout);
    

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.