Results 1 to 3 of 3

Thread: Styling a QAbstractItemView item

  1. #1
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Styling a QAbstractItemView item

    I am attempting to style the items in QCompleter's dropdown via .qss

    In my .qss file styles I declare in

    Qt Code:
    1.  
    2. }
    To copy to clipboard, switch view to plain text mode 

    work however styles in

    Qt Code:
    1.  
    2. }
    To copy to clipboard, switch view to plain text mode 

    are completely ignored. In particular I am trying to increase spacing between items, does anybody know how to do that?

  2. #2
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Styling a QAbstractItemView item

    Ok I came back to this problem and found the problem & solution! So I thought I would share because I came across other people posting similar problems but never anybody posting a solution.

    QCompleter sets a custom QAbstractItemDelegate on it's model and unfortunately this custom item delegate does not inherit QStyledItemDelegate but simply QItemDelegate (and then overrides the paintmethod to show the selected state).

    So in order to use the stylesheet ::item subcontrol with QCompleter you need to do something like this

    Qt Code:
    1. mCompleterItemDelegate = new QStyledItemDelegate(this);
    2.  
    3. ...
    4.  
    5. mCompleter.setModel(someitemmodel);
    6. mCompleter.popup()->setItemDelegate(mCompleterItemDelegate); //Must be set after every time the model is set
    To copy to clipboard, switch view to plain text mode 

    Now the items in the QCompleter popup list can be styled via standard stylesheet syntax

    Qt Code:
    1. QListView::item {
    2. margin: 2px;
    3. }
    4.  
    5. QListView::item:selected {
    6. background: orange;
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to Berryblue031 for this useful post:

    Kamandol (24th March 2011), thru (29th March 2016)

  4. #3
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Styling a QAbstractItemView item

    Many thanks for sharing the solution!

    It's a shame that isn't possible at the moment to make it work from Designer's .ui forms. Having to set the StyledItemDelegate for every QComboBox(in my case) right after calling the form setupUi() method is quite painful...but better than nothing .

Similar Threads

  1. Replies: 0
    Last Post: 12th October 2010, 05:22
  2. Replies: 5
    Last Post: 2nd April 2010, 21:26
  3. Replies: 3
    Last Post: 26th November 2009, 18:28
  4. Styling each item in a QComboBox
    By rishid in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2008, 09:10
  5. QStyle not styling
    By irudkin in forum Qt Programming
    Replies: 0
    Last Post: 25th April 2006, 10:52

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.