Results 1 to 10 of 10

Thread: Overwriting custom widget stylesheet from mainWindow

  1. #1
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Overwriting custom widget stylesheet from mainWindow

    I've created a custom widget plugin and set a stylesheet to it.
    When I drag it into the designer, then change the mainWindow stylesheet, it doesn't change my widget's style. Is there a way I can force it to do this?

    Basically, I want the custom widget (myButton) to have a style while I'm adding it to the designer. I then want this to change from a single main window stylesheet; like from a skinning stylesheet file.

    Any suggestions?

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

    Default Re: Overwriting custom widget stylesheet from mainWindow

    from the top of my head:
    You could override the constructor (or maybe showEvent()) of your custom widget, to see if it has a parent, and if it does, it should look if the parent uses a style sheet, and then call setStyleSheet() with the parents styleSheet().
    ==========================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. The following user says thank you to high_flyer for this useful post:

    pan (11th November 2010)

  4. #3
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Overwriting custom widget stylesheet from mainWindow

    Thanks high_flyer.
    I had just tried writing a check in the constructor of my widget - on this "window()->styleSheet()" - to see if there was a styleSheet on the top window.
    It seems to be close to what I want.
    I'll check what I can do in showEvent() perhaps that will be the better option.

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

    Default Re: Overwriting custom widget stylesheet from mainWindow

    I'll check what I can do in showEvent() perhaps that will be the better option.
    Now that I think of it, it is the better option (showEvent()), since when you drag a widget from the tool panel in designer, your custom widget is already created, but has no parent.
    What you can do in addition to the showEvent() is also overload setParent() to make sure that if parents change the style sheet of the new parent is loaded.
    ==========================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.

  6. #5
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Overwriting custom widget stylesheet from mainWindow

    Thanks again.
    It works perfectly when I run the app, just need to fix it in the designer... I'll try overloading setParent() now.

  7. #6
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Overwriting custom widget stylesheet from mainWindow

    I wanted to read the styleSheet from the top level (QMainWindow). It works fine when I run the app, but during the designer phase my widget can't seem to find the top window through the method: window()

    Is there a way to get around this whilst in the designer?

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

    Default Re: Overwriting custom widget stylesheet from mainWindow

    I don't think so.
    The forms in designer are not in an application.
    The designer only helps you save some typing when you design your forms, and makes it easier to use layouts etc.
    If you want to have things adjust like that in designer you will have to change designer it self!
    ==========================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.

  9. #8
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Overwriting custom widget stylesheet from mainWindow

    Ok. Good to know the limitation. Working at runtime is a good enough solution.

    I had been looking at this: QDesignerFormWindowInterface

    But still wasn't able to access the main window.
    Thanks for replying so quickly.

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

    Default Re: Overwriting custom widget stylesheet from mainWindow

    I had been looking at this: QDesignerFormWindowInterface

    But still wasn't able to access the main window.
    Heh, you are one step ahead of me, the trolls have added so much in last few Qt releases, that I have trouble to keep up, speically with such not very often used classes.
    From a brief look in to QDesignerFormWindowInterface, I think that just might be what you are looking for.
    Have a look at QDesignerFormEditorInterface::formWindowManager (), and then in QDesignerFormWindowManagerInterface.
    I am almost certain that this will allow you to get the form styleSheet on which your custom widget is dropped.
    ==========================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.

  11. #10
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Overwriting custom widget stylesheet from mainWindow

    I finally got it working:
    Qt Code:
    1. form = QDesignerFormWindowInterface::findFormWindow(widget);
    2. form->mainContainer()->styleSheet();
    To copy to clipboard, switch view to plain text mode 
    This works for the designer.

Similar Threads

  1. Using custom Q_PROPERTY with Stylesheet
    By hubbobubbo in forum Qt Programming
    Replies: 7
    Last Post: 30th September 2010, 10:48
  2. How to use stylesheet of custom widget
    By luochen601 in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2010, 10:10
  3. Replies: 10
    Last Post: 29th May 2010, 18:42
  4. Usability of StyleSheet in Pure Custom Widget Plugin?
    By umituzun84 in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 14:22
  5. Add a custom widget in code to a MainWindow
    By Steff in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2009, 07:17

Tags for this Thread

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.