PDA

View Full Version : Understanding highlighting in a QTableView



scarleton
11th July 2010, 00:48
How exactly does the whole high lighting in a QTableView work? I actually want it to be more of a multiple column list view, aka not editable with the exception for the first column which is ItemIsUserCheckable. I have the model configured correctly so that only the first column is selectable. It is similar to the code I posted in this thread:

http://www.qtcentre.org/threads/32209-Centering-a-QCheckBox-in-a-TableView?p=149856

But I have tweaked the settings a bit. I am using QT Designer, so here is what is in the generated code:


customerTableView->setObjectName(QString::fromUtf8("customerTableView"));
customerTableView->setEditTriggers(QAbstractItemView::SelectedClicked );
customerTableView->setProperty("showDropIndicator", QVariant(true));
customerTableView->setAlternatingRowColors(true);
customerTableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
customerTableView->setSelectionBehavior(QAbstractItemView::SelectRows );
customerTableView->setSortingEnabled(true);
customerTableView->setCornerButtonEnabled(false);
customerTableView->horizontalHeader()->setProperty("showSortIndicator", QVariant(true));
customerTableView->verticalHeader()->setHighlightSections(false);

The feature I am working on now is allowing the user to select a row which will enable two buttons (delete/edit). What is happening is when I select a row, then click on one of the buttons, the row that was highlighted isn't highlighted anymore. Also, I don't know how to get a signal or event on when the row is no longer highlighted, so the buttons can be disabled.

Question:

How to keep the row selected when clicking on a widget outside the tree?
How to detect when no row is highlighted, so button state can change correctly?


Sam

Lykurg
11th July 2010, 00:57
1. Prepare a minimal compilable example. It should not happen. (Also note that the color can change since your view wont have the focus any more)
2. See the selection model of your view! It provides signals which informs you when the selection changes.

scarleton
11th July 2010, 01:41
When you say "It should not happen", what should not happen?

The response to #2 drives me NUTS! I spend a lot of time searching for answers before I post a question so the response of "See the selection model of your view! It provides signals which informs you when the selection changes." ain't doing me any good. I just ain't all that smart, I ain't go no idea where to go lookin for the correct singles. Is there any way someone might be so kind as to tell me the actual signal name and where I will find that signal? Is it on the model, the view or on one of the children of the model/view?

Sam

scarleton
11th July 2010, 02:23
1. Prepare a minimal compilable example.Well, considering I am using a database as a data source, minimal is a bit hard. I took the querymodel example and added to it a bit:

4892

Once this code is running, click on a row, then go click on one of the buttons at the bottom, the row is no longer highlighted.

Sam

scarleton
11th July 2010, 04:44
Ok, after even more digging, I finally found the chapter in the Qt help called Handling Selections in Item Views (http://doc.qt.nokia.com/4.6/model-view-selection.html). I now understand what you mean by selection model. Until I found this article, I didn't realize that Qt had a secondary type of model, the selection model.

I am still completely mystified at why the selected cells are not very visible. The attached example is similar from the early code except it is multi select. If you selected a number of rows, then go click the button at the bottom, the highlighted rows go from blue to gray, since the alternatingRowColors is true, you cannot really tell the difference between the selected rows and the alternate row color.

Here is the new code: 4893

Sam

Lykurg
11th July 2010, 07:26
When you say "It should not happen", what should not happen? I mean, that the selection goes away if you click the button.


The response to #2 drives me NUTS! I spend a lot of time searching for answers before I post a question so the response of "See the selection model of your view! It provides signals which informs you when the selection changes." ain't doing me any good. I just ain't all that smart, I ain't go no idea where to go lookin for the correct singles. Is there any way someone might be so kind as to tell me the actual signal name and where I will find that signal? Is it on the model, the view or on one of the children of the model/view? Obviously you find the answer yourself and you can't expect that everything is provided for you. Because to solve it, you only have to look in the documentation, an no offense, if you can't do that it will be hard for you to work with Qt at all. For the future, just look in the "List of all members, including inherited members" and search for the catch word you are given, so you would have come to: QTableView -> QAbstractItemView::selectionModel -> QItemSelectionModel. But your site is even better.


the highlighted rows go from blue to gray, since the alternatingRowColors is true,
Here on my mac I can't reproduce your problem. Please check, if alternating row color is false, the item is also turning gray. This is what I expect that it will do. If so, it ist because - as mentioned - the widget looses its focus. If you don't want that color change (even if the focus change) you have to alter e.g. the palette of the table view or if it should be systemwide change your application style.

scarleton
11th July 2010, 14:01
Obviously you find the answer yourself and you can't expect that everything is provided for you. Because to solve it, you only have to look in the documentation, an no offense, if you can't do that it will be hard for you to work with Qt at all. For the future, just look in the "List of all members, including inherited members" and search for the catch word you are given, so you would have come to: QTableView -> QAbstractItemView::selectionModel -> QItemSelectionModel. But your site is even better.I take no offense and cannot agree more with the statement that you have to learn to find things in the Qt documentation. Actually I would take that one step farther and say to be a competent programmer, period, you need to master finding things in documentation. That is the hardest part.

The only thing I would ask of you is to be a touch more verbose in your replies sometimes. If I had known there was such a thing as secondary model for selection, your post would have been all I needed (if I had realized there was such a thing as a selection model, I would have not asked the question). But I didn't know, so your post was of little service. My goal is not to beat you up, nor to blame you, fore I always appreciate folks that are willing to take the time to reply and help out others. I am simply trying to provide some feedback so you can make the most of your time:)

Here on my mac I can't reproduce your problem. Please check, if alternating row color is false, the item is also turning gray. This is what I expect that it will do. If so, it ist because - as mentioned - the widget looses its focus. If you don't want that color change (even if the focus change) you have to alter e.g. the palette of the table view or if it should be systemwide change your application style.Yea, once I posted the last reply, I started to think it might be a styling issue.

Sam

Lykurg
11th July 2010, 14:32
The only thing I would ask of you is to be a touch more verbose in your replies sometimes.
Normaly I am, but it was 1 am and I was tired. Further you posted on the "Qt Programming" forum where we suppose that little hints are enough. In the newbie section (which we read and answer as often as the programming forum) we explain more.

But since everything is clear now, all is fine.


Happy coding with Qt!