How to add change style sheet of QTButtons and QTLabel in a loop using QSignalMapper and findChild in C++98
-
I am new in QT using C++ 98.I am setting the stylesheet in QT from of 5 label and 5 button using this class function
Code 1(working)
void Display::setting_Style() { MyForm::getMyForm()->widget.BTN_FUN1_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); MyForm::getMyForm()->widget.BTN_FUN2_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); MyForm::getMyForm()->widget.BTN_FUN3_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); MyForm::getMyForm()->widget.BTN_FUN4_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); MyForm::getMyForm()->widget.BTN_FUN5_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); MyForm::getMyForm()->widget.LBL_FUN1_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); MyForm::getMyForm()->widget.LBL_FUN2_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); MyForm::getMyForm()->widget.LBL_FUN3_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); MyForm::getMyForm()->widget.LBL_FUN4_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); MyForm::getMyForm()->widget.LBL_FUN5_TEL->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 0, 0);border-radius:30px;")); }
I want to replace this muliple statements qt widget for setStyleSheet by QSignalMapper findChid for setting same stylesheet in one line in a loop in C++ like this
Code 2(not working)
#include <QMainWindow> #include <QSignalMapper> #include <QPushButton> #include <QLabel> void Display::setting_Style() { signalMapper = new QSignalMapper(this); for (int i = 1; i <= 5; ++i) { QPushButton* btn = signalMapper->findChild<QPushButton*>("BTN_FUN" +QString::number(i)+"_TEL"); btn->setStyleSheet("background-color: rgb(255, 0, 0);border-radius:30px;"); signalMapper->setMapping(btn, btn); //signalMapper->setMapping(this, btn) } for (int i = 1; i <= 5; ++i) { QLabel *btn = signalMapper->findChild<QLabel*>("LBL_FUN" +QString::number(i)+"_TEL"); btn->setStyleSheet("background-color: rgb(255, 0, 0);border-radius:30px;"); signalMapper->setMapping(btn, btn); } }
It is giving me this error
The program is crashing ,segmentation fault
How i can resolve this error and make Code 2 work ?
-
I am new in QT using C++ 98
It's almost 2023. It's time to move on :)
As for the code - it's kinda all over the place.
You're just iterating over some widgets. Why do you even need a signal mapper here? Especially since you map those widget to themselves? That doesn't make sense.
find_child
will return nullptr if it couldn't find anything. You're not checking that so you're probably getting nullptr and then trying to callbtn->setStyleSheet
on it. That will crash.Those buttons don't seem to be children of the signal mapper. At least there's no code here that would make them that. Your signal mapper doesn't have any children here, so
find_child
won't find anything and will return nullptr. You should callfind_child
on the actual parent of those buttons.In any case,
find_child
operates on object names, not variable names. Are these widgets actually named with setObjectName()? If not thenfind_child
will not find them and return nullptr.