QInputDialog Example

edited February 2013 in Ruby Scripting
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

  • edited February 2013

    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:

    bool ok = true;
    QString text = QInputDialog::getText(this, tr("Enter Name"),tr("User name:"), QLineEdit::Normal,"", &ok);
    

    This is how it translates to Ruby:

    # use the main window as the parent
    mw = RBA::Application::instance.main_window
    text = RBA::QInputDialog::getText(mw, "Enter Name", "User name:", RBA::QLineEdit_EchoMode::Normal, "")
    

    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:

    # use the main window as the parent
    mw = RBA::Application::instance.main_window
    ok = RBA::Value::new(true)
    text = RBA::QInputDialog::getText(mw, "Enter Name", "User name:", RBA::QLineEdit_EchoMode::Normal, "", ok)
    if ok.value
      # "Ok" was pressed ...
      ...
    end
    

    The next example is a input dialog with an item selection. Here's the C++ code:

    QStringList city;
    city << tr("Bhavnagar") << tr("Ahmedabad") << tr("Mumbai") << tr("Delhi");
    bool ok;
    QString item = QInputDialog::getItem(this, tr("getItem()"),tr("City"), city, 0, false, &ok);
    if (ok && !item.isEmpty())
        ...
    

    It translates to KLayout's Qt/Ruby implementation the following way:

    city = [ "Bhavnagar", "Ahmedabad", "Mumbai", "Delhi" ]
    
    # use the main window as the parent
    mw = RBA::Application::instance.main_window
    ok = RBA::Value::new(true)
    item = RBA::QInputDialog::getItem(mw, "getItem()", "City", city, 0, false, ok)
    if ok.value && !item.empty?
      # "Ok" was pressed ...
      ...
    end
    

    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:

    int i = QInputDialog::getInteger(this, tr("getInteger()"),tr("Value"), 25, 0, 100, 1, &ok);
    if (ok)
        ...
    

    translates to

    # use the main window as the parent
    mw = RBA::Application::instance.main_window
    ok = RBA::Value::new(true)
    i = RBA::QInputDialog::getInteger(mw, "getInteger()", "Value", 25, 0, 100, 1, ok)
    if ok.value
      # "Ok" was pressed ...
      ...
    end
    

    Translating the double value example works precisely the same way.

    Regards,

    Matthias

  • edited November -1
    Thank you very much! That was the push that I needed.
    -Jared
Sign In or Register to comment.