Results 1 to 3 of 3

Thread: [QCombobox] Display decorated value

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default [QCombobox] Display decorated value

    I have a combobox with several predefined values and a one custom item that allows manually enter the value in the edit field of the combobox. When user enters/edits the value, I'd like to show that value as is. But when editing is finished to display the decorated value (e.g. "123 Kb" instead of "123" entered by a user). What is the best way to achieve this? I've tried to use QStyledItemDelegate overriding displayText method, but this doesn't seem to work, because the item in the drop down list should be still called "custom", only edit field value should be decorated.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [QCombobox] Display decorated value

    Try setting custom line edit for the combo box, something like that:
    Qt Code:
    1. class MyLineEdit : public QLineEdit
    2. {
    3. public:
    4. MyLineEdit( QWidget* parent = 0 ) : QLineEdit( parent ) {}
    5.  
    6. void focusInEvent( QFocusEvent* e )
    7. {
    8. this->setText( value );
    9. QLineEdit::focusInEvent( e );
    10. }
    11.  
    12. void focusOutEvent( QFocusEvent* e )
    13. {
    14. this->value = this->text();
    15. if( !this->value.isEmpty() )
    16. {
    17. this->setText( this->value + " Kb" );
    18. }
    19. QLineEdit::focusOutEvent( e );
    20. }
    21.  
    22. private:
    23. QString value;
    24. };
    25.  
    26. combobox->setLineEdit( new MyLineEdit( this ) );
    To copy to clipboard, switch view to plain text mode 
    This doesn't take into account text set via setText() but it's just an (working) example.

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: [QCombobox] Display decorated value

    Thanks, I've ended up with similar approach, but I worked with combobox only and didn't set custom line edit or subclass it. I just thought maybe it was possible with data roles or delegates, but it seems like it's not.

Similar Threads

  1. QMenu display with delay. Or not display else
    By stepchik in forum Newbie
    Replies: 1
    Last Post: 10th September 2013, 15:06
  2. Replies: 0
    Last Post: 1st September 2011, 15:20
  3. Replies: 12
    Last Post: 1st May 2011, 11:38
  4. custom display in a QComboBox
    By scarleton in forum Qt Programming
    Replies: 2
    Last Post: 3rd September 2010, 22:13
  5. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29

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.