Results 1 to 7 of 7

Thread: StyleSheet in QT with QTCreator

  1. #1
    Join Date
    Aug 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default StyleSheet in QT with QTCreator

    Hi guys, i'm very, very, newbie in QT e C++

    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

    Qt Code:
    1. QMenu { color : #ff0000 }
    To copy to clipboard, switch view to plain text mode 

    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/stylesh...ml#style-rules
    that the syntax is correct.

    Can someone help me?
    Thanks very much

  2. #2
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: StyleSheet in QT with QTCreator


  3. #3
    Join Date
    Aug 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: StyleSheet in QT with QTCreator

    Oh Great!
    Thanks very much!

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

    Qt Code:
    1. void ImageViewer::createMenus()
    2. //! [19] //! [20]
    3. {
    4. fileMenu = new QMenu(tr("&File"), this);
    5. fileMenu->setStyleSheet("color : #ff0000; background-color : #0000ff");
    6. fileMenu->addAction(openAct);
    7. fileMenu->addAction(printAct);
    8. fileMenu->addSeparator();
    9. fileMenu->addAction(exitAct);
    10.  
    11. viewMenu = new QMenu(tr("&View"), this);
    12. viewMenu->addAction(zoomInAct);
    13. viewMenu->addAction(zoomOutAct);
    14. viewMenu->addAction(normalSizeAct);
    15. viewMenu->addSeparator();
    16. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. QPushButton { color: red }
    To copy to clipboard, switch view to plain text mode 
    also in QTCreator help??

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

    Qt Code:
    1. background-color: white;
    2. background-image: url(draft.png);
    3. background-attachment: scroll;
    4. }
    To copy to clipboard, switch view to plain text mode 

    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?

  4. #4
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: StyleSheet in QT with QTCreator

    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

    Qt Code:
    1. qApp->setStyleSheet(styleSheet);
    To copy to clipboard, switch view to plain text mode 

    where styleSheet is a QString.

    A stylesheet is like a CSS for an HTML page.

  5. #5
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: StyleSheet in QT with QTCreator

    Hi,
    Qt Code:
    1. background-color: white;
    2. background-image: url(draft.png);
    3. background-attachment: scroll;
    4. }
    To copy to clipboard, switch view to plain text mode 
    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).

  6. #6
    Join Date
    Aug 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: StyleSheet in QT with QTCreator

    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:

    Qt Code:
    1. firstStatusBar->setGeometry(0, 620, 480, 10);
    To copy to clipboard, switch view to plain text mode 

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

    Thanks very much for helping me.

  7. #7
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: StyleSheet in QT with QTCreator

    No, you don't have to use .ui files.
    You can just use the setStyleSheet(QString) syntax in your C++ code.

Similar Threads

  1. Replies: 7
    Last Post: 12th January 2011, 22:01
  2. [Solved] Using a Subversion Client with QtCreator
    By philwinder in forum Qt Tools
    Replies: 1
    Last Post: 30th November 2010, 22:16
  3. stylesheet standard button names
    By h123 in forum Qt Programming
    Replies: 0
    Last Post: 14th April 2009, 12:42
  4. QtCreator won't compile using VS2005
    By Spinz99 in forum Installation and Deployment
    Replies: 0
    Last Post: 3rd April 2009, 23:25
  5. stylesheet loading issue - thread
    By talk2amulya in forum Qt Programming
    Replies: 8
    Last Post: 26th February 2009, 12:53

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.