Do not read button texts. You will be forced to interpret the texts, moreover, you can read them wrongly.
Solution #1: Each button has its own instance. Compare button with the instance addresses:
QPushButton *button
= dynamic_cast<QPushButton
*>
(sender
());
if( button == &button0 ) // the '0' button
{
do '0' button;
}
else if( button == &button1 )
{
etc;
}
...
else
{
report error;
}
QPushButton *button = dynamic_cast<QPushButton *>(sender());
if( button == &button0 ) // the '0' button
{
do '0' button;
}
else if( button == &button1 )
{
etc;
}
...
else
{
report error;
}
To copy to clipboard, switch view to plain text mode
Solution #1 needs that your slot is a member of a class where all buttons are reachable (the calculator frame most likely).
Solution #2 (simplified d_stranz solution). Derive a class, say myButton, from QPushButton and add suitable data to myButton. Populate your calculator with myButtons. When you get a signal, check your data and see which button was it.
{
public :
int myID;
myButton
( QWidget *parent
= nullptr
);
... add suitable ctors;
virtual ~myButton();
}
{
}
...
myButton::~myButton()
{
}
class myButton : public QPushButton
{
public :
int myID;
myButton( QWidget *parent = nullptr );
... add suitable ctors;
virtual ~myButton();
}
myButton::myButton( QWidget *parent ) : QPushButton(parent)
{
}
...
myButton::~myButton()
{
}
To copy to clipboard, switch view to plain text mode
Set IDs for all buttons (naturally, you can do it in your ctors). Populate your calculator with myButtons. Now:
myButton button = dynamic_cast<myButton *>(sender());
if( button == nullptr ) report error, it wasn't myButton;
else
{
switch( button->myID )
{
case button0 :
{
}
...
}
}
myButton button = dynamic_cast<myButton *>(sender());
if( button == nullptr ) report error, it wasn't myButton;
else
{
switch( button->myID )
{
case button0 :
{
}
...
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks