Sending INT from dialog back to main.cpp
-
I have a main.cpp setup, and added a dialog, in my dialog I have
[code]
CardSelect QDialog;
QDialog.setModal(true);
if(QDialog.exec())
{
Card= //the return INT from Dialog//
QMessageBox::information(0,"Success",QString::number(Card)); // ’this’ didn’t work so I used ‘0’//
}
[/code]
And in my dialog named cardselect I have
[code]
void CardSelect::on_comboBox_highlighted(int index)
{
card = index;
}void CardSelect::on_pushButton_clicked() { // code to send card back to main// }[/code]
I know I must be missing something here, simple yet obvious to someone who knows c++ inside and out, but for me, I learned mostly on my own, and cannot figure this out, if you could help with this, I would be very grateful.
Thank you
-
I think you could use signals and slots.
http://qt-project.org/doc/qt-4.8/signalsandslots.htmlHope it helps you.
Regards.
-
Hi Crossfire_softwarez,
First there is a little misunterstanding with your CardSelect instance variable maybe. It probably inherits QDialog, but should not be named "QDialog" too. Prefer a name like "cardSelectDialog".
Calling exec() will blocking until dialog is canceled/accepted/closed. To retrieve your value, the easiest way is to add an attribute to you CardSelect class, that you will set in you "on_pushButton_clicked()" method, and then call that attribute getter right after you called exec().
Thus giving
@
// CardSelect declaraction (.h)
class CardSelect : public QDialog
{
...
private:
int m_cardValue;
};
@
@
// CardSelect implementation (.cpp)void CardSelect::on_pushButton_clicked()
{
m_cardValue = 1;
// or m_cardValue = sender().property("cardValue"); to use Qt property system
}int CardSelect::myCardValue() const
{
return m_cardValue;
}
@
@
// Main
if(cardSelectDialog.exec())
{
int Card = cardSelectDialog.myCardValue();
QMessageBox::information(0,"Success",QString::number(Card)); // ’this’ didn’t work so I used ‘0’//
}
@Note that exec() returns 1 only if user "accepts" dialog. Don't forget to init your private m_cardValue too in CardValue constructor.
-
And this thread belongs to "General and Desktop":http://qt-project.org/forums/viewforum/10/ category :)
-
[quote author="Adrien Leravat" date="1358949397"]And this thread belongs to "General and Desktop":http://qt-project.org/forums/viewforum/10/ category :)[/quote]
Moved.