Results 1 to 3 of 3

Thread: highlighting hovered QListWidget item

  1. #1
    Join Date
    Jul 2012
    Posts
    4

    Default highlighting hovered QListWidget item

    Me again.

    I have QListWidget where I want to highlight hovered item. The problem is, when I try to do so using CSS, it doesn't work - background color isn't changing on hover. My code:
    Qt Code:
    1. QStringList files = Settings->recentFiles();
    2. int numRecentFiles = qMin(files.size(), 5);
    3. for (int i = 0; i < numRecentFiles; i++)
    4. {
    5. listWidget->addItem(new QListWidgetItem(files[i]));
    6. }
    7.  
    8. listWidget->setMouseTracking(true);
    9. listWidget->setStyleSheet("QListWidget::item:hover {background-color:yellow;}");
    To copy to clipboard, switch view to plain text mode 

    I've also tried to succeed with signal-slot mechanism and I must say, almost did it. The problem is, when cursor leaves the widget, highlight stays at the last hovered element, because I don't know how to use the 'viewport left event'. I would be grateful if someone could provide me some example. (lol, I ended this sentence with semicolon and pushed Ctrl+S).

    Thanks in advance

    EDIT: Well, right after posting I answered myself the first question. Instead of
    Qt Code:
    1. listWidget->setStyleSheet("QListWidget::item:hover {background-color:yellow;}");
    To copy to clipboard, switch view to plain text mode 
    I should write
    Qt Code:
    1. this->setStyleSheet("QListWidget::item:hover {background-color:yellow;}");
    To copy to clipboard, switch view to plain text mode 
    but I don't like the effect.

    Second question still active though.
    Last edited by joval; 28th July 2012 at 19:23.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: highlighting hovered QListWidget item

    this example shows 'background' not 'background-color'
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jul 2012
    Posts
    4

    Default Re: highlighting hovered QListWidget item

    Quote Originally Posted by amleto View Post
    this example shows 'background' not 'background-color'
    For a single attribute, it is recommended to use specified names.


    I managed to achieve the wished effect. Firstly, I was not satisfied with CSS hovering because background wasn't expanding to margins, so I changed margin to padding and now it's ok.

    Problem solved (I still don't know how to use viewport, but it's a thing for another story )

Similar Threads

  1. Replies: 2
    Last Post: 17th September 2011, 03:40
  2. Replies: 2
    Last Post: 1st April 2011, 09:32
  3. QTableWidget || QListWIdget with syntax highlighting
    By ericV in forum Qt Programming
    Replies: 7
    Last Post: 21st August 2009, 13:26
  4. QListWidget Item
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2008, 09:02
  5. Reg - add item in QListWidget
    By suresh in forum Newbie
    Replies: 1
    Last Post: 1st September 2006, 17: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
  •  
Qt is a trademark of The Qt Company.