Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Checking if the button in a QPushButtonArray is not a specific button
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 757 Views 1 Watching
  • 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.
  • P Offline
    P Offline
    patelvrajn
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • P Offline
        P Offline
        patelvrajn
        wrote on last edited by
        #3

        @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 *'

        jsulmJ 1 Reply Last reply
        0
        • P patelvrajn

          @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 *'

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • P Offline
            P Offline
            patelvrajn
            wrote on last edited by patelvrajn
            #5

            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();
                }
            }
            
            J.HilkJ 1 Reply Last reply
            0
            • P patelvrajn

              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();
                  }
              }
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @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


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2

              • Login

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