Results 1 to 10 of 10

Thread: QListView stylesheet problem

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QListView stylesheet problem

    Hi

    I'm trying to use a stylesheet on QListView and almost got it working except for a couple of things which I can't get to work.

    Here is the style sheet that works:

    Qt Code:
    1. QString stylesheet = "";
    2. // Hover effect:
    3. stylesheet += "QListView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #FAFBFE, stop: 1 #DCDEF1) }";
    4. // Selected item gradient:
    5. stylesheet += "QListView::item:selected:active { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6a6ea9, stop: 1 #888dd9); }";
    6. // The padding of items in the list:
    7. stylesheet += "QListView::item { padding: 5px 1px 5px 1px;}";
    8. d->verticalModeList->setStyleSheet(stylesheet);
    To copy to clipboard, switch view to plain text mode 

    The two things I don't get:
    1) Setting the background of the view does not work. When I add the following the background changes but all the other effects (hover etc.) breaks and does not work anymore. I set the background like this:
    Qt Code:
    1. stylesheet += "background-color: #EEEEEE;";
    To copy to clipboard, switch view to plain text mode 

    2) I want to get rid of the border around the text of the selected item in the list view. I've attempted to find this border by making it yellow and once I find it I will remove it, however the following creates a yellow border around the whole item.

    Qt Code:
    1. // The text underneath the selected item:
    2. stylesheet += "QListView::item::text { border-style: dot-dash; border-width: 5px 1px 5px 1px; border-color: yellow; }";
    To copy to clipboard, switch view to plain text mode 


    Sorry for not posting pictures but don't have a place to host them.
    Cheers,
    Jaco


    Added after 1 3 minutes:


    Just to clarify, d->verticalModeList is a QListWidget in my previous post.
    Last edited by JPNaude; 12th November 2010 at 12:19.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListView stylesheet problem

    Qt Code:
    1. stylesheet += "background-color: #EEEEEE;";
    To copy to clipboard, switch view to plain text mode 
    This will just append this string to the styleSheet string, and its not what you want.
    You want to change the stylesheet you have, if I understand correctly.
    For that you need to parametrize your string with substrings, change the substring, update the main stylesheet string, and set it again.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QListView stylesheet problem

    Hmmm maybe my post was a bit confusing. I want to combine the hover effect with a custom background color.

    Thus I set the style sheet on the QListView as follows:

    Qt Code:
    1. QString stylesheet = "";
    2. // I want a colored background:
    3. stylesheet += "background-color: #EEEEEE;";
    4. // Hover effect:
    5. stylesheet += "QListView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #FAFBFE, stop: 1 #DCDEF1) }";
    6. // Selected item gradient:
    7. stylesheet += "QListView::item:selected:active { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6a6ea9, stop: 1 #888dd9); }";
    8. // The padding of items in the list:
    9. stylesheet += "QListView::item { padding: 5px 1px 5px 1px;}";
    10. // The text underneath the selected item:
    11. stylesheet += "QListView::item::text { border-style: dot-dash; border-width: 5px 1px 5px 1px; border-color: yellow; }";
    12. d->verticalModeList->setStyleSheet(stylesheet);
    To copy to clipboard, switch view to plain text mode 

    The thing is, when I remove the background color part, the hovering etc. works. However when I combine it with the other effects, none of the other effects (like hovering etc.) work.

    Thanks for your time,
    Cheers
    Jaco

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListView stylesheet problem

    No, you post was clear.
    But my probably was not.
    Here is a bit of pseudo code to explain what I meant:
    Qt Code:
    1. QSring MyClass::changeHoverBackground(const QString &strBackground)
    2. {
    3. return QString( "QListView::item:hover { " +strBackground + " }");
    4. }
    5. ....
    6.  
    7. QString strBackground1 = "background-color: #EEEEEE;";
    8. QString strBackground2 = "background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #FAFBFE, stop: 1 #DCDEF1) "
    9. QString strBackground = strBackground1;
    10.  
    11. QString strHover = changeHoverBackgrounf(strBackground2);
    12.  
    13.  
    14. d->verticalModeList->setStyleSheet(strHover);
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QListView stylesheet problem

    Hi high_flyer

    Thank you again for your response. I think I am clearly missing something since I still don't understand what you mean. If I understand the pseudo code that you posted correctly it will create different backgrounds for the hover effect?

    I want the QListWidget to have a custom background color, along with a hover effect. As I mentioned before, I can change only the background color and it works but if I try to combine this with hover effects, the hover effects does not work.

    I've tried a couple of other things also without success. First I tried to set the palette, this does not work.
    Qt Code:
    1. d->verticalModeList->setPalette(QPalette(QColor(100, 150, 200)));
    2. d->verticalModeList->setAutoFillBackground(true);
    To copy to clipboard, switch view to plain text mode 

    I also tried to reimplement the paint function of the QListWidget as follows, this also does not work. I know that the code is incomplete, but I just want to test if the background is actually painted in a different color.
    Qt Code:
    1. void MyListWidgetSubclass::paintEvent(QPaintEvent *event)
    2. {
    3. setPalette(QPalette(QColor(100, 150, 200)));
    4. setAutoFillBackground(true);
    5. QListWidget::paintEvent(event);
    6. }
    To copy to clipboard, switch view to plain text mode 

    I find it really amazing that it is this difficult so achieve something simple like this.


    Added after 46 minutes:


    Well I'm not sure if its the best way to solve it, but I managed to change the background color of the QListWidget by painting a rectangle over it before painting the items.

    Here is the paintEvent() override that I used:
    Qt Code:
    1. void MyListWidgetSubclass::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this->viewport());
    4. painter.setPen(QPen(Qt::blue));
    5. painter.setBrush(QBrush(mBackground, Qt::SolidPattern));
    6. painter.drawRect(-1,-1,sizeHint().width()+1,sizeHint().height()+1);
    7. QListView::paintEvent(event);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by JPNaude; 15th November 2010 at 10:44.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListView stylesheet problem

    Your style sheet in the original post was ok, but you made it break with the line:
    Qt Code:
    1. stylesheet += "background-color: #EEEEEE;";
    To copy to clipboard, switch view to plain text mode 
    chang it to
    Qt Code:
    1. stylesheet += "QListView {
    2. background-color: #EEEEEE;
    3. }";
    To copy to clipboard, switch view to plain text mode 
    and to be on the safe side have it to be the first string in the style sheet.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. The following user says thank you to high_flyer for this useful post:

    JPNaude (15th November 2010)

  8. #7
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QListView stylesheet problem

    Well thank you very much it does actually solve it! I guess I didn't completely get the syntax of the stylesheets.

    Thanks again for the help.


    Added after 9 minutes:


    Well the last things that I mentioned in my original post is that I cannot get rid of the rectangle around the text of a selected item. Below is an image of the rectangle that I'm talking about:



    Any ideas to get rid of it? I tried a few things but it does not remove the rectangle:

    Qt Code:
    1. // Try 1
    2. stylesheet += "QListView::item::text { border-style: none; }";
    3.  
    4. // Try 2
    5. stylesheet += "QListView::item:selected:active::text { border-style: none; }";
    To copy to clipboard, switch view to plain text mode 

    I'm sure this must be possible but I don't have a clue how to do it.
    Last edited by JPNaude; 15th November 2010 at 11:39.

  9. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListView stylesheet problem

    Your item does not have a border!
    Only the Label IN the item.
    Your item is not only the label, but an object that has a label.
    You can test it by applying a rule for no border for QLabel.
    If the border goes away, then just change to rule to apply for QLabel which are children of your QListView.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #9
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QListView stylesheet problem

    I've tried to get hold of the QLabel but I'm not convinced that the text is a QLabel. I set a style on QLabel for my whole application to make all labels red and they all go red, but not the text.

    The solution to get rid of it for me is:

    Qt Code:
    1. myListWidget->setFocusPolicy(Qt::NoFocus);
    To copy to clipboard, switch view to plain text mode 

    Thanks again for the help.

  11. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QListView stylesheet problem

    Well in that case use focus as a state filter for your style sheet:
    Qt Code:
    1. stylesheet += "QListView::item::focus{ border-style: none; }";
    To copy to clipboard, switch view to plain text mode 

    This way you have everything as style sheet and not part in style sheet and part in code.

    Style sheets are tricky, you have to experiment to get it right!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QListView's scrollbar stylesheet
    By luochen601 in forum Qt Programming
    Replies: 4
    Last Post: 9th September 2010, 06:34
  2. StyleSheet problem
    By fmariusd in forum Qt Programming
    Replies: 0
    Last Post: 22nd February 2010, 16:10
  3. Problem stylesheet with effect
    By hakimingos in forum Qt Programming
    Replies: 0
    Last Post: 9th February 2010, 06:23
  4. QListView StyleSheet NOT WORKING!
    By QbelcorT in forum Qt Programming
    Replies: 5
    Last Post: 22nd March 2009, 08:38
  5. Stylesheet (css) problem
    By madcama in forum Newbie
    Replies: 1
    Last Post: 4th December 2006, 22:41

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.