简体   繁体   中英

Adding constraint to css file for a QWidget

I use a css file in my Qt Projets, with Visual Studio 2010.

main.cpp :

QApplication app(argc, argv);
// Mise en place du style CSS
QFile File("Resources/style.css");
File.open(QFile::ReadOnly);
QString styleSheet = File.readAll();
File.close();
app.setStyleSheet(styleSheet);

A part of my css file :

QWidget#contenuDescription {
    background-color: rgb(0, 150, 255);
    border: 2px solid rgb(0, 0, 255);
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    border-bottom-right-radius: 10px;
    border-bottom-left-radius: 10px;
}

QLabel#nom1 {
    background-color: rgb(0, 150, 255);
    font-size: 36px;
}

QLabel#nom2 {
    background-color: rgb(0, 150, 255);
    font-size: 24px;
}

QLabel#nom3 {
    background-color: rgb(0, 150, 255);
    font-size: 20px;
}

I want to change the color to my QLabels : nom1, nom2 and nom3 when bool m_changeColor == true .

I know we can use ::hover if we want to change the style sheet when the mouse is on the QLabel. Something like this does it exist for my problem ?

Thank you in advance for your answer.

You need to use properties:

Q_PROPERTY(bool changeColor ...)

Os set a property dynamically :

nom1Label->setProperty("changeColor", true);

Then in CSS:

QLabel#nom1[changeColor="true"] {
    ...
}

Also note:

Warning: If the value of the Qt property changes after the style sheet has been set, it might be necessary to force a style sheet recomputation. One way to achieve this is to unset the style sheet and set it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM