Results 1 to 1 of 1

Thread: Styling a QListView with custom widgets

  1. #1
    Join Date
    Sep 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Angry Styling a QListView with custom widgets

    Hello,

    I am trying to customize the look of my application using stylesheets, which has proven to be rather challenging given my loathing of CSS. I would very much appreciate if someone with more experience could help me shed some light on this.

    I have a QListView and I want to have a hovering effect for its items. In the constructor of the widget that contains that QListView (in this case, called groupListView), I do this:

    Qt Code:
    1. ui->groupsListView->setObjectName(QString("GroupsListView"));
    2. ui->groupsListView->setStyleSheet(QString("QListView#GroupsListView::item:hover { background: #000000; }"));
    To copy to clipboard, switch view to plain text mode 

    Now, the QListView has an item delegate, which builds and displays a custom widget. The paint event of the custom widget looks like this:

    Qt Code:
    1. QPainter painter(this);
    2. opt.init(this);
    3. style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
    4. painter.setRenderHint(QPainter::Antialiasing, true);
    To copy to clipboard, switch view to plain text mode 

    However, when I hover my mouse over the custom widget, nothing happens.

    Is that because I have a custom widget? I can't style anything in the item itself.

    If so, is there anything special I should pay attention to in the custom widget code? I have also tried to achieve this from the custom widgets' stylesheet, by setting the widget's stylesheet to something like this:

    Qt Code:
    1. QWidget#GroupMeterListWidget {
    2. color: #ffffff;
    3. border: 1px solid black;
    4. border-radius: 3px;
    5. padding: 1px;
    6. background: qlineargradient(
    7. x1:0, y1:0, x2:0, y2:1,
    8. stop:0 #bec1d2,
    9. stop: 0.4 #717990,
    10. stop: 0.5 #5c637d,
    11. stop:1 #68778e);
    12. }
    13.  
    14. :hover{
    15. color: #ffffff;
    16. border: 1px solid black;
    17. border-radius: 3px;
    18. padding: 1px;
    19. background: qlineargradient(
    20. x1:0, y1:0, x2:0, y2:1,
    21. stop:0 #ced1e2,
    22. stop: 0.4 #8189a0,
    23. stop: 0.5 #6c738d,
    24. stop:1 #78879e);
    25. }
    To copy to clipboard, switch view to plain text mode 

    (please ignore the horribly-looking colors, I just tried to make them noticeable).

    This actually works in Qt Designer, but if I try to set it in code, in the constructor of the custom widget, the first part works fine, but -- again -- when I hover over the widget, nothing happens. It seems like they would not trigger the hover event -- for the sake of it, I tried to override enterEvent and leaveEvent and they did not seem to fire.

    I dug through the Qt docs for some indication about this but I haven't been able to find anything relevant. Can anyone give me a quick idea about where to look?

    Thanks!

    Edit: I just noticed it slipped my mind -- I am using Qt version 4.7.4 on a Linux machine with gcc 4.5.1. It's from the official Fedora 14 repositories, if it's of any importance, but I have access at pretty much any version (this one is on one of my work computers which it would appear I have been rather lazy to maintain ).
    Last edited by ALz; 27th September 2012 at 12:47.

Similar Threads

  1. Replies: 1
    Last Post: 18th July 2012, 09:59
  2. Replies: 0
    Last Post: 30th April 2012, 15:17
  3. QListView and custom Widgets
    By Spooky in forum Qt Programming
    Replies: 7
    Last Post: 17th May 2011, 17:45
  4. QListView and custom widgets in each cell
    By T4ng10r in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2009, 10:14
  5. Replies: 0
    Last Post: 15th May 2009, 15:38

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.