From: Stefano Crocco Date: 2010-11-21T22:35:49+09:00 Subject: Re: Qt-Ruby implementation of the "Address Book Example" On Sunday 21 November 2010 21:20:37 Arturo Bonechi wrote: > Thank you for the reply! > > Stefano Crocco wrote in post #962907: > > I'm not completely sure, but this should work: > > - define a method called add_entry which takes two optional arguments > > having > > nil as default value. Inside the method, you check whether one of the > > arguments is nil or not and choose the correct behaviour accordingly: > > > > def add_entry name = nil, address = nil > > > > if name and address > > > > #behave as the addEntry(QString, QString) function > > > > else > > > > #behave as the addEntry() function > > > > end > > > > end > > Indeed, it works! The simplest solution is often the better! Thank you! > > > - define two slots, one with no arguments and one with two arguments: > > > > slots 'add_entry()', 'add_entry(QString,QString)' > > By the way, I have some questions again. > - In this example, they use the QPair class. It seems that this class > isn't implemented in Qt-Ruby (as for the array and hash classes). So I > use an array of size two instead and it seems to work fine. Is it the > way to do it or do I have to use an other Ruby class to use QPair? Yes, using an array here is correct. Everytime you see a collection looking like a list in C++ (for example, whenever they use QStringList), you need to replace it with an array in qtruby > - If I want to create a slot that have a Ruby hash as parameter, which > QT class must I use in the slot declaration (QHash, QMap, ...)? > I don't think you can do that. It seems that neither QHash nor QMap are implemented in qtruby, and passing a native ruby hash doesn't seem to work. In my opinion, being restricted to using C++ types is one of the most serious weaknesses of qtruby. In your case, I think the best option would be to declare both signal with a string argument, then convert the hash to a string (using Marshal or YAML) before emitting the signal and converting it back in the slot. > > Note that this won't work with signals: there can be only one signal > > with a > > given name, regardless of its signature. > > I didn't knew that, thank you. Is it specific to Qt-Ruby or it is the > same in C++? No, it's another limitation of qtruby. It's caused by the fact that calling signals creates a method taking a number of parameters according to the signature you specified. Calling it again with the same name attempts to create another method with the same name and (maybe) a different number of arguments. Since ruby doesn't allow to have different methods with the same name, however, this effectively overwrites the first method. Stefano -- Posted via http://www.ruby-forum.com/.