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

id selector not working in qss in qt 5.7



  • I have a class called mywidget in which i am creating multiple objects of Qpushbutton and i am maintaining a separate qss stylesheet. I am trying to have a separate design for each pushbutton, as per the document "QPushbutton#objectname1{ color:Red}" and "QPushbutton#objectname2{ color:Blue}" should work, but it is not working, is there any other way please help.


  • Moderators

    hi @Amogh

    I haven't yet heard of an instance where that method failed.

    can you show the button and stylesheet creation ?



  • Mywidget.cpp
    MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    {
    m_b1= new QPushButton("Submit");
    m_b2= new QPushButton("Cancel");
    .....
    ....
    .....
    QFile file(":/new/xyz.qss");
    file.open(QFile::ReadOnly);
    stylesheet = QLatin1String(file.readAll());
    qApp->setStyleSheet(stylesheet);
    }
    .qss file
    QPushButton#m_b1{
    background-color: blue;
    border-width: 2px;
    border-color: darkkhaki;
    border-style: solid;
    border-radius: 5;
    padding: 3px;
    min-width: 9ex;
    min-height: 2.5ex;
    }
    QPushButton#m_b2{
    background-color: Red;
    border-width: 2px;
    border-color: darkkhaki;
    border-style: solid;
    border-radius: 5;
    padding: 3px;
    min-width: 9ex;
    min-height: 2.5ex;
    }



  • Variable names are lost during compilation you have to call setObjectName
    m_b1->setObjectName(QStringLiteral("m_b1"));



  • @VRonin Thank you soo much! It works now :)


Log in to reply