How to customize a tabwidget using stylesheet ?



  • I would like to customize a QTabWidget.
    I have seen a nice example( rounded-border rectangle with a gradient) but it is given in QML....
    http://doc.qt.digia.com/4.7/declarative-ui-components-tabwidget.html

    Any ideas of making that kind of tab using a more simple way, like using stylesheet functions ?

    Thks in advance for any enligtenment :)


  • Moderators



  • Thks! that's exactly what i would like :)
    Do you know how to write external stylesheet like the example above and how to include it in a project ?(i used to use setstylesheet function, but don't know how to use as "external", and i think it will be more simple)



  • You should open a new thread or try to do some Google search. Despite, here is:

    @
    QApplication a(argc, argv);
    QFile file(":/css/StyleSheet.qss");
    if (file.open(QFile::ReadOnly))
    {
    a.setStyleSheet(file.readAll());
    file.close();
    }@

    The StyleSheet.qss is a normal text file but ended with .qss despite .css to mark that is for Qt code. You can put this file wherever you want and include it in a resources (.qrc) file inside the project. With this method you dont need to include the QSS file in your executable folder.

    If you only want to load css styles (in a QSS file or whatever you want) and it is not important for you to include the file in the executable folder, try with this code:

    @
    QApplication a(argc, argv);
    QFile file("css/StyleSheet.qss");
    if (file.open(QFile::ReadOnly))
    {
    a.setStyleSheet(file.readAll());
    file.close();
    }@


  • Moderators

    The easiest way would be to add the css file to your "resource system":http://qt-project.org/doc/qt-4.8/resources.html. But you could also read the file from the file system.

    In Qt (e.g. on startup) read the file and set it either to the QApplication instance or on your MainWindow like this (better on the QApplication so that widgets without parent also get the style set):
    @
    QFile file(":/my-stylesheet.css"); //resource path
    if ( file.open(QFile::ReadOnly) )
    {
    QString css = file.readAll().constData();
    qApp->setStyleSheet( css );
    }
    file.close();
    @



  • Thanks for responses ! i used to did not know about QFile system, i have tried it, but it seems that the changes made in the stylesheet are not synchronized in the execution of my app....
    Maybe i'll post a new thread about that weird thing :)


  • Moderators

    your changes in the stylesheet only take effect once you restart your application...
    unless you use a "QFileSystemWatcher ":http://qt-project.org/doc/qt-4.8/qfilesystemwatcher.html and read and set the stylesheet again on modifications.



  • Thks i may need it in my future!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.