You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, March 19th 2007, 10:30pm

How to check if a row is selected in a table (QT4)

Firstly,

Does anybody know the difference between QTableView and QTableWidget. They say that QTableView is closer to the QTable class of Qt3, but I think that QTableWidget is closer, and this is the one that I am using.

Now, back to my question....

I want to let the user modify the number of rows that a table has, by adding or deleting rows. The first task is straight forward, but how do you do the second one. I tried to use the itemClicked SIGNAL but that did not work. I need to find a way to check which items, and consequently which row(s) are selected.

Thank you for your help,
Vasilis

This post has been edited 1 times, last edit by "vgkanis" (Mar 20th 2007, 2:00pm)


Manivannan_1984

Intermediate

  • "Manivannan_1984" is male

Posts: 305

Location: chennai

Occupation: Software Engineer

  • Send private message

2

Tuesday, March 20th 2007, 6:51am

RE: How to check if a row is selected in a table (QT4)

Try this signal... it will alow you to keep tracking of current item

Quoted

void currentChanged ( int index )
[PHP]

Quoted

U can make anything, but nothing can make U.

3

Tuesday, March 20th 2007, 2:24pm

RE: How to check if a row is selected in a table (QT4)

Quoted

Originally posted by Manivannan_1984
Try this signal... it will alow you to keep tracking of current item

Quoted

void currentChanged ( int index )


I used the

Source code

1
currentRow()


function, and it works. Of course this function works only if you do not alter the selectionMode. I had set it to SingleSelection and it did not work.

4

Monday, March 26th 2007, 11:42pm

RE: How to check if a row is selected in a table (QT4)

The currentRow function has one flaw: You need to actually select the row. If you simple click on a cell, the row is not selected.
Therefore, I am back to my initial problem; How do I know which row is selected?

I know that there is the cellPressed signal, which does a pretty good job. But, it does not work when the table's cells consist of widgets, and this is my case.

5

Wednesday, March 28th 2007, 7:34pm

RE: How to check if a row is selected in a table (QT4)

With the following code

Source code

1
int rows=QTableWidget->currentIndex().row()


I do get the selected row. But, because I am using widgets, I need to manually select the row. The row is not selected if I click on a cell, no matter what the setSelectionBehavior and setSelectionMode are set to.

Is this a bug in QTableWidget?

6

Wednesday, March 28th 2007, 7:55pm

I set

Source code

1
Table->setSelectionBehavior(QAbstractItemView::SelectRows);


and i still can edit any cells with dbl click and keyboard, but selected cell don't see

7

Wednesday, March 28th 2007, 8:06pm

Try

Source code

1
Table->setSelectionMode(QAbstractItemView::SingleSelection )

8

Wednesday, March 28th 2007, 8:17pm

If this ?

Source code

1
int row = Table->row(Table->currentItem());


or this

Source code

1
int row = Table->currentItem()->row();

This post has been edited 1 times, last edit by "SABROG" (Mar 28th 2007, 8:20pm)


9

Wednesday, March 28th 2007, 8:21pm

I think it will work.

But, my problem is that the row is not selected when I click on a cell. In other words, the row is selected only if I clicked on the column header.

This post has been edited 1 times, last edit by "vgkanis" (Mar 28th 2007, 8:22pm)


10

Wednesday, March 28th 2007, 8:23pm

For this you must use:

Source code

1
Table->setSelectionBehavior(QAbstractItemView::SelectRows);


But you can't see what cell you select, but can edit any cell.

11

Wednesday, March 28th 2007, 8:29pm

Correct,
But at the same time, the row is not selected.