Results 1 to 4 of 4

Thread: Do not paint selection in QStyledItemDelegate

  1. #1
    Join Date
    Aug 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Do not paint selection in QStyledItemDelegate

    We have a very complicated custom coloring scheme for a delegate in a QTableWidget. We do need the SelectionModel though, so setSelectionMode(QAbstractItemView::NoSelection) is not an option.

    I tried to overwrite the paint function like this:

    Qt Code:
    1. void LineEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QStyleOptionViewItemV4 noSelectionOption(option);
    4. initStyleOption(&noSelectionOption, index);
    5. noSelectionOption.showDecorationSelected = false;
    6.  
    7. QStyledItemDelegate::paint(painter, noSelectionOption, index);
    8. }
    To copy to clipboard, switch view to plain text mode 

    But the Texts in the cells are still drawn with blue background and white foreground.

    How can I fix this?

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

    Default Re: Do not paint selection in QStyledItemDelegate

    Remove QStyle::State_Selected from QStyleOption::state
    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.


  3. #3
    Join Date
    Aug 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Do not paint selection in QStyledItemDelegate

    Added one line of code as to your suggestion and it works like a charm now.

    Qt Code:
    1. void LineEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QStyleOptionViewItemV4 noSelectionOption(option);
    4. initStyleOption(&noSelectionOption, index);
    5. noSelectionOption.showDecorationSelected = false;
    6. noSelectionOption.state &= ~QStyle::State_Selected;
    7.  
    8. QStyledItemDelegate::paint(painter, noSelectionOption, index);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot!

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

    Default Re: Do not paint selection in QStyledItemDelegate

    FYI, this would have been enough:

    Qt Code:
    1. void LineEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QStyleOptionViewItem noSelectionOption(option);
    4. noSelectionOption.state &= ~QStyle::State_Selected;
    5. QStyledItemDelegate::paint(painter, noSelectionOption, index);
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.


Similar Threads

  1. Paint selection in QStyledItemDelegate's subclass
    By woodtluk in forum Qt Programming
    Replies: 2
    Last Post: 26th April 2012, 15:51
  2. Replies: 2
    Last Post: 17th July 2011, 14:14
  3. Custom QStyledItemDelegate paint function never called
    By mattsnowboard in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2011, 01:57
  4. Custom QStyledItemDelegate
    By Berryblue031 in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2011, 10:32
  5. How to paint a selection rectangle on a pixmap?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 8th January 2006, 19:52

Tags for this Thread

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.