Checking if the button in a QPushButtonArray is not a specific button



  • Hello,

    I am trying to create a function that iterates through a Layout and stores the list of QPushButtons in a QList. After it has stored the QPushButtons into a list I want to iterate through the list and check whether or not the specific QPushButton is the one I am looking for. The reason for this function is so I may "unhighlight" all other buttons when this function is called by the on-click slot of one of the buttons.

    The highlight and unhighlight actions are as follows (however, I want a generalized function not one that checks if the button has a certain stylesheet):

    Highlight

        buttonx->setStyleSheet("border: 1px solid #ffffff; background: #DFDFDF; border-radius: 25px;");
    

    Unhighlight

        buttonx->setStyleSheet("border: 1px solid #ffffff; background: white");
    

    My current progress in the code is as follows where Tabs_List is a QList of QPushButton Type however, I cannot seem to push the Buttons to the end of the list because of the following error:

    error: reference to type 'const QPushButton' could not bind to an rvalue of type 'QLayoutItem *'

    How do I fix this? Additionally I cannot seem to find a method allowing me to see if a button in the array is the same button as the active button (the button I don't want to unhighlight) specified in the parameter. I assume there is some way via pointers by comparing the addresses of the buttons but I am not sure.

    QVBoxLayout Tabs_Grid = new QVBoxLayout();
    
    void MainWindow::toggleActiveButton(QPushButton *active)
    {
        QList<QPushButton> tabs_list;
        if(Tabs_Grid->takeAt(0)->widget()->metaObject()->className() == "QPushButton")
        {
            tabs_list.push_back(Tabs_Grid->takeAt(0));
        }
    }
    

    Any help is greatly appreciated.


  • Qt Champions 2018

    @patelvrajn said in Checking if the button in a QPushButtonArray is not a specific button:

    Tabs_Grid->takeAt(0)->widget()

    Here you're accessing correctly the widget but not the line below.



  • @Christian-Ehrlicher

    I purposely omitted the part after takeAt because whether I put Widget or not it, the same type of error ensues. The error I get for replacing that line with:

    tabs_list.push_back(Tabs_Grid->takeAt(0)->widget());
    

    is:

    reference to type 'const QPushButton' could not bind to an rvalue of type 'QWidget *'


  • Qt Champions 2018

    @patelvrajn You should change

    QList<QPushButton> tabs_list;
    

    to

    QList<QPushButton*> tabs_list;
    

    QObject instances are not copyable.
    Currently you're trying to push_back a pointer where an instance is expected, this is not going to work.



  • That worked! I did not realize I was sending a pointer into an array of instances, thank you! I also figured out the rest of the code I needed assistance on, on my own. For those who need a reference for this type of situation, this is the resultant solution:

    void MainWindow::toggleActiveTab(QPushButton *active)
    {
        QList<QPushButton*> tabs_list;
    
        std::string comparator = "QPushButton";
    
        for(int index = 0; index < Tabs_Grid->count(); index++)
        {
            if(strncmp(Tabs_Grid->itemAt(index)->widget()->metaObject()->className(), comparator.c_str(), (size_t) comparator.length()) == 0)
                tabs_list.push_back((QPushButton *)Tabs_Grid->itemAt(index)->widget());
        }
    
        while(!tabs_list.empty())
        {
            if(tabs_list.front() == active)
            {
                tabs_list.front()->setStyleSheet("border: 1px solid #ffffff; background: #DFDFDF; border-radius: 25px;");
                tabs_list.pop_front();
                continue;
            }
            tabs_list.front()->setStyleSheet("border: 1px solid #ffffff; background: white");
            tabs_list.pop_front();
        }
    }
    

  • Moderators

    @patelvrajn you should keep in mind, that setStyleSheet calls are rather expensive. If you do this a lot of times for a whole bunch of objects.

    There are other ways to influence the stylesheet/appearance of on object
    take a look here, to learn more about QStylesheet and dynamic properties;:

    https://wiki.qt.io/Dynamic_Properties_and_Stylesheets


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.