PDA

View Full Version : StyleSheet in QT with QTCreator



maurelio79
23rd August 2009, 14:23
Hi guys, i'm very, very, newbie in QT e C++ :p

I'm lerning changing some example in QTCreator; now i'm working on Imageviewer example.

I'd like to change some style, but i'm not able to apply StyleSheet, for example i write in imageviewer.cpp



QMenu { color : #ff0000 }


but i got an error:


/home/maurelio/pgm/qt-sdk/qt/examples/widgets/imageviewer/imageviewer.cpp:49: error: expected unqualified-id before ‘{’ token

but i red, for example here
http://doc.trolltech.com/4.3/stylesheet-syntax.html#style-rules
that the syntax is correct.

Can someone help me?
Thanks very much

PaceyIV
23rd August 2009, 14:47
See http://doc.trolltech.com/4.3/stylesheet-examples.html

maurelio79
23rd August 2009, 15:20
Oh Great!
Thanks very much!

I'm reading.
Now i'm able to apply a style to a single menu, for example:



void ImageViewer::createMenus()
//! [19] //! [20]
{
fileMenu = new QMenu(tr("&File"), this);
fileMenu->setStyleSheet("color : #ff0000; background-color : #0000ff");
fileMenu->addAction(openAct);
fileMenu->addAction(printAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);

viewMenu = new QMenu(tr("&View"), this);
viewMenu->addAction(zoomInAct);
viewMenu->addAction(zoomOutAct);
viewMenu->addAction(normalSizeAct);
viewMenu->addSeparator();
}


Now i have to understand, how to apply the style to entire menu.
A question:
Why in a lot of example i read something like:



QPushButton { color: red }

also in QTCreator help??

In the link that you give me, i don't understand where put code like this:



QTextEdit, QListView {
background-color: white;
background-image: url(draft.png);
background-attachment: scroll;
}


in the *.cpp? Is it possible create a one file with all style (like css), so i don't need to look for style in all files?

PaceyIV
23rd August 2009, 16:41
You have to create a stylesheet for all your application and use it.

Look at the Style Sheet Example in the qt dir (qt/examples/widgets/stylesheet)

You define all your style sheet and then apply this to your application with the command



qApp->setStyleSheet(styleSheet);


where styleSheet is a QString.

A stylesheet is like a CSS for an HTML page.

nix
24th August 2009, 07:11
Hi,

QTextEdit, QListView {
background-color: white;
background-image: url(draft.png);
background-attachment: scroll;
}
Looks as code to put in qtDesigner.
You can use stylesheet directly into the ui designer (right click on the dialog and choose "change stylesheet").
Using this method allow you to see the aspect of your ui without compile anything (a preview will show you the exact look of yours widgets in any style).

maurelio79
24th August 2009, 23:19
So, if i want to use StyleSheet, i need to use as well .ui file?
For example, just to learn i'm making a window with some QLabel, QLineEdit, QStatusBar (just to know elemnts), but i don't have a .ui file, i have:

- main.cpp
- MainWindow.cpp
- MainWIndow.h
- MainWindow.pro

i put elements on main window with code:
example:



firstStatusBar->setGeometry(0, 620, 480, 10);


So to use StyleSheet, i have to use .ui?

Thanks very much for helping me.

profoX
25th August 2009, 05:18
No, you don't have to use .ui files.
You can just use the setStyleSheet(QString) syntax in your C++ code.