Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Gui from xml in c++
Forum Updated to NodeBB v4.3 + New Features

Gui from xml in c++

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 3.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SilverLuna

    Hi I have to prepare an application which in c ++ will generate gui from xml file at application startup. Is it possible at all?

    for example:

    File
    <Name="Name1" ReadOnly="true" >
    <Header="header1"/>
    <Type ="Enum" >
    <enum1 >
    <enum2>
    </Type>
    <UserPermission= "per1">
    <Path="..s.s.s.s">
    </Name>

    <Window Name="Win1">
    <Name1>
    </Window>

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by jsulm
    #2

    @SilverLuna said in Gui from xml in c++:

    Is it possible at all?

    Of course, but you will have to implement that by yourself. Parse the XML (Qt provides classes for that) and generate the UI widgets according to the XML content.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    Ayush92A 1 Reply Last reply
    6
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #3

      Hi
      If the XML format is not
      already fixed, then please notice that Qts own UI files are in fact a
      XML files.
      Those files can be directly loaded with
      https://doc.qt.io/qt-5/quiloader.html

      1 Reply Last reply
      6
      • jsulmJ jsulm

        @SilverLuna said in Gui from xml in c++:

        Is it possible at all?

        Of course, but you will have to implement that by yourself. Parse the XML (Qt provides classes for that) and generate the UI widgets according to the XML content.

        Ayush92A Offline
        Ayush92A Offline
        Ayush92
        wrote on last edited by
        #4

        @jsulm Can you give an idea of how to manually generate UI widgets using XML?

        JonBJ 1 Reply Last reply
        0
        • Ayush92A Ayush92

          @jsulm Can you give an idea of how to manually generate UI widgets using XML?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #5

          @Ayush92
          The .ui file exported from Qt Creator/Designer is an example of just such an XML file. And the uic compiler processes that into source C++ code, or you can use QUiLoader class at runtime. You might want to take a look at that (shame if you cannot use that existing XML format?), or the QUILoader class source code (e.g. https://code.woboq.org/qt5/qttools/src/designer/src/uitools/quiloader.cpp.html, https://code.woboq.org/qt5/qttools/src/designer/src/lib/uilib/formbuilder.cpp.html) can give you some ideas. It's just a matter of reading whatever XML schema you choose to define, and then creating new QWidgets off it as specified.

          Ayush92A 1 Reply Last reply
          2
          • JonBJ JonB

            @Ayush92
            The .ui file exported from Qt Creator/Designer is an example of just such an XML file. And the uic compiler processes that into source C++ code, or you can use QUiLoader class at runtime. You might want to take a look at that (shame if you cannot use that existing XML format?), or the QUILoader class source code (e.g. https://code.woboq.org/qt5/qttools/src/designer/src/uitools/quiloader.cpp.html, https://code.woboq.org/qt5/qttools/src/designer/src/lib/uilib/formbuilder.cpp.html) can give you some ideas. It's just a matter of reading whatever XML schema you choose to define, and then creating new QWidgets off it as specified.

            Ayush92A Offline
            Ayush92A Offline
            Ayush92
            wrote on last edited by Ayush92
            #6

            My agenda is this [https://stackoverflow.com/questions/64370459/want-to-change-orientation-from-landscape-to-portrait-view-of-qt-c-application?noredirect=1#comment113829091_64370459]. So I want to do changes in header files like ui_x.h.So I am using external XML for changing the size of the widgets in UI for different orientations. So I parsed XML and want to duplicate ui_x.h files and change UI. Basically, I want two different UI XML files having different sizes of widgets for the same class. Is this possible?

            JonBJ 1 Reply Last reply
            0
            • Ayush92A Ayush92

              My agenda is this [https://stackoverflow.com/questions/64370459/want-to-change-orientation-from-landscape-to-portrait-view-of-qt-c-application?noredirect=1#comment113829091_64370459]. So I want to do changes in header files like ui_x.h.So I am using external XML for changing the size of the widgets in UI for different orientations. So I parsed XML and want to duplicate ui_x.h files and change UI. Basically, I want two different UI XML files having different sizes of widgets for the same class. Is this possible?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #7

              @Ayush92
              Well, as you can see from the response there, your question got shot down on stackoverflow! Don't be too put off, they are an unfriendly bunch :)

              First, let me say I know nothing about xrandr, or whether it has any suitability here.

              I would suggest that you do not want to do what you are asking for in this question here. You could approach by, as you ask here, doing something like designing the page(s) multiple times in Qt Designer, and then probably loading one of the two .ui files into your widget dynamically at runtime (probably easier than generating two classes). But that means maintaining separate design-time files, and what happens if you want this for multiple pages?

              But it looks like you should be able to design this just once and then use the various Qt layouts to automatically reposition/resize at runtime according as the screen varies for landscape/portrait. Which involves no more than getting the layouts right for what you want. Or, if you do need to, do you understand that you can design sizes at design-time but then have code which alters the size/position dynamically at runtime? You do not have to design forms/layouts via XML/.ui file, you can do everything that achieves via code at runtime. I think you are thinking the XML is mandatory for Qt widgets, but it is not.

              Ayush92A 1 Reply Last reply
              2
              • JonBJ JonB

                @Ayush92
                Well, as you can see from the response there, your question got shot down on stackoverflow! Don't be too put off, they are an unfriendly bunch :)

                First, let me say I know nothing about xrandr, or whether it has any suitability here.

                I would suggest that you do not want to do what you are asking for in this question here. You could approach by, as you ask here, doing something like designing the page(s) multiple times in Qt Designer, and then probably loading one of the two .ui files into your widget dynamically at runtime (probably easier than generating two classes). But that means maintaining separate design-time files, and what happens if you want this for multiple pages?

                But it looks like you should be able to design this just once and then use the various Qt layouts to automatically reposition/resize at runtime according as the screen varies for landscape/portrait. Which involves no more than getting the layouts right for what you want. Or, if you do need to, do you understand that you can design sizes at design-time but then have code which alters the size/position dynamically at runtime? You do not have to design forms/layouts via XML/.ui file, you can do everything that achieves via code at runtime. I think you are thinking the XML is mandatory for Qt widgets, but it is not.

                Ayush92A Offline
                Ayush92A Offline
                Ayush92
                wrote on last edited by
                #8

                @JonB Hi, I tried this method but no clue about how to access so many child widgets of loaded Ui to get landscape/portrait view of UI.

                mrjjM JonBJ 2 Replies Last reply
                0
                • Ayush92A Ayush92

                  @JonB Hi, I tried this method but no clue about how to access so many child widgets of loaded Ui to get landscape/portrait view of UI.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Ayush92
                  Hi
                  You are not very clear about what you want to do besides having the GUI in landscape mode.

                  Do you want to enumerate all widgets at Runtime to change positions to adjust to landscape or
                  what is it you are trying to do ?

                  Often one will simply have 2 GUI premade and switch between like that talk about here
                  https://doc.qt.io/archives/qt-4.8/qt-widgets-orientation-example.html

                  Also, you confuse me with the XML part.
                  Is this something you must have ?? As for QT , you can directly design this
                  and not care about XML/UI files but it keeps
                  pop-up up but its not clear why you need xml files or "gui from xml" files.

                  Ayush92A 1 Reply Last reply
                  1
                  • Ayush92A Ayush92

                    @JonB Hi, I tried this method but no clue about how to access so many child widgets of loaded Ui to get landscape/portrait view of UI.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #10

                    @Ayush92
                    Looking at the screenshots you referenced in your stackoverflow post, they seem to have all the same elements in the same order. The only difference is the screen resolution for portrait vs landscape.

                    I do not see any need for separate designs. Just use Qt layouts & size policies, and the same source will hopefully adjust for landscape or portrait appropriately. In any case, even within landscape/portrait you will need to do the work to allow for different device resolutions/sizes, so when you've done that it should be good for landscape vs portrait too.

                    1 Reply Last reply
                    3
                    • mrjjM mrjj

                      @Ayush92
                      Hi
                      You are not very clear about what you want to do besides having the GUI in landscape mode.

                      Do you want to enumerate all widgets at Runtime to change positions to adjust to landscape or
                      what is it you are trying to do ?

                      Often one will simply have 2 GUI premade and switch between like that talk about here
                      https://doc.qt.io/archives/qt-4.8/qt-widgets-orientation-example.html

                      Also, you confuse me with the XML part.
                      Is this something you must have ?? As for QT , you can directly design this
                      and not care about XML/UI files but it keeps
                      pop-up up but its not clear why you need xml files or "gui from xml" files.

                      Ayush92A Offline
                      Ayush92A Offline
                      Ayush92
                      wrote on last edited by Ayush92
                      #11

                      @mrjj I have tried oreintation example but don't know how to tackle so many QWidget simultaneously for heavy application.I have to write duplicate code for the same line for both UI like this

                      connect(portrait.pushButton, SIGNAL(clicked()), this, SLOT(close()));
                      connect(landscape.pushButton, SIGNAL(clicked()), this, SLOT(close()));
                      
                      connect(portrait.pushButton_LandscapeMode, SIGNAL(clicked()), this, SLOT(sl_LandscapeView()));
                      connect(landscape.pushButton_LandscapeMode, SIGNAL(clicked()), this, SLOT(sl_LandscapeView()));
                      connect(portrait.pushButton_PortraitMode, SIGNAL(clicked()), this, SLOT(sl_PortraitView()));
                      connect(landscape.pushButton_PortraitMode, SIGNAL(clicked()), this, SLOT(sl_PortraitView()));
                      

                      Please guide.

                      mrjjM 1 Reply Last reply
                      0
                      • Ayush92A Ayush92

                        @mrjj I have tried oreintation example but don't know how to tackle so many QWidget simultaneously for heavy application.I have to write duplicate code for the same line for both UI like this

                        connect(portrait.pushButton, SIGNAL(clicked()), this, SLOT(close()));
                        connect(landscape.pushButton, SIGNAL(clicked()), this, SLOT(close()));
                        
                        connect(portrait.pushButton_LandscapeMode, SIGNAL(clicked()), this, SLOT(sl_LandscapeView()));
                        connect(landscape.pushButton_LandscapeMode, SIGNAL(clicked()), this, SLOT(sl_LandscapeView()));
                        connect(portrait.pushButton_PortraitMode, SIGNAL(clicked()), this, SLOT(sl_PortraitView()));
                        connect(landscape.pushButton_PortraitMode, SIGNAL(clicked()), this, SLOT(sl_PortraitView()));
                        

                        Please guide.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #12

                        @Ayush92

                        HI
                        Sorry. The example does that but it's actually a bit silly.
                        There is no need to name the variables according to the orientation.
                        Simply have different UI files ( portrait/landscape)
                        and all Widgets can be named the same and the connections can be reused.

                        Ayush92A 1 Reply Last reply
                        2
                        • mrjjM mrjj

                          @Ayush92

                          HI
                          Sorry. The example does that but it's actually a bit silly.
                          There is no need to name the variables according to the orientation.
                          Simply have different UI files ( portrait/landscape)
                          and all Widgets can be named the same and the connections can be reused.

                          Ayush92A Offline
                          Ayush92A Offline
                          Ayush92
                          wrote on last edited by
                          #13

                          @mrjj I have tried but not getting results. Can you elaborate on how can I control both UI with the same-named widgets?

                          mrjjM 1 Reply Last reply
                          0
                          • Ayush92A Ayush92

                            @mrjj I have tried but not getting results. Can you elaborate on how can I control both UI with the same-named widgets?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @Ayush92
                            Hi
                            What did you try and where did you get stuck ?

                            Do note the ObjectName and variable name is 2 different things.

                            Ayush92A 1 Reply Last reply
                            1
                            • mrjjM mrjj

                              @Ayush92
                              Hi
                              What did you try and where did you get stuck ?

                              Do note the ObjectName and variable name is 2 different things.

                              Ayush92A Offline
                              Ayush92A Offline
                              Ayush92
                              wrote on last edited by Ayush92
                              #15

                              @mrjj I am not getting a point i.e I have to mention a UI ( Ui::Landscape landscape;
                              Ui::Portrait portrait;) here

                              connect(portrait.pushButton, SIGNAL(clicked()), this, SLOT(close()));
                              

                              How can I get control of the same-named widget other UI?

                              mrjjM 1 Reply Last reply
                              0
                              • Ayush92A Ayush92

                                @mrjj I am not getting a point i.e I have to mention a UI ( Ui::Landscape landscape;
                                Ui::Portrait portrait;) here

                                connect(portrait.pushButton, SIGNAL(clicked()), this, SLOT(close()));
                                

                                How can I get control of the same-named widget other UI?

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @Ayush92

                                Hi
                                That is a variable name. so That can be the same for 2 separate UI files.
                                So you can have say 2 UI files name file1.ui and file2.ui
                                and both have a Button, that you can reference with the same variable name.

                                Its important they are separate files as else Designer WILL make them other name as the variable name is based on the objectname and there cant be 2 widgets with same objectname.
                                But we can have same variable names with no issues between 2 files.

                                1 Reply Last reply
                                1

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved