Results 1 to 4 of 4

Thread: Creating Pushbutton dynamically using xml

  1. #1
    Join Date
    Jul 2015
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Post Creating Pushbutton dynamically using xml

    Hi all am new to Qt and i want to create a push buttons dynamically using xml without hard coding , i searched in internet, but i didnt get anything related to this.,
    what all the things i have to read for this or if you have any example program for this please give it,
    Thank you

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,378
    Thanks
    246
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Creating Pushbutton dynamically using xml

    You need to give a little more detail. "Create push buttons dynamically using XML" Where? In a dialog, a widget, floating in space in front of your monitor? When? At the time your program starts up, or any time while your program is running? How? From XML in a file? XML generated on the fly?

    Since you've done your searching, your must have come across the fact that Qt's own Qt Designer creates XML files (.ui) that the MOC compiler parses to create C++ code that creates the UI dynamically during program startup. You probably also came across the fact that Qt's QML and QtQuick also create UIs dynamically during program execution using a UI description contained in a javascript .qml file.

    You could use either of those tools. Or do you want to invent your own wheel?

  3. The following user says thank you to d_stranz for this useful post:

    Narthan (3rd July 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    11
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Post Re: Creating Pushbutton dynamically using xml

    Thank you.,
    I need to load Xml file to Qmainwindow using parsing like QXmlStreamReader , based on xml file, dynamically i need to generate pushbuttons in mainwindow and name name to that push buttons without hard coding at the start of execution

    For example

    <Dorms>
    <Dorm ID="0" Name="Dorm Building 0">
    <Room ID="0" Name="Room 0"/>
    <Room ID="1" Name="Room 1"/>
    <Room ID="2" Name="Room 2"/>
    </Dorm>
    </Dorms>

    above is the xml file , i need to read this file and i want to dynamically create a Qpushbutton and for that pushbuttons it is dynamically named as Name of the room, like room 0 room 1,
    Last edited by Narthan; 3rd July 2015 at 06:54.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,378
    Thanks
    246
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Creating Pushbutton dynamically using xml

    I would use QDomDocumentfor this instead of QXmlStreamReader.

    In your MainWindow constructor, create a simple QWidget instance and set it as the central widget. Add a layout of whatever type you want (grid, horizontal box, vertical box) depending on how you want the buttons laid out. If there are a lot of buttons, then you might want to use a QScrollArea as the central widget, and put the QWidget and layout inside that.

    Read the XML into the QDomDocument (QDomDocument::setContent()), then use QDomDocument::elementsByTagName() to get a list of all of the "Room" nodes. Go through the list of room nodes, retrieve the ID and Name for each one. Create a QPushButton with the room name and insert it into the layout. Connect the button's clicked() signal to whatever slot you want to execute when the button is clicked. You can connect them all to the same slot, bcause you can use the sender() method to determine which button invoked the slot.

  6. The following user says thank you to d_stranz for this useful post:

    Narthan (6th July 2015)

Similar Threads

  1. Dynamically creating rows og gridlayout
    By GAURAV PANT in forum Qt Quick
    Replies: 2
    Last Post: 14th March 2014, 09:06
  2. Creating Labels & Radioboxes dynamically
    By premroxx in forum Newbie
    Replies: 1
    Last Post: 9th September 2012, 00:24
  3. Dynamically creating buttons from Xml file
    By chandru@080 in forum Newbie
    Replies: 5
    Last Post: 25th November 2010, 10:34
  4. Creating dock widget as tab dynamically
    By vidyadhar in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2009, 12:03
  5. QWizard creating objects dynamically
    By afflictedd2 in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2009, 15:43

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.