Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Can we override the properties set through stylesheet ?



  • Hi,

    I have a scenario where there is a list of widgets on a form, for all these widgets few properties like color/font has been set through a single stylesheet file (.qss). Now through a settings dialog can I change override the widget properties like font / foreground color / background color ? Or It will reflect always the properties as set in the stylesheet file ?

    Regards
    Sam



  • The last stylesheet line will override the previous ones if they affect the same properties.

    Just give it a try, you should be good.



  • A test example to explain:

    I have a form with two pushButton , For the first button the font properties are set through stylesheet , when I click on the second button , there in the slot I set the font using QFont which doesn;t change anything.

    @/****BEGIN TEST ****/

    QFile file("C:/test/TestFont/style.qss");

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    qDebug() << "Could not load file";
    else
    {
    QString StyleSheet = QLatin1String(file.readAll());
    ui->pushButton->setStyleSheet(StyleSheet);
    }

    /****END TEST ****/
    @

    SLOT:
    @void MainWindow::on_pushButton_2_clicked()
    {
    QFont font;
    font.setFamily("Arial");
    font.setPointSize(10);

    ui->pushButton->setFont(font);
    qDebug() << ui->pushButton->font();
    

    }@

    StyleSheet.qss
    @QPushButton#SomeButton
    {
    font-family: MS Outlook;
    font: bold 14px;
    }@



  • so you're not using a stylesheet in your slot

    What message does the qDebug in your slot give you?



  • Yes thats the question, If we can mix stylesheet properties and QFont/QPalette

    So if I set the same value using stylesheet then it works, but in this case I need to write a css file dynamically and force it to be used only for the specific module that uses it within the application.

    @QFont font;
    font.setFamily("Arial");
    font.setPointSize(20);

    ui->pushButton->setStyleSheet(QString("font-family: %1; font: %2px;").arg(font.family()).arg(font.pointSize()));@

    The idea is that we have a designer with a lot of widgets, somewhere there is a container/ list with some style define that can be edited by the user,

    So if I drag and drop a particular style to a widget the should be rendered/reflected by all the widgets which has the same type, eg a pushbutton.



  • Implemented !

    Thanks Eddy



  • Hi Sam , Please share how you achieved that.



  • Set font and refresh qss theme again. It works. Looks dirty but I think there is no right solution in such case.

    Added info for future reference


Log in to reply