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); 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(); }
-
@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();
-
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 :)