PDA

View Full Version : QTableView and marking a cell exclusively



hml
7th June 2010, 14:48
Hello,

I have a QTableView with each cell being selectable individually, 4 columns and a runtime number of rows.
I wish to be able to mark 1 of the rows (exclusively of all others), in column 4.

I suppose there are different ways to do that, but let's say right clicking on the cell marks that cell (exclusively).
The marked cell then should show the string "Implied"


How do I change the QTableView to handle the righ-click command?
In terms of implementation, is there an easier way than "right-click" ?
If T is the data type for those cells in the data model, that is currently translated to Qstring before display, would I add a member variable to my instance of QAbstractTableModel to hold which cell is marked?


Regards,

tbscope
7th June 2010, 15:58
Look into delegates

hml
9th June 2010, 15:08
I already have a delegate.
I'd appreciate some pointers as to which part of the delegate will help with this?

rds,

tbscope
9th June 2010, 19:15
In your view, you handle the right mouse click.
Keep track of the currently exclusive index.
When right clicking, check if the currently exclusive index = the new exclusive index. If not, set it and reset the other.
Use setData of your model and a custom role for example.

The delegate looks at the custom role and paints a different background.

hml
12th June 2010, 00:08
I'm trying to pick the right button mouse click event, so I connected the
clicked(const QModelIndex &) signal to a private slot to my view.
In my slot, I have:

const Qt::MouseButtons mb = QApplication::mouseButtons() ;
if (mb.testFlag(Qt::LeftButton))
std::cout<< "LeftButton clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;
if (mb.testFlag(Qt::RightButton))
std::cout<< "Right button clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;
if (mb.testFlag(Qt::MidButton))
std::cout<< "MidButton clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;
if (mb.testFlag(Qt::XButton1))
std::cout<< "XButton1 clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;
if (mb.testFlag(Qt::XButton2))
std::cout<< "XButton2 clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;


I click on the cells in the table view and my slot is called, however, none of the flags are set.

Is there a different way to find out which mouse button was clicked?

hml
12th June 2010, 00:24
I found another way: I've implemented the contextMenuEvent and that works

wysota
12th June 2010, 02:32
I found another way: I've implemented the contextMenuEvent and that works

That's not a good idea because a context menu event can be triggered also by means other than right clicking a cell. A better choice would be to use mousePressEvent() or mouseReleaseEvent() for that. Just remember to call the base class implementation when it makes sense to avoid loosing existing functionality.

But I would probably do it all in the delegate by overriding editorEvent(). I would also store the mark ("implied") in the view and not in the model if it makes sense that you have two views of the same model with different implications in each of them.

Also, don't use QApplication::mouseButtons(). Use QMouseEvent::button() instead.