PDA

View Full Version : setting a custom QStyle on a QWidget doesn't work



JimDaniel
10th January 2008, 16:51
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?

Brandybuck
10th January 2008, 18:36
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?

JimDaniel
10th January 2008, 18:54
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.



//this is main, the only place I can set styles and have it work
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
app.setStyle(new MaxStyle());
LightPrimitiveProperties * properties = new LightPrimitiveProperties();
LightPrimitiveDialog light(properties);

light.show();
app.exec();

return 0;
}

//here is part of the main Dialog's constructor, any style here does nothing
this->setWindowIcon(QIcon(m_install_directory + "/Icons/icon.png"));
this->setStyle(new MaxStyle());
this->setPalette(QPalette(QColor(197, 197, 197))); //match 3DSMax color scheme

//here is the actual widget I need the style on, does nothing ("this" refers to the Dialog)
m_dialog_scrollarea = new QScrollArea();
m_dialog_scrollarea->setStyle(new MaxStyle());
m_dialog_scrollarea->setFixedSize(210, 800);
m_dialog_scrollarea->setWindowFlags(Qt::FramelessWindowHint);
m_dialog_scrollarea->setWidget(this);


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

JimDaniel
11th January 2008, 00:40
No one has any ideas? This is really shutting me down...

jacek
11th January 2008, 13:48
Maybe you forgot to add "const" at the end of method signatures?

JimDaniel
11th January 2008, 15:54
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?

Brandybuck
11th January 2008, 18:40
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.

JimDaniel
11th January 2008, 21:09
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.

jacek
11th January 2008, 21:45
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?

m_dialog_scrollarea->horizontalScrollBar()->setStyle(new MaxStyle());
m_dialog_scrollarea->verticalScrollBar()->setStyle(new MaxStyle());

JimDaniel
11th January 2008, 23:03
That did it. Thanks so much!