View Full Version : QTableView, no selection rectangle
alisami
24th December 2008, 08:17
Hi,
Is it possible to hide the selection rectangle for the table view items when the item is highlighted and selected ?
I mean what I want is to select the cell and paint its background but I don't want to see the dotted selection rectangle around the cell.
Sami
spirit
24th December 2008, 08:26
try this
...
setSelectionMode(QAbstractItemView::NoSelection);
...
or you can implement your own delegate and do not draw selection.
alisami
24th December 2008, 09:25
Actually what I want is to select a row when I click on a cell, and I'm using setSelectionMode(QAbstractItemView::SingleSelectio n) and setSelectionBehavior(QAbstractItemView::SelectRows ).
What I don't want is the dotted rectangle displayed when I click on a cell. It can be easily seen when selection mode is set to `NoSelection`. It does not selects the cell, but the active cell is shown with this rectangle.
spirit
24th December 2008, 09:39
now it's clear. so, you need to implement your own delegate and don't draw dotten focus rect.
jpn
24th December 2008, 10:54
view->setFocusPolicy(Qt::NoFocus);
spirit
24th December 2008, 11:04
view->setFocusPolicy(Qt::NoFocus);
what about keyboard navigation in this case? it will not work.
jpn
24th December 2008, 11:19
What's the point of having focus if he wants to hide it? :)
spirit
24th December 2008, 13:58
that's true. :) but I think he wants to have keyboard navigation I think. :)
alisami
24th December 2008, 14:17
What's the point of having focus if he wants to hide it? :)
that will do the trick but there is a minor bug ( I guess this is not a feature :) ), when focus is deactivated, when a cell is double clicked for editing, after the editing, the focus rectangle becomes active again.
setting the item flags to un-editable mode fixes that problem.
is there a way to set the table as read-only so that none of the items are editable?
alisami
24th December 2008, 14:18
that's true. :) but I think he wants to have keyboard navigation I think. :)
Yeah actually I may need keyboard navigation. But I guess I can implement keyboard inputs to change the selected rows, without using the focus.
spirit
24th December 2008, 14:54
do you think this is bet solution to implement navigation of treeView which is already works fine insted of creating custom delegate and write several lines of code? I don't thinks so.
alisami
25th December 2008, 14:40
do you think this is bet solution to implement navigation of treeView which is already works fine insted of creating custom delegate and write several lines of code? I don't thinks so.
can you describe which part I have to reimplement so that I can lose the focus rectangle?
the main reason why I want to do such things is that, I want to implement an outlook-like message list, and I think the best way is to use QTableWidget. do I proceed in the right direction or do you have any other suggestion ?
spirit
25th December 2008, 15:03
example
class MyDelegate: public QItemDelegate
{
Q_OBJECT
public:
virtual void drawFocus ( QPainter * /*painter*/, const QStyleOptionViewItem & /*option*/, const QRect & /*rect*/ ) const {}
};
...
QTableWidget *table = new QTableWidget(10, 10);
table->setItemDelegate(new MyDelegate());
for (int row = 0; row < 10; ++row) {
for (int col = 0; col < 10; ++col) {
QTableWidgetItem *item = new QTableWidgetItem();
table->setItem(row, col, item);
}
}
...
alisami
25th December 2008, 19:50
example
class MyDelegate: public QItemDelegate
{
Q_OBJECT
public:
virtual void drawFocus ( QPainter * /*painter*/, const QStyleOptionViewItem & /*option*/, const QRect & /*rect*/ ) const {}
};
...
QTableWidget *table = new QTableWidget(10, 10);
table->setItemDelegate(new MyDelegate());
for (int row = 0; row < 10; ++row) {
for (int col = 0; col < 10; ++col) {
QTableWidgetItem *item = new QTableWidgetItem();
table->setItem(row, col, item);
}
}
...
Thanks a lot Spirit, for not giving up on me :)
I finally got what I need.
Sami
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.