Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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(MyForm::getMyForm());
                    for (int i = 1; i <= 5; ++i) {
                        QPushButton* btn = signalMapper->widget->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 = widget->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 ?

What I have tried:

I have take this code fro this link
Link URL
Posted
Updated 5-Jan-23 0:24am
v4
Comments
Richard MacCutchan 20-Dec-22 3:45am    
You need to use the debugger to find out where the segmentation fault occurs. There is nothing obvious in the above code. But also check in the above code that none of the calls are returning null references.

1 solution

There are a few issues with the code you provided in Code 2:

In the first loop, you are trying to access the widget member of the signalMapper object, but this object does not have a widget member. You need to use MyForm::getMyForm() to get a pointer to the main window widget and then use that to call findChild.

The findChild method returns a QObject*, which you are trying to cast to a QPushButton*. However, this is not guaranteed to work, because findChild might return a pointer to a different type of widget. You should use the qobject_cast function to safely cast the returned pointer to a QPushButton*.

You are using the + operator to concatenate strings in the findChild method calls, but this operator is not defined for strings in C++98. You should use the std::stringstream class or the sprintf function to construct strings with variable parts.

Here is a fixed version of your code where these issues are fixed, and that should make Code 2 work:

C++
#include <QMainWindow>
#include <QSignalMapper>
#include <QPushButton>
#include <QLabel>
#include <sstream>

void Display::setting_Style()
{
    signalMapper = new QSignalMapper(MyForm::getMyForm());
    for (int i = 1; i <= 5; ++i) 
    {
        std::stringstream ss;
        ss << "BTN_FUN" << i << "_TEL";
        QPushButton* btn = qobject_cast<QPushButton*>(MyForm::getMyForm()->findChild<QObject*>(ss.str().c_str()));
        if (btn) 
        {
            btn->setStyleSheet("background-color: rgb(255, 0, 0);border-radius:30px;");
            signalMapper->setMapping(btn, btn);
        }
    }
    for (int i = 1; i <= 5; ++i) 
    {
        std::stringstream ss;
        ss << "LBL_FUN" << i << "_TEL";
        QLabel* btn = qobject_cast<QLabel*>(MyForm::getMyForm()->findChild<QObject*>(ss.str().c_str()));
        if (btn) 
        {
            btn->setStyleSheet("background-color: rgb(255, 0, 0);border-radius:30px;");
            signalMapper->setMapping(btn, btn);
        }
    }
}
This code should fix the segmentation fault error and apply the desired style sheet to the buttons and labels.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900