Reputation: 77
I'm relatively new to Qt and I’m stuck with trying to figure out how to have different buttons each return a specific value when they're clicked.I've read through the Qt doc on signals and slots but I didn't find something useful.
I know that I can just have used the clicked function and return a value using the "goto slots" however, from the main, I would like to just call a getChoice function that would open a new window and based of what the user clicks I would return a corresponding int that I can use for later. Something like this is how it would look from the main.
OptionMenuWindow option;
int choice = option.checkChoice();
So this is my code in the OptionMenuWindow class and I've tried passing an int as a reference through to the slot so that when choice0Clicked is activated I know which button was clicked.
int OptionMenuWindow::checkChoice(){
this->show();
QEventLoop waitForResponse;
//THIS IS WHERE I TRY AND DO THAT
connect(ui->manageEmployee, SIGNAL (clicked()), &waitForResponse, SLOT(choice0Clicked()));
connect(ui->view, SIGNAL (clicked()), &waitForResponse, SLOT(choice1Clicked()));
connect(ui->generatePayroll, SIGNAL (clicked()), &waitForResponse, SLOT(choice2Clicked()));
waitForResponse.exec();
cout << "here" << endl
cout << choice << endl;
return choice;
}
void OptionMenuWindow::choice0Clicked()
{
choice = 0;
}
void OptionMenuWindow::choice1Clicked()
{
choice = 1;
}
void OptionMenuWindow::choice2Clicked()
{
choice = 2;
}
choice is defined in the header file.
This is the error I'm getting:
QObject::connect: No such slot QEventLoop::choice0Clicked(&choice)
QObject::connect: (sender name: 'manageEmployee')
QObject::connect: No such slot QEventLoop::choice1Clicked(&choice)
QObject::connect: (sender name: 'view')
QObject::connect: No such slot QEventLoop::choice2Clicked(&choice)
QObject::connect: (sender name: 'generatePayroll')
I really believe that there is a better of doing this so if there is, can someone please explain it?
And if there is anything you need me to elaborate on, please tell me it in the comments.
Thank you
Upvotes: 1
Views: 1673
Reputation: 2069
You can use QSignalMapper.
From Qt Assistant :
This class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal
signalMapper = new QSignalMapper(this);
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
}
connect(signalMapper, SIGNAL(mapped(QString)),
this, SIGNAL(clicked(QString)));
Upvotes: 1
Reputation: 12831
You can not overload a slot. It should match the signal signature.
It should be something like this only
QObject::connect(ui->manageEmployee, SIGNAL (clicked()), &waitForResponse, SLOT(choice0Clicked()));
QObject::connect(ui->view, SIGNAL (clicked()), &waitForResponse, SLOT(choice1Clicked()));
QObject::connect(ui->generatePayroll, SIGNAL (clicked()), &waitForResponse, SLOT(choice2Clicked()));
Now coming to the button indexing
Let us assume you have push buttons -- "button1", "button2" and "button3".
Create "QButtonGroup" object.
QButtonGroup *bGroup = new QButtonGroup ();
Add all buttons with Ids.
bGroup->addButton(button1, 0);
bGroup->addButton(button2, 1);
bGroup->addButton(button3, 2);
Now in the slots to get ID of button:
void OptionMenuWindow::choice0Clicked()
{
QPushButton* button = qobject_cast<QPushButton*>(sender());
int buttonID = bGroup->id(button);
}
void OptionMenuWindow::choice1Clicked()
{
QPushButton* button = qobject_cast<QPushButton*>(sender());
int buttonID = bGroup->id(button);
}
void OptionMenuWindow::choice2Clicked()
{
QPushButton* button = qobject_cast<QPushButton*>(sender());
int buttonID = bGroup->id(button);
}
Upvotes: 0