Results 1 to 7 of 7

Thread: Highlight an item in QCombobox by text color

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2017
    Posts
    55
    Thanks
    8
    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();

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 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.