简体   繁体   中英

Qt4 SegmentationFault on QTableWidget::setItem

I have a very strange problem here. My program is SegmentationFault when I'm setting item to a table. Here is my code.

Header:

class Program : public QMainWindow {
    Q_OBJECT
    public:
        Program();

    private:
        QTableWidget *table;

    private slots:
        void newSlot();
}

Cpp File:

Program::Program() : QMainWindow() {
    ....
    ....
    ....
    ....
    table = new QTableWidget();
    table->setRowCount( 0 );
    table->setColumnCount( 2 );
    ....
    ....
    ....
}

void Program::newSlot() {
    ....
    ....
    ....
    table->insertRow( table->rowCount() );
    table->setItem( table->rowCount() - 1, 0, new QTableWidgetItem( "something" ) );
    table->setItem( table->rowCount() - 1, 1, new QTableWidgetItem( "something" ) );
    ....
    ....
    ....
}

The thing is when the program reaches the table->setItem( ... ) in newSlot() , I get a segmentation fault. Have I made some silly mistake somewhere that's causing this mess? 'Coz I have used the exact same code else where without any problem.

you have to specify column count:

table->setColumnCount( 2 );

to do

table->setItem( table->rowCount() - 1, 0, new QTableWidgetItem( "something" ) );
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM