Results 1 to 14 of 14

Thread: Get rid of the stupid frustrating focus rectangle in QTreeWidget

  1. #1
    Join Date
    May 2009
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

    Qt Code:
    1. void Style::drawControl(ControlElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget ) const
    2. {
    3. if(element == CE_ItemViewItem)
    4. {
    5. const QStyleOptionViewItem *b = qstyleoption_cast<const QStyleOptionViewItem *>(option);
    6.  
    7. if (btn)
    8. {
    9. if (btn->state & State_HasFocus)
    10. {
    11. btn->state = btn->state ^ State_HasFocus;
    12. }
    13. }
    14.  
    15. QWindowsStyle::drawControl(element, btn, painter, widget);
    16.  
    17. }
    18. else
    19. {
    20. QWindowsStyle::drawControl(element, option, painter, widget);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    This is not working as expected because this test is NEVER true

    Qt Code:
    1. if (btn->state & State_HasFocus)
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget

    isn't it QStyle::State_Selected for items?

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

    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.

  4. #4
    Join Date
    May 2009
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default 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 View Post
    What if you make a copy of the option ?
    Not sure if const is causing some trouble, might be it is

    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:

    Qt Code:
    1. void MyStupidItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
    2. {
    3. QStyleOptionViewItem itemOption(option);
    4.  
    5. if (itemOption.state & QStyle::State_HasFocus)
    6. {
    7. itemOption.state ^= QStyle::State_HasFocus;
    8. }
    9.  
    10. QStyledItemDelegate::paint(painter, itemOption, index);
    11. }
    To copy to clipboard, switch view to plain text mode 

    Now the focus rectangle i gone, BUT the my style sheet is not applied anymore

    Can you please advise how can I combine those to work together? Thank you

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget

    Quote Originally Posted by Carlsberg View Post
    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.
    Qt Code:
    1. treeWidget->setFocusPolicy(Qt::NoFocus);
    To copy to clipboard, switch view to plain text mode 

    ... or reimplement the delegate (not the style) and ignore the focus flag from QStyleOptionViewItem in the painting routine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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-
    Warning: Qt style sheets are currently not supported for custom QStyle subclasses. We plan to address this in some future release.

  7. #7
    Join Date
    May 2009
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. treeWidget->setFocusPolicy(Qt::NoFocus);
    To copy to clipboard, switch view to plain text mode 
    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 View Post
    ... 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

    Qt Code:
    1. class MyDelegate : public QItemDelegate
    2. {
    3. protected:
    4. void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
    5. };
    6.  
    7. void MyDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    8. {
    9. QStyleOptionViewItem itemOption(option);
    10.  
    11. if (itemOption.state & QStyle::State_HasFocus)
    12. {
    13. qDebug("FOCUS");
    14. itemOption.state ^= QStyle::State_HasFocus;
    15. }
    16.  
    17. QItemDelegate::paint(painter, itemOption, index);
    18. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    May 2009
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget

    Qt Code:
    1. Warning: Qt style sheets are currently not supported for custom QStyle subclasses. We plan to address this in some future release
    To copy to clipboard, switch view to plain text mode 
    Ah, ok, then it's normal behaviour. Now I guess I will have to try to paint everything in the paint() function

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    May 2009
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget

    Quote Originally Posted by wysota View Post
    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

    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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get rid of the stupid frustrating focus rectangle in QTreeWidget

    Try doing equivalents of these (I mean the selectors):
    css Code:
    1. QListView::item:selected:!active {
    2. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
    3. stop: 0 #ABAFE5, stop: 1 #8588B2);
    4. }
    5.  
    6. QListView::item:selected:active {
    7. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
    8. stop: 0 #6a6ea9, stop: 1 #888dd9);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Manipulating the "border" property might just work here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    May 2009
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

    CustomTreeWidget&#4.png

    Thank you all for input

  13. #13
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default 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

  14. #14
    Join Date
    Aug 2017
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default 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 View Post
    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

    CustomTreeWidget&#4.png

    Thank you all for input

Similar Threads

  1. Replies: 1
    Last Post: 3rd November 2009, 23:26
  2. Hide the focus rectangle of a QGraphicsLineItem
    By ronno in forum Qt Programming
    Replies: 2
    Last Post: 16th June 2009, 11:47
  3. Replies: 1
    Last Post: 18th November 2008, 07:57
  4. Preventing blue focus rectangle
    By MrGarbage in forum Qt Programming
    Replies: 2
    Last Post: 21st December 2007, 16:36
  5. QTreeWidget selection/focus
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 3rd October 2007, 17:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.