PDA

View Full Version : Selecting somes cells from a QTableWidget



Stanfillirenfro
12th April 2014, 10:43
Hi!
Can anyone helps in selecting just some cells from a column of a QtableWidget? Is there a specific method for that?
To be clear, I want to remove some cells from my selection of a given column's data.
I have tried the following code, but it is not wroking. I have the following error message:


Cannot call member function 'QModelIndexList QItemSelectionModel::selectedRows(int) const' without object

Here is my code:

myTable->setSelectionMode(QItemSelectionModel::selectedRows ());

Many thanks in advance.

anda_skoa
12th April 2014, 11:04
There are two errors in your code

1) setSelectionMode takes a value of the QAbstractItemView::SelectionMode enum, not a list of model indexes
2) QItemSelectionModel::selectedRows() is an instance method, it needs to be called on an instance of QItemSelection model. That is also what the compiler tells you :)

Cheers,
_

Stanfillirenfro
12th April 2014, 11:27
Many thanks Anda_Skoa for your reply.

I have modified the code as followed, but even if I deselect some cells, I still have their data.


myTableau->setSelectionMode(QAbstractItemView::MultiSelection );

What am I doing wrong?

Many thanks!

anda_skoa
12th April 2014, 14:19
I am not sure what you mean. The selection does not affect any data, it is just visualization.

Cheers,
_

Stanfillirenfro
12th April 2014, 14:58
The selection does not affect any data, it is just visualization.
Yes Anda_skoa, you are wright. It is just vusualization.

A click on the name of my column select all cells of this column, and marks them blue. If I have rows on my table, how to tell the code that I just want data from rows 1, 3 and 5 for example?

Many thanks!

anda_skoa
12th April 2014, 20:25
The first argument of the item() method is the row.
Get the item for each cell you are interested in and read its data.

Cheers,
_

Stanfillirenfro
13th April 2014, 10:05
Thanks Anda_Skoa for your reply.

I am using a loop to accees the cells of my column.
I have added the following condition in my loop:


QTableWidgetItem *valueOfMycell;
if(valueOfMyCell->isSelected())
{
}

The output (from qDebug) ist excatly the values I am looking for, but the program crasches down.
When I removed the line 2, I have all values of my culumn, including those deselected.
I now know that the condition of the line 2 is not appropriate, but I am wondering if there is another condition which matches with this case.
I would appreciate any help.

Many thanks in advance.

anda_skoa
13th April 2014, 10:55
The condition looks good, it is more likely that you have an error elsewhere, especially if "the program crashes down" means it unexpectedly terminates.

Alternatively look at QTableWidget::selectedItems() to directly get the selected cells.

Cheers,
_

Stanfillirenfro
13th April 2014, 12:18
Many thanks Anda_Skoa for your help.

I have successfully fixed the problem using QTableWidget::selectedItems() as you have specified.

For me is this problem solved.

Many thanks one more time.