Results 1 to 7 of 7

Thread: Highlight an item in QCombobox by text color

  1. #1
    Join Date
    Nov 2010
    Posts
    39
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Highlight an item in QCombobox by text color

    I have QCombobox, QStandardItemModel is used as a data model for it, every item in QCombobox it is a QStandardItem respectively, QTreeView is used as view. I try to select some (parent) combobox items by blue color like this:
    Qt Code:
    1. QStandardItem* all = new QStandardItem("some text");
    2. all->setForeground(QBrush(QColor(Qt::blue)));
    To copy to clipboard, switch view to plain text mode 
    or like this:
    Qt Code:
    1. all->setData(Qt::blue, Qt::ForegroundRole);
    To copy to clipboard, switch view to plain text mode 
    In drop down list (tree) that element (item) is blue (highlighted), however when I select that item its text becomes black again which is default for all the items in the combobox. How can I fix that to keep desired item color when I select it?

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Highlight an item in QCombobox by text color

    Try creating and setting a custom item delegate. The way selections are drawn by the delegate, and it is there that it is decided how to draw the selected item.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Nov 2010
    Posts
    39
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Highlight an item in QCombobox by text color

    I looked at delegates, however have not found how to draw selected item. Do You have any example link?

  4. #4
    Join Date
    Nov 2010
    Posts
    39
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Highlight an item in QCombobox by text color

    Seems delegate does not solve the problem:
    http://www.qtforum.org/article/26686...rent-item.html

  5. #5
    Join Date
    Nov 2010
    Posts
    39
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Highlight an item in QCombobox by text color

    Have found the solution after writing bug report to nokia. Solution: connect signal currentIndexChanged(int) of the combobox (signal activated(QModelIndex) from QTreeView was not emitted for some reason) and process it in a slot to change parent items color and font when selected:
    Qt Code:
    1. connect(myCombobox, SIGNAL(currentIndexChanged(int)), myCombobox, SLOT(setupComboboxFont()));
    To copy to clipboard, switch view to plain text mode 
    in slot we write:
    Qt Code:
    1. void KComboboxBox::setupComboboxFont()
    2. {
    3. QFont comboboxFont = font();
    4. QPalette comboboxPalette = palette();
    5. QModelIndex index = ((QTreeView *)view())->currentIndex();
    6. if(!index.parent().isValid()) // parent index
    7. {
    8. comboboxFont.setBold(true);
    9. comboboxPalette.setColor(QPalette::Text, Qt::blue);
    10. comboboxPalette.setColor(QPalette::WindowText, Qt::blue);
    11. }
    12. else
    13. {
    14. comboboxFont.setBold(false);
    15. if(parentWidget() != 0)
    16. comboboxPalette = parentWidget()->palette();
    17. }
    18. setFont(comboboxFont);
    19. setPalette(comboboxPalette);
    20. }
    To copy to clipboard, switch view to plain text mode 
    That is all!!! Workable!

  6. #6
    Join Date
    Apr 2017
    Posts
    43
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Highlight an item in QCombobox by text color

    Quote Originally Posted by AlekseyK View Post
    Have found the solution after writing bug report to nokia. Solution: connect signal currentIndexChanged(int) of the combobox (signal activated(QModelIndex) from QTreeView was not emitted for some reason) and process it in a slot to change parent items color and font when selected:
    Qt Code:
    1. connect(myCombobox, SIGNAL(currentIndexChanged(int)), myCombobox, SLOT(setupComboboxFont()));
    To copy to clipboard, switch view to plain text mode 
    in slot we write:
    Qt Code:
    1. void KComboboxBox::setupComboboxFont()
    2. {
    3. QFont comboboxFont = font();
    4. QPalette comboboxPalette = palette();
    5. QModelIndex index = ((QTreeView *)view())->currentIndex();
    6. if(!index.parent().isValid()) // parent index
    7. {
    8. comboboxFont.setBold(true);
    9. comboboxPalette.setColor(QPalette::Text, Qt::blue);
    10. comboboxPalette.setColor(QPalette::WindowText, Qt::blue);
    11. }
    12. else
    13. {
    14. comboboxFont.setBold(false);
    15. if(parentWidget() != 0)
    16. comboboxPalette = parentWidget()->palette();
    17. }
    18. setFont(comboboxFont);
    19. setPalette(comboboxPalette);
    20. }
    To copy to clipboard, switch view to plain text mode 
    That is all!!! Workable!
    In Line 5, I get a compiler error for view() are you casting view() to a (QTreeView *)

    error: 'view' was not declared in this scope
    QModelIndex index = ((QTreeView *)view())->currentIndex();

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,323
    Thanks
    36
    Thanked 1,526 Times in 1,477 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Highlight an item in QCombobox by text color

    Quote Originally Posted by emp1953 View Post
    In Line 5, I get a compiler error for view() are you casting view() to a (QTreeView *)

    error: 'view' was not declared in this scope
    QModelIndex index = ((QTreeView *)view())->currentIndex();
    Well, view() is a valid method in a QComboBox subclass, so maybe you are not inside such a class?

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 28th April 2011, 14:43
  2. Set text color for menu item
    By stella1016 in forum Qt Programming
    Replies: 6
    Last Post: 16th March 2010, 10:27
  3. Menu Item has no highlight
    By blackfox in forum Qt Programming
    Replies: 6
    Last Post: 24th September 2008, 12:17
  4. Setting a highlight text color for QTableWidgetItem
    By Hiba in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2007, 10:51
  5. Replies: 1
    Last Post: 17th March 2006, 08:19

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.