Results 1 to 10 of 10

Thread: setting a custom QStyle on a QWidget doesn't work

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default setting a custom QStyle on a QWidget doesn't work

    I try to set a custom style on a widget, a QScrollArea specifically, and nothing happens, so I go up the hierarchy and set the custom style to a QDialog, which is the main widget in the application. Still nothing. So I go up and set it to the application object in main(). It works!

    The only problem is eventually this QDialog is going to be a .dll so it won't have an application object. How do I make this work?

  2. #2
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: setting a custom QStyle on a QWidget doesn't work

    You say "custom style". Are you also having this problem with the standard Qt styles? Could you also post some code snippets so we can see what you are doing?

  3. #3
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: setting a custom QStyle on a QWidget doesn't work

    Yes, I subclassed QWindowsStyle and drew some of my own controls. I just tried doing it with the built-in styles but it was the same.

    Here are some snippets of how I'm using the styles.

    Qt Code:
    1. //this is main, the only place I can set styles and have it work
    2. int main(int argc, char * argv[])
    3. {
    4. QApplication app(argc, argv);
    5. app.setStyle(new MaxStyle());
    6. LightPrimitiveProperties * properties = new LightPrimitiveProperties();
    7. LightPrimitiveDialog light(properties);
    8.  
    9. light.show();
    10. app.exec();
    11.  
    12. return 0;
    13. }
    14.  
    15. //here is part of the main Dialog's constructor, any style here does nothing
    16. this->setWindowIcon(QIcon(m_install_directory + "/Icons/icon.png"));
    17. this->setStyle(new MaxStyle());
    18. this->setPalette(QPalette(QColor(197, 197, 197))); //match 3DSMax color scheme
    19.  
    20. //here is the actual widget I need the style on, does nothing ("this" refers to the Dialog)
    21. m_dialog_scrollarea = new QScrollArea();
    22. m_dialog_scrollarea->setStyle(new MaxStyle());
    23. m_dialog_scrollarea->setFixedSize(210, 800);
    24. m_dialog_scrollarea->setWindowFlags(Qt::FramelessWindowHint);
    25. m_dialog_scrollarea->setWidget(this);
    To copy to clipboard, switch view to plain text mode 

    I'd appreciate any help you can give, this is the first time I've customized a style.

  4. #4
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: setting a custom QStyle on a QWidget doesn't work

    No one has any ideas? This is really shutting me down...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setting a custom QStyle on a QWidget doesn't work

    Maybe you forgot to add "const" at the end of method signatures?

  6. #6
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: setting a custom QStyle on a QWidget doesn't work

    Thanks for responding. No, all the methods in the QStyle class are declared const, I just copy and pasted from the QStyle example mainly.

    The problem must be that the QApplication is overriding any style I set inside it. If this is the case, its possible that when I create the .dll with no application object it will work fine the way it is. I will test that. But it seems like there should be a way to force a widget to accept a certain style, right?

  7. #7
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: setting a custom QStyle on a QWidget doesn't work

    The QWidget::setStyle() documentation says, "Setting a widget's style has no effect on existing or future child widgets."

    Here is my guess: Since you are setting this on a QDialog, it only affects the QDialog, and NOT any child widgets of that dialog. I am guessing that the style is being set, but that you are not able to see any differences on a QDialog. You do set it on a QScrollArea, but if you have not styled panels and scrollbars, then you would not see differences either.

    Try setting the style on a child widget instead, one that is easier to tell if the style changes (like a button) and see if that works.

  8. The following user says thank you to Brandybuck for this useful post:

    JimDaniel (11th January 2008)

  9. #8
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: setting a custom QStyle on a QWidget doesn't work

    Thanks Brandybuck. I gave what you suggested a try. My custom style redraws groupboxes and scrollbars. Setting the style directly on the groupboxes works beautifully. However, it still doesn't work on the scroll bar area and that confuses me.

    I'm sure my QStyle code is correct, because when I set the style on the QApplication object, everything comes out how its supposed to, groupboxes and scrollbars alike.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setting a custom QStyle on a QWidget doesn't work

    Quote Originally Posted by JimDaniel View Post
    I'm sure my QStyle code is correct, because when I set the style on the QApplication object, everything comes out how its supposed to, groupboxes and scrollbars alike.
    Then how about this?
    Qt Code:
    1. m_dialog_scrollarea->horizontalScrollBar()->setStyle(new MaxStyle());
    2. m_dialog_scrollarea->verticalScrollBar()->setStyle(new MaxStyle());
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to jacek for this useful post:

    JimDaniel (11th January 2008)

  12. #10
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: setting a custom QStyle on a QWidget doesn't work

    That did it. Thanks so much!

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.