PDA

View Full Version : QToolButton and StyleSheets.



steb
25th May 2009, 09:14
I'm using StyleSheets under QT 4.5 in my app, and most things work (aside from a few quirks)... but I haven't figured out how to get an icon loaded into a QToolButton derived class.

in my c++ code:


class myToolButton : public QToolButton
{
Q_OBJECT

public:
myToolButton ( QWidget *pParent = 0 ) : QToolButton(pParent) {}
};



and my stylesheet css:


myToolButton {
border: none;
min-height: 2px;
min-width: 2px;
width: 16px;
height: 16px;
icon-size: 13px, 13px;
image : url (styles/cream/mybtn_up.png);
}
myToolButton:hover {
image : url (styles/cream/mybtn_hover.png);
}
myToolButton:pressed{
image : url (styles/cream/mybtn_pressed.png);
}



but the images never show. Is it not possible to do this with QT StyleSheets yet? Or am I missing something?


I'd really appreciate any help,

steb.

steb
25th May 2009, 18:03
I found the issue. Relative paths aren't working for the derived class. It works only with absolute paths to the image....

So I wrote a simple stylesheet pre-parser in my style loading method to parse all relative urls() and replace them with absolute paths (pre-pending the application base dir...)

...bug or feature?

aamer4yu
26th May 2009, 05:39
Why dont you go the Qt way ,,, use resource files !!
If you have the files in your resource file, you wont have to worry about the path much. Have a look at Qt Resource System in assistant :)

talk2amulya
26th May 2009, 08:46
I found the issue. Relative paths aren't working for the derived class. It works only with absolute paths to the image....

So I wrote a simple stylesheet pre-parser in my style loading method to parse all relative urls() and replace them with absolute paths (pre-pending the application base dir...)

...bug or feature?
there is no bug, relative path also works. the issue is that you've put space between "url" and right parenthesis..remove that and everything would work like magic!

steb
28th May 2009, 10:15
Why dont you go the Qt way ,,, use resource files !!
If you have the files in your resource file, you wont have to worry about the path much. Have a look at Qt Resource System in assistant :)

the "Qt way"?
Loading of stylesheets and external images at run-time is supported by QT... so I'm not sure I understand where you are coming from using this term.

My goal was to allow my end-users to create/modify their own themes for the application.

Anyways - as I mentioned above my themeing system now works fine with my pre-processor. This even allowed me to add support for some CSS3 features unsupported by QT such as @variables....


there is no bug, relative path also works. the issue is that you've put space between "url" and right parenthesis..remove that and everything would work like magic!

actually, no spaces existed in the original stylesheet - if there were spaces between "url" and the first parenthesis it would have given a parsing error.
The CSS sample code I posted above was just me paraphrasing the original code.

This wasn't the bug I was seeing with derived classes & relative paths.

talk2amulya
28th May 2009, 10:28
actually, no spaces existed in the original stylesheet - if there were spaces between "url" and the first parenthesis it would have given a parsing error.



qt never gives any parsing error on CSS :) mind that..and it definitely works with relative paths if properly given. there is no such "bug"

steb
28th May 2009, 19:12
qt never gives any parsing error on CSS :) mind that..and it definitely works with relative paths if properly given. there is no such "bug"

I never said it "threw/raised" the parsing errors. It just fails ungracefully.

There are bugs in QT's current CSS parsing.... lots of them.
here's just one example:

/*
QSlider::vertical
{
/* background-image: url(images/mixer/mixer_slider_back.png); */
border-width: 1;
width : 50px;
height : 170px;
}
*/


A CSS parser should ignore the above script (because the outer /* */ comment marker pair defines everything inside as a comment... regardless if there is another comment block within.

QT fails to parse this correctly. It sees the first comment marker (/*) and matches it with the first comment close marker... instead of the second comment close marker.
QT's parser obviously uses a simple internal flag to look for the matching comment close marker , instead of using a stack like system to match markers.

QT also seems to have issues with properly cascading styles in certain cases. Such as the one I indicated in the original post.

lni
28th May 2009, 20:06
I never said it "threw/raised" the parsing errors. It just fails ungracefully.

There are bugs in QT's current CSS parsing.... lots of them.
here's just one example:

/*
QSlider::vertical
{
/* background-image: url(images/mixer/mixer_slider_back.png); */
border-width: 1;
width : 50px;
height : 170px;
}
*/


A CSS parser should ignore the above script (because the outer /* */ comment marker pair defines everything inside as a comment... regardless if there is another comment block within.

QT fails to parse this correctly. It sees the first comment marker (/*) and matches it with the first comment close marker... instead of the second comment close marker.
QT's parser obviously uses a simple internal flag to look for the matching comment close marker , instead of using a stack like system to match markers.

QT also seems to have issues with properly cascading styles in certain cases. Such as the one I indicated in the original post.

You got a bad nested comment of /* */. Such nested comments are not allowed not just in CSS, but in C as well...

If you want nested comment, use "//"

faldzip
28th May 2009, 20:36
the "Qt way"?
they mean to use Qt Resource System. You can then get rid of the relative paths, as it allows you to compile your resources into the exec file. Read about it in Assistant.

/*
QSlider::vertical
{
/* background-image: url(images/mixer/mixer_slider_back.png); */
border-width: 1;
width : 50px;
height : 170px;
}
*/
Actually this code should not work in general, and for me it is quite normal that the first "/*" is matched with first "*/". That's the way it works.

steb
28th May 2009, 20:51
they mean to use Qt Resource System. You can then get rid of the relative paths, as it allows you to compile your resources into the exec file. Read about it in Assistant.

I know about the resource compiler. It's useless for a situation where I want my customers to be able to edit and create their own stylesheets/themes for loading as runtime....
hence my use of external stylesheets that are loaded at runtime - not pre-compiled into the app.



Actually this code should not work in general, and for me it is quite normal that the first "/*" is matched with first "*/". That's the way it works.

you're right. I just checked the CSS design specs. strange that nested comments are not supported in the spec, especially after so many years and several revisions.