push button identification

  • 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);
            for (m=0;m<max;m++)
             int t=0;
                for (s=2;s<200;s++)
               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])

  • Moderators

    I suggest you look into


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

  • @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);

    and compare the objectnames in slot like this,


  • Moderators


    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 :)

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

Log in to reply