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. push button identification
Forum Updated to NodeBB v4.3 + New Features

push button identification

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 2.4k 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.
  • C Offline
    C Offline
    craigger
    wrote on last edited by
    #1

    Hey guys,
    i created a pushbutton menu based on a variable textfile. this is working but i can't identify which button was clicked. Each button should open the same window but depending on the clicked button other datasets should be used.
    Hope you guys can help me.

    void MainWindow::createMainmenu()
    {
        char* temp = getenv("Errors"); 
        std::string envirovar(temp); 
    
        int s,m;
    
        std::string Errormessage [30];
        char zeile[200],buffer[200];/
        std::fstream Logfile (envirovar + "\\Errorlog.txt", std::ios::in);
        max=30;
    
            for (m=0;m<max;m++)
            {
            Logfile.getline(line,200);
            Errorcode[m]=line[0];
             int t=0;
                for (s=2;s<200;s++)
                {
    
                    buffer[t]=zeile[s];
                    t++;
    
                }
               Errormessage[m]=buffer;
    
               if (Errorcode[m]!="\t"){
               // Create the button, make "this" the parent
               m_button = new QPushButton(buffer, this);
               // set size and location of the button
               m_button->setGeometry(QRect(QPoint(100, 100+m*100),
               QSize(400, 50)));
    
               // Connect button signal to appropriate slot
               connect(m_button, SIGNAL (clicked()), this, SLOT (HandleButton()));
    
                }
    
               if (Errormessage[m]==Errormessage[m-1])
               {
                   max=m;
               }
            }
            Logfile.close();
    
    }
    
    
    1 Reply Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      I suggest you look into

      QSignalMapper
      

      Link to the docu the "Advanced Signals and Slots Usage" Chapter is 100% your situation :)


      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
      • Venkatesh VV Offline
        Venkatesh VV Offline
        Venkatesh V
        wrote on last edited by
        #3

        @craigger
        Hi and welcome to devnet,

        Whenever your button object getting created give object name to that and once you clicked it will go to slot there you can compair with objectname.

        In your case you just do like this,
        m_button = new QPushButton(buffer, this);
        m_button->setObjectName(QString(buffer));

        and compare the objectnames in slot like this,

        this->sender()->objectName();

        J.HilkJ 1 Reply Last reply
        2
        • Venkatesh VV Venkatesh V

          @craigger
          Hi and welcome to devnet,

          Whenever your button object getting created give object name to that and once you clicked it will go to slot there you can compair with objectname.

          In your case you just do like this,
          m_button = new QPushButton(buffer, this);
          m_button->setObjectName(QString(buffer));

          and compare the objectnames in slot like this,

          this->sender()->objectName();

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Venkatesh-V

          You can do that, certainly, but theres a reason I did not suggest this approach myself, let me quote the docu here :)

          QObject *QObject::sender() const

          Warning: This function violates the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot.
          
          Warning: As mentioned above, the return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this objects thread. Do not use this function in this type of scenario.
          

          sender() can be used, but using QSignalMapper is - in this case - the "intended" way :)


          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
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            You can also use a lambda:
            connect(m_button, &QPushButton::clicked,[m,this]()->void{HandleButton(m);});

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            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