Results 1 to 6 of 6

Thread: Style Sheets

  1. #1
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4

    Default Style Sheets

    Hey guys, I'm currently writing an application using only QtCreator to write every line by myself, anyways, I'm wondering if there's a more efficient way to set style sheets because currently I'm editing widgets with the "setStyleSheets" method but when I change a few properties it becomes unreadable, I'd like to load them from a file or something.

    Like being able to set a style to a DockWidget with this:
    Qt Code:
    1. background-color: rgb(0, 0, 0);
    2. border-right-color: rgb(170, 255, 127);
    3. }
    4.  
    5. QDockWidget::title{
    6. background-color:pink;
    7. background-image: url(:/newPrefix/dockTitleTile.png);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by yakuzan; 28th May 2010 at 20:33.

  2. #2
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Style Sheets

    I'd like to load them from a file or something.
    Save your style sheet in to a text file, and load it using QApplication::setStyleSheet().
    Then you can just change the file, and you application will look different, that is the clean way to do it.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Style Sheets

    ...and put the file in the Qt resource system, then the application will always find the file.:
    Qt Code:
    1. QFile file(":/yourCss.qss");
    2. file.open(QIODevice::ReadOnly | QIODevice::Text);
    3. setStyleSheet(file.readAll());
    4. file.close();
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to Lykurg for this useful post:

    waynew (19th July 2010)

  5. #4
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4

    Default Re: Style Sheets

    Here's the method provided inside the Qt Doc's for anyone interested.

    Qt Code:
    1. void MainWindow::loadStyleSheet(const QString &sheetName)
    2. {
    3. QFile file(":/" + sheetName.toLower() + ".qss");
    4. file.open(QFile::ReadOnly);
    5. QString styleSheet = QLatin1String(file.readAll());
    6.  
    7. setStyleSheet(styleSheet);
    8. }
    To copy to clipboard, switch view to plain text mode 


    Thanks again.
    Last edited by yakuzan; 28th May 2010 at 22:36.

  6. #5
    Join Date
    May 2010
    Posts
    7
    Qt products
    Qt4

    Default Re: Style Sheets

    Sorry about the double post, felt like I could just re-use this thread instead.

    So I have a custom button class and I've created buttons and added them inside a regular CustomButton array:
    Qt Code:
    1. for(int x=0 ; x < size_x ; x++){
    2. for(int y=0 ; y < size_y ; y++){
    3.  
    4. buttonGrid[x][y] = new OthelloButton();
    5. centralGrid->addWidget(buttonGrid[x][y],x,y);
    6. buttonGrid[x][y]->setObjectName("gridButton");
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    For some reason, it's not listening to any styles sheet, for example the following code doesn't change anything while it should:
    Qt Code:
    1. #gridButton{
    2. background-color:orange;
    3. color:white;
    4. }
    To copy to clipboard, switch view to plain text mode 

    I've been stuck on this for some time, couldn't find any answer.

    Edit: Fixed, when I took some example code on the Qt doc's there was un-commented text between two declarations

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Style Sheets

    It is not the best idea to have a widget with objects which have all the same name. So if all your custom buttons should have that look, better write:
    css Code:
    1. OthelloButton {/*...*/}
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QDockWidget-title
    By moowy in forum Qt Programming
    Replies: 18
    Last Post: 23rd April 2014, 21:13
  2. how to hide the title bar when a QDockWidget is docked?
    By oscar721 in forum Qt Programming
    Replies: 5
    Last Post: 23rd January 2014, 21:16
  3. Combined Title and Tab for QDockWidget
    By chaoticbob in forum Qt Programming
    Replies: 0
    Last Post: 25th May 2010, 20:16
  4. contextmenu in QDockWidget title
    By klipko in forum Newbie
    Replies: 4
    Last Post: 7th March 2008, 23:32
  5. No restore button in QDockWidget title bar
    By trskel in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2007, 10:59

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.