views:

104

answers:

1

I have a QPushButton, QDateEdit and another custom object. I want to connect the button to the date edit object in a way that when I click the button, the date edit object will change its set date to a date defined on the custom object. Kinda like this:

connect(pushbutton,SIGNAL(clicked()),dateedit,SLOT(setDate(custom_object.getDate())));

but I can't do that. Apparently, the connect statement doesn't specify what's the information being passed from the signal to the slot, only the type of the information being passed. Is there a way to do this without having to create a new class?

+3  A: 

It's usually easiest to create a wrapper function that contains that functionality. In other words:

connect(pushbutton, SIGNAL(clicked()), SLOT(setDateFromCustomObject()));

And then, in the same class that calls connect:

void YourClass::setDateFromCustomObject() {
  dateEdit->setDate(custom_object.getDate());
}

It is possible to do connect time binding with specific arguments and objects through the use of an external library called Qxt. It doesn't look like their documentation is complete or up-to-date, but they do provide some nice functionality. I consider it for advanced users only.

Kaleb Pederson
Why doesn't Qt support this natively? Seems less complicated than creating whole new classes just because of a slot.
David McDavidson
@bullettime: It violates the signal/slot loose coupling. The best way to do what you wants is to create a subclass.
Adam W
You shouldn't need to create another class. You most likely already have a class somewhere that knows about both the pushbutton and the dateedit. The ideal scenario would be a lambda function, but we don't have that option.
Kaleb Pederson
@Adam W - Actually, I see nothing wrong with adding support for runtime binding nor do I see how it would affect coupling. The message passing architecture wouldn't change, rather, Qt would simply gain some data injection support. On the other hand, it muddles the concept of connecting the sender and the receiver and that of databinding. It would also complicate debugging a bit. Despite that, I absolutely would use it if it were available. In one line, the connect call, I would know exactly what was being bound and injected.
Kaleb Pederson
@Kaleb: I agree with injection on connect, but your example has one class that receives a signal from a member (`pushbutton`), then sets a value in another member (`dateEdit`) with the help of a third (`custom_object`). Ideally the third member (`custom_object`) would be a member of a `dateEdit` subclass so you could call a slot on that to set the date. This way each class could independently do the task it needed to.
Adam W
Correct. Although you don't **have** to create a class, it could certainly be better design to subclass in order to better group by responsibilities -- ideally adhering to the single responsibility principle. As I don't fully know what `custom_object` does, I don't presume to know what's better in this case. Overall, those are good considerations.
Kaleb Pederson