PDA

View Full Version : Signal/Slot question



sooofunky
4th May 2011, 11:56
Hello,

I've got a button, which when clicked invokes a slot of another widget. The problem is I need to transfer data (date value), which clicked() does not understand/know. Do I really need to implement a custom button to be able to transfer data? How would you solve this problem?

Thanks!

wysota
4th May 2011, 12:01
This is really a C++ issue and not a Qt one. There are numerous ways of sharing data within objects. Without knowing your concrete use-case it is hard to suggest anything.

sooofunky
4th May 2011, 12:07
Hope this makes it clearer:

connect(ui.pushButton, SIGNAL(clicked()), myGraph, SLOT(nextDay(QEdit))); //QEdit is the currently selected date

MarekR22
4th May 2011, 12:29
IMHO the best way to do it is catch signal from button by MainWidow (I assume that this is your window widget), and this slot invoke a custom signal which will fit to your requirements or simply call this method (best approach depends on how you designed your code). Direct connection is impossible in this case.

Added after 11 minutes:

Example of slot:

void MainWindow::pushButton_clicked()
{
QDate date(QDate::fromString(ui.edit->text()));
if (date.isValid()) {
emit dateHasBeenSelected(date);
} else {
ui.edit->setText(QDate::currentDate().toString());
}
}

wysota
4th May 2011, 13:24
myGraph should have a pointer to the object carrying the date and when it receives the clicked() signal from the button, it should query the object for the date and use it according to its needs. You don't need any extra slots here.