QT dynamic object name for stylesheet

819 views Asked by At

I have a Card widget class, Card.h and Card.cpp my code is; mainWindow.cpp

for(int i=0;i<12;i++){
   // sembolList[i] is pixmap , i is cardNumber, wCardArea is parent widget
   Card *card = new Card(sembolList[i],i,ui->wCardArea);
}

and I wanna reach object from inside itself Card.cpp in constractor

this->setObjectName("card" + QString::number(cardNumber));
qDebug() << "objectName:" << this->objectName(); // this is show true object name for example card1  

and inside of mouseclick event

this->setStyleSheet("#" + this->objectName() + " {border:5px solid red;border-radius: 10px;padding:5px}");

but this is not working. if remove ("#" + objectName) , all included widget have border. I want only first widget having border, only card have border , not its inside widget.

1

There are 1 answers

0
Vladimir Bershov On

According to The Style Sheet Syntax arcticle, you need to specify a class name before "#".

Try

setStyleSheet("Card#" + objectName() + " {border:5px solid red;border-radius: 10px;padding:5px}");

or

setStyleSheet("QWidget#" + objectName() + " {border:5px solid red;border-radius: 10px;padding:5px}");

If still not working, read Why do stylesheets not work when subclassing QWidget and using Q_OBJECT?