Results 1 to 8 of 8

Thread: Create a dialog on the fly dynamically based on an input xml file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Create a dialog on the fly dynamically based on an input xml file

    Qt Code:
    1. void ReportParametersDialog::processXmlFile(const QString &xmlFile, QGridLayout *layout)
    2. {
    3. QDomDocument document("parameters");
    4. QFile file(xmlFile);
    5. if (!file.open(QIODevice::ReadOnly))
    6. {
    7. return;
    8. }
    9. if (!document.setContent(&file))
    10. {
    11. file.close();
    12. return;
    13. }
    14. QDomElement root = document.documentElement();
    15. QDomNode node = root.firstChild();
    16. while (!node.isNull())
    17. {
    18. QDomElement param = node.toElement();
    19. if (!param.isNull())
    20. {
    21. if (param.tagName() != "Parameter")
    22. {
    23. continue;
    24. }
    25. if (param.attribute("type") == "textfield")
    26. {
    27. QDomElement labelElem = node.firstChildElement("Label");
    28. QLabel *label = new QLabel(labelElem.text());
    29. QDomElement value = node.firstChildElement("Value");
    30. QDomElement size = node.firstChildElement("Size");
    31. QDomElement toolTip = node.firstChildElement("ToolTip");
    32.  
    33. QLineEdit *lineEdit = new QLineEdit(value.text());
    34. lineEdit->setMaximumWidth(size.text().toInt());
    35. lineEdit->setToolTip(toolTip.text());
    36.  
    37. int row = layout->rowCount();
    38. layout->addWidget(label, row, 0);
    39. layout->addWidget(lineEdit, row, 1);
    40. }
    41. }
    42. node = node.nextSibling(); }
    43. }
    To copy to clipboard, switch view to plain text mode 

    I thought about using QSignalMapper but I don't see how to detect if the value of any widget has changed without storing the value somewhere.
    Do you have any better idea?
    Regards,
    Franco
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: Create a dialog on the fly dynamically based on an input xml file

    In your loop that creates the QLineEdit, you can use the QObject::setProperty() method to associate a unique value with the widget (as a QVariant). You can also connect the QLineEdit::editingFinished() signal to a slot. In the slot, call sender() to retrieve the QLineEdit pointer, then use the QObject::property() method to retrieve the unique value you have stored for the widget. You can also use QLineEdit::text() to retrieve whatever the user has changed the text to.

    You are going to have devise some way of connecting the widgets you create via XML to fields in a data structure, otherwise you will end up with widgets connected to slots that don't have any idea what to do with the data they have retrieved.

    But there are a lot of problems with your design. How are you going to prevent users from entering incorrect values, for example? How will you enforce required fields (i.e. a field that cannot be left empty)? How can you set the tab order so that the tab key doesn't just take the user to random places in the grid each time it is pressed? What you will be creating is a dialog with no way to control what happens between the time the dialog is displayed until the time the user clicks OK or Cancel.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Create a dialog on the fly dynamically based on an input xml file

    Sorry d_stratz,
    it seems that some text has been lost in my previous answer.

    Regarding what you said, so far I am assuming that the xml has correct values.
    But you are right, I will follow your advice and do the checks at the moment of parsing the file.

    To replace the edited value of the QLineEdit in the xml, I was thinking of using the position in the grid of the widget (basically the row number) and associating it with the position of the tag in the xml file. What do you think?
    Franco Amato

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: Create a dialog on the fly dynamically based on an input xml file

    To replace the edited value of the QLineEdit in the xml, I was thinking of using the position in the grid of the widget (basically the row number) and associating it with the position of the tag in the xml file. What do you think?
    I think it would be better to add a new field to the XML element - a "key" or "id" that you can use as the unique property the you can assign to the QLineEdit. When the edited value changes, you get the property from the widget, find the same key or id in your XML, and use that to change the value.

    Using the location of the widget in the grid to find it in the XML is fragile - if you add a new widget or change the order of the widgets in the XML, then the mapping between the grid and the XML is no longer correct. Adding a "key" that uniquely maps between widget and XML makes your layout independent of the number and placement of the widgets in the XML.

    To make it easy, you could use a QMap< QString, QDomElement * > data structure to give you a quick way to look up the XML element that gets changed whenever the QLineEdit with that key (the QString):

    Qt Code:
    1. void MyDialog::onEditingFinished()
    2. {
    3. QLineEdit * pEdit = qobject_cast< QLineEdit * >( sender() );
    4. if ( pEdit )
    5. {
    6. QString text = pEdit->text();
    7. QString key = pEdit->property( "key" ).toString();
    8.  
    9. // QMap< QString, QDomElement * > mKeyMap is a member of MyDialog
    10. QDomElement * pXML = mKeyMap[ key ];
    11. pXML->setAttribute( "myAttr", text );
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    You build the QMap when you read the XML to create the GUI. And of course you would add error checking to the code above to make sure that the "key" exists in the map, and that the map lookup has returned a non-null pointer.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Create a dialog on the fly dynamically based on an input xml file

    Yours is a good idea, but unfortunately I can't change the xml files.
    However, giving a look at the xml, I noticed that the label names are unique and I can add these names as properties of the QLineEdit widgets
    Qt Code:
    1. QLineEdit::setProperty("id", labelName);
    To copy to clipboard, switch view to plain text mode 

    Then, I can use the labelName as information to retrieve the correct modified tag (even if I don't know how yet). It is the first time that I work with xml files
    Franco Amato

Similar Threads

  1. Replies: 1
    Last Post: 12th March 2015, 06:41
  2. Replies: 0
    Last Post: 25th March 2014, 06:24
  3. Replies: 7
    Last Post: 5th February 2014, 11:20
  4. Replies: 3
    Last Post: 19th October 2011, 13:08
  5. Replies: 2
    Last Post: 28th September 2010, 10:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.