0

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.

Marcus
  • 1,685
  • 1
  • 18
  • 33

1 Answers1

1

you have to specify column count:

table->setColumnCount( 2 );

to do

table->setItem( table->rowCount() - 1, 0, new QTableWidgetItem( "something" ) );
...
AnatolyS
  • 4,249
  • 18
  • 28
  • Ahh... Sorry. My mistake in the code here. It is supposed to be two. I'll correct it. – Marcus Mar 28 '13 at 13:03
  • sorry to tell but the following example work as you expected: int main(int argc, char *argv[]) { QApplication a(argc, argv); QTableWidget w; w.setColumnCount(2); w.insertRow(0); w.setItem(0, 0, new QTableWidgetItem("1")); w.setItem(0, 1, new QTableWidgetItem("2")); w.show(); return a.exec(); } may be there is no enought info about your problem. – AnatolyS Mar 28 '13 at 13:09
  • When you type it that way, yes it works... Even in my program. The first time I populate the table, its perfect. Its later on when I try to add a new row and populate it, that it crashes. I could put up the actual code, but its too long. – Marcus Mar 28 '13 at 13:14
  • are there subcribers for your table widget events? – AnatolyS Mar 28 '13 at 13:17
  • yes... One for `cellChanged( int, int )` which is connected to a function that saves that data in that row via QSettings: `settings.setValue( table->item( row, 0 )->text(), table->item( row, 1 )->text() );` – Marcus Mar 28 '13 at 13:22
  • try to rewrite it in the following manner: if (table->item(row, 0) && table->item(row, 1) ) {settings.setValue( table->item( row, 0 )->text(), table->item( row, 1 )->text() );} – AnatolyS Mar 28 '13 at 13:23
  • Ok.. I think a description of what happens during the program execution would help. This is for windows. Program Launcher. I want custom shortcuts to open certain programs. Using QxtGlobalShortcut I am able to to that. The table is to add or remove the shortcuts. I use QSettings to save the key bindings. The first time I populate the table, I read the data from the settings file. The `newSlot()` or rather `addShortcut()` is called when I add another shortcut. I'm using exactly the same code for populating the table both the times. – Marcus Mar 28 '13 at 13:33
  • The `cellChanged( int, int )` signal is connected to `saveData( int, int )`. When populating the table the first time, this `saveData( int, int )` is not called, where as when I add a row later on it is. Any idea why? And yes, the problem is due to `saveData( int, int )` – Marcus Mar 28 '13 at 13:36
  • I see I had made a series of silly mistakes!! All's well now. The main one was I had not realized `setItem(...)` must result in `cellChanged(...)`. – Marcus Mar 28 '13 at 13:52