Results 1 to 3 of 3

Thread: Applying a "selected" style

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Applying a "selected" style

    I have a little style sheet, that I load from a file in the main class and apply it to the QApp like this:

    Qt Code:
    1. QApplication a(argc, argv);
    2.  
    3. QFile file("styles.css");
    4. file.open(QFile::ReadOnly);
    5. QString styleSheet = QLatin1String(file.readAll());
    6. a.setStyleSheet(styleSheet);
    7.  
    8. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    It's very convenient, because I can change the looks of my application without even having to recompile the code. Now here is the thing. I have QFrames on my gui, that I want to highlight when the mouse moves them and keep highlighted when they get clicked. So I need to set some kind of attribute in the code that the style sheet reacts to.

    I have seen this thing in a style sheet before:

    Qt Code:
    1. .selected {
    2. border-width: 3px;
    3. }
    To copy to clipboard, switch view to plain text mode 

    and I figure it would apply to items that somehow get a "selected" attribute. Does anyone know how to do this?

    Thanks
    Cruz

  2. #2
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Applying a "selected" style

    i think you can use this to highlight your frame when it's hovered:
    QFrame:hover
    {
    //some style sheet
    }

    and QFrame does not seem to have selected property, you can add a property to it in code (when a frame is selected, re-implement the QFrame::mouseEvent(...) or install an event filter to the frame)
    Qt Code:
    1. // when your frame is selected
    2. //in code
    3. ( your frame calls ) setProperty("isSelected", true);
    4. ( also your frame calls) setStyle(" ");//this is needed to make it work, i don't know why
    5. // when unselected
    6. (your frame calls) setProperty("isSelected", false);
    7. ( also your frame calls) setStyle(" ");//this is needed to make it work, i don't know why
    To copy to clipboard, switch view to plain text mode 

    //in style sheet:
    Qt Code:
    1. QFrame[isSelected="true"]
    2. {
    3. // style when selected
    4. }
    5.  
    6. QFrame[isSelected="false"]
    7. {
    8. //style when unselected
    9. }
    To copy to clipboard, switch view to plain text mode 

    Hope this is useful for you.

  3. The following user says thank you to nifei for this useful post:

    Cruz (16th January 2009)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Applying a "selected" style

    Yes it does have hover! Works like a charm, thank you!

Similar Threads

  1. Mac Style for Qt Application---help needed
    By swamyonline in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2008, 21:49

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.