Hello. I am very new to both Ruby and KLayout. With that said, this is a very simple question. I would like to see a script example of a numeric value being input into a "QInputDialog" box and then have that entered numeric value saved as a variable.
Thank you,
Jared
Comments
Hi Jared,
As a basis for this discussion, I am referring to the samples found here:
http://www.developer.nokia.com/Community/Wiki/How_to_take_input_from_user_using_QInputDialog_in_Qt
For the first example this is the C++ code:
This is how it translates to Ruby:
Here we use the main window object as the parent for the input dialog, because that's the only Qt window object that we have. Using it as the parent means the input dialog is a sub-window of the main window and does not become an own icon in the window list for example. We could use "nil" for the parent as well, but then the input dialog would become an independent main window and appear in the window list for example.
The "Normal" enum constant must be taken from the enum declaration instead of the QLineEdit name space (RBA::QLineEdit_EchoMode::Normal). This is because Ruby treats the enum types as individual classes and the enum constants are defined inside that class.
You could employ QObject's "tr" method to emulate the "tr" macro (use "RBA::QObject::tr(your_string)"), but that would be tedious and there is not much use since the dictionaries will be missing.
In above sample, "text" will be "nil" if Cancel was pressed or an empty text was entered. If you want to know whether "Cancel" was pressed, you have to emulate the "ok" parameter. This is not quite straightforward since pointers to strongly typed values are unknown in Ruby. For this purpose you can use the "Value" object. It basically wraps a Ruby object into something that KLayout's Ruby to C++ bridge can pass over to a bool pointer in Qt:
The next example is a input dialog with an item selection. Here's the C++ code:
It translates to KLayout's Qt/Ruby implementation the following way:
Note that it is possible to simple pass a Ruby array of strings to QStringList. Again the "Value" object is used to encapsulate the "ok" value so it can be passed to the bool pointer of the getItem method.
Translating the integer value example is straightforward:
translates to
Translating the double value example works precisely the same way.
Regards,
Matthias
-Jared