PDA

View Full Version : Styling Qml desktop components



unias
30th September 2014, 16:38
Currently I'm working on a desktop application that supports touchscreen display. I use QML on Qt 4.8 and OpenSUSE 13.1 (I can't upgrade either Qt or the OS due to dependencies reason)

Because of the lack of main components, I use qml desktop component (https://qt.gitorious.org/qt-components/desktop/source/b96e92a936b3fba8622bc721861ec7cbfd3fdb02:). However, I don't find a way to style those conveniently, like use CSS. Something like change the color of the button, change the border... seem complicated

I've read Qml styling guide (https://qt-project.org/wiki/QmlStyling), Qml integrating guide (http://qt-project.org/doc/qt-4.8/qml-integration.html) but can't find a satisfied answer.

Any ideas are appreciated :)

wysota
30th September 2014, 16:48
Those components are not really meant to be styled.

unias
30th September 2014, 17:23
So it's impossible or the cost outweigh the benefit?

wysota
30th September 2014, 19:24
Both :) If you want stylable Qt Quick components, you'll need to upgrade to Qt5.

unias
1st October 2014, 10:40
I found a 2 years old thread, which contains your posts :D

http://www.qtcentre.org/threads/47888-Qml-Desktop-Components-using-StyleItems-What-is-it-Can-I-change-it

I think this has something very promising, maybe I can "inject" CSS into the component...

My wild idea, it ain't work. Do you have any comments, ideas, anythings... ?

qstyleitem.cpp

case Button: {
if (!m_styleoption)
m_styleoption = new QStyleOptionButton();

QStyleOptionButton *opt = qstyleoption_cast<QStyleOptionButton*>(m_styleoption);
opt->text = text();
QPushButton *a = new QPushButton;
a->setStyleSheet("background-color:black;");//=> set CSS here
opt->initFrom(a);

opt->features = (activeControl() == "default") ?
QStyleOptionButton::DefaultButton :
QStyleOptionButton::None;
}
break;

wysota
1st October 2014, 10:44
I don't understand what this code is supposed to do. You are creating a push button and then you are not doing anything with it. What is the intention behind this code? If you expected that calling initFrom and passing the button would magically make your qml desktop component black then this won't happen. QStyleOption::initFrom does not do anything regarding drawing, it simply fills the QStyleOption structure with data taken from its argument. The list of variables is public so you can see in the docs what they are.

unias
1st October 2014, 11:09
Yes, that's a wild idea :p

I think it's the only place that can modify the style of conponents, so I try to modify it to "inject" a Qss file.

Another idea is use QDeclarative to "convert" QML to a widget and set style from there. But I have no idea how to do it now :p

You have any ideas?

wysota
1st October 2014, 12:17
You have any ideas?

Yes. Forget stylesheets. Either implement a regular style for your components or don't use the components at all and do styling via regular qml statements.

unias
1st October 2014, 15:31
Reality is that harsh :(

My team is using those components so I can't change to regulars components which use just qml statements.

Okay, I think I'll look at the source code of Qt5, hope I can reuse some elements to solve the problem.

Thank for the help :) I appreciate that :)

wysota
1st October 2014, 20:20
My team is using those components
What is your team using for styling them?

unias
2nd October 2014, 02:41
What is your team using for styling them?

LOL, they just use them, my job is styling those components :p

wysota
2nd October 2014, 10:01
LOL, they just use them, my job is styling those components :p

In that case don't use them or implement a proper style in C++.