Get rid of the stupid frustrating focus rectangle in QTreeWidget
Now what beautiful the life would have been if every widget had a function like this
void setFocusRectangleVisible(bool status)
But no, there is none. So after looking over the entire google, I found this example:
http://qt.nokia.com/developer/faqs/736
which works fine. I took that example and replaced some things in there to use it on my QTreeWidget.
Now I have this
Code:
{
if(element == CE_ItemViewItem)
{
if (btn)
{
if (btn->state & State_HasFocus)
{
btn->state = btn->state ^ State_HasFocus;
}
}
}
else
{
}
}
This is not working as expected because this test is NEVER true
Code:
if (btn->state & State_HasFocus)
Can someone please help me? What am I missing? Is the problem the fact that the focus is not on the item itself, but on the tree widget?
I'm using Qt 4.5
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
isn't it QStyle::State_Selected for items?
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
What if you make a copy of the option ?
Not sure if const is causing some trouble, might be it is :rolleyes:
though I guess it is QStyle::State_hasFocus only, I had used it in delegate to get rid of that focus rect, and it worked for me in the delegate.
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
I've tried wth Selected before posting here, it des not work...
Quote:
Originally Posted by
aamer4yu
What if you make a copy of the option ?
Not sure if const is causing some trouble, might be it is :rolleyes:
though I guess it is QStyle::State_hasFocus only, I had used it in delegate to get rid of that focus rect, and it worked for me in the delegate.
Ok, I've used an item delegate like this:
Code:
{
if (itemOption.
state & QStyle::State_HasFocus) {
itemOption.
state ^= QStyle::State_HasFocus;
}
QStyledItemDelegate::paint(painter, itemOption, index);
}
Now the focus rectangle i gone, BUT the my style sheet is not applied anymore :D
Can you please advise how can I combine those to work together? Thank you :rolleyes:
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Quote:
Originally Posted by
Carlsberg
Now what beautiful the life would have been if every widget had a function like this
void setFocusRectangleVisible(bool status)
But no, there is none.
Code:
treeWidget->setFocusPolicy(Qt::NoFocus);
... or reimplement the delegate (not the style) and ignore the focus flag from QStyleOptionViewItem in the painting routine.
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Are you also using the custom style ?
From the docs of Qt Style Sheets-
Quote:
Warning: Qt style sheets are currently not supported for custom QStyle subclasses. We plan to address this in some future release.
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Quote:
Originally Posted by
wysota
Code:
treeWidget->setFocusPolicy(Qt::NoFocus);
I now that, but that is not good. I want the focus, I just don't want the useless rectangle.
Quote:
Originally Posted by
wysota
... or reimplement the delegate (not the style) and ignore the focus flag from
QStyleOptionViewItem in the painting routine.
I don't understand this. I did this, which seems to be what you said, and the result is the same. The focus rectangle is not shown, but setStyleSheet has no effect anymnore, so the selection is some dark gray
Code:
{
protected:
void paint
(QPainter* painter,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const;
};
{
if (itemOption.
state & QStyle::State_HasFocus) {
qDebug("FOCUS");
itemOption.
state ^= QStyle::State_HasFocus;
}
}
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Code:
Warning
: Qt style sheets are currently not supported
for custom
QStyle subclasses.
We plan to address
this in some future release
Ah, ok, then it's normal behaviour. Now I guess I will have to try to paint everything in the paint() function
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Subclass QStyledItemDelegate and not QItemDelegate. To be honest I'm not very fond of the idea of representing focus with selection. These are two different mechanisms. And as far as I remember you can define the focus rectangle using style sheets as well, you should dig deeper in the style sheet reference.
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Quote:
Originally Posted by
wysota
Subclass
QStyledItemDelegate and not QItemDelegate. To be honest I'm not very fond of the idea of representing focus with selection. These are two different mechanisms.
I have done both, as it can be seen above. The result is identical. Well, having an ugly rectangle over the nice selection is not only just ugly, but also extremely useless. Not to mention that nobody uses the tab key by going through many widgets. In 15 years I used the tab key only once, when the mouse died. And it was sick :cool:
Quote:
And as far as I remember you can define the focus rectangle using style sheets as well, you should dig deeper in the style sheet reference.
I've done that, but found absolutely nothing :(
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Try doing equivalents of these (I mean the selectors):
Code:
QListView::item:selected:!active {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #ABAFE5, stop: 1 #8588B2);
}
QListView::item:selected:active {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #6a6ea9, stop: 1 #888dd9);
}
Manipulating the "border" property might just work here.
1 Attachment(s)
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Ok, I've done it. The solution was to completely rewrite the delegate's paint() function for the following states:
QStyle::State_Selected && QStyle::State_Active
QStyle::State_Selected && !QStyle::State_Active
QStyle::State_MouseOver
and of course for the nothing state
For QStyle::State_MouseOver to be correctly set, you must set the viewport's attribute Qt::WA_Hover. That is a bug
http://bugreports.qt.nokia.com/browse/QTBUG-6063
I also made the background color to cover only the text part of the item. This is the final result, when active and selected plus a hovered item
Attachment 4794
Thank you all for input
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Carlsberg, please... could you send me your solution??
I'm facing the same problem...
thank you very much
Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget
Hey Carlsberg, please send me your solution.
I am fighting the same thing.
Thanks.
GD
Quote:
Originally Posted by
Carlsberg
Ok, I've done it. The solution was to completely rewrite the delegate's paint() function for the following states:
QStyle::State_Selected && QStyle::State_Active
QStyle::State_Selected && !QStyle::State_Active
QStyle::State_MouseOver
and of course for the nothing state
For QStyle::State_MouseOver to be correctly set, you must set the viewport's attribute Qt::WA_Hover. That is a bug
http://bugreports.qt.nokia.com/browse/QTBUG-6063
I also made the background color to cover only the text part of the item. This is the final result, when active and selected plus a hovered item
Attachment 4794
Thank you all for input