PDA

View Full Version : QTableWidget in designer



pkomurka
12th May 2011, 13:35
Maybe it is a very lame question, but I am struggling with SIGSEGVs and really don't know what I am doing wrong...

I have a QTableWidget constructed via designer. I can use it in a program as ui->mytable. Everything is just fine as long as I don't want to create a new QTableWidgetItem via program.

At first, I set number of columns to 3 and rows to 0 and set text.



ui->mytable->setColumnCount(3);
ui->mytable->setRowCount(0);

QStringList tblLabels;
tblLabels << "blah1" << "blah2" << "blah3";
ui->mytable->setHorizontalHeaderLabels(tblLabels);


Next, I have a button which adds one single row at a time.



int row;
row = ui->mytable->rowCount();
ui->mytable->setRowCount(row + 1);


This successfully adds one row, which is editable, everything is working fine. If I try to read or modify cell, which was already written from UI, everything works. But I want to predefine cell values for whole row. I am doing it as it is written in the manual. To create new items on empty row (current row), I just do:



int row;
QTableWidgetItem *item1 = new QTableWidgetItem("1st value");
QTableWidgetItem *item2 = new QTableWidgetItem("2nd value");
QTableWidgetItem *item3 = new QTableWidgetItem("3rd value");
row = ui->mytable->currentRow();
ui->mytable->setItem(row,0,item1);
ui->mytable->setItem(row,1,item2);
ui->mytable->setItem(row,2,item3);


The setItem call fails with SIGSEGV... Not have a clue why. I tried to create own subclass of QTableWidget and do its own initialization, again with SIGSEGV. The whole row is created with three columns, but I cannot add new items via program.

Thank you for any help !

high_flyer
12th May 2011, 15:54
What is the value of 'row' after this call:

row = ui->mytable->currentRow();
Current row returns the row of the current item, and you didn't set any item to be current.
You probably mean to use rowCount() - 1.

pkomurka
13th May 2011, 06:05
In row variable is correct value. I figured that out, my mistake was hidden in on_mytable_cellChanged() slot, which was called every time setItem was called. So error was not in initialization, but in another code which was called along.

At end, it was very lame, as supposed (it is every time) :)