Marcus
Marcus

Reputation: 1703

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.

Upvotes: 0

Views: 221

Answers (1)

AnatolyS
AnatolyS

Reputation: 4319

you have to specify column count:

table->setColumnCount( 2 );

to do

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

Upvotes: 1

Related Questions