Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to add change style sheet of QTButtons and QTLabel in a loop using QSignalMapper and findChild in C++98
QtWS25 Last Chance

How to add change style sheet of QTButtons and QTLabel in a loop using QSignalMapper and findChild in C++98

Scheduled Pinned Locked Moved Unsolved Language Bindings
2 Posts 2 Posters 512 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    getche
    wrote on last edited by getche
    #1

    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 ?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 call btn->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 call find_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 then find_child will not find them and return nullptr.

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved