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 Offline
    S Offline
    SilverLuna
    wrote on 6 Oct 2020, 05:25 last edited by
    #1

    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>

    J 1 Reply Last reply 6 Oct 2020, 05:27
    1
    • S SilverLuna
      6 Oct 2020, 05:25

      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>

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 6 Oct 2020, 05:27 last edited by jsulm 10 Jun 2020, 05:32
      #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

      A 1 Reply Last reply 19 Oct 2020, 11:17
      6
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 6 Oct 2020, 05:33 last edited by mrjj 10 Jun 2020, 06:37
        #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
        • J jsulm
          6 Oct 2020, 05:27

          @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.

          A Offline
          A Offline
          Ayush92
          wrote on 19 Oct 2020, 11:17 last edited by
          #4

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

          J 1 Reply Last reply 19 Oct 2020, 11:26
          0
          • A Ayush92
            19 Oct 2020, 11:17

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

            J Offline
            J Offline
            JonB
            wrote on 19 Oct 2020, 11:26 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.

            A 1 Reply Last reply 19 Oct 2020, 11:42
            2
            • J JonB
              19 Oct 2020, 11:26

              @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.

              A Offline
              A Offline
              Ayush92
              wrote on 19 Oct 2020, 11:42 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?

              J 1 Reply Last reply 19 Oct 2020, 12:29
              0
              • A Ayush92
                19 Oct 2020, 11:42

                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?

                J Offline
                J Offline
                JonB
                wrote on 19 Oct 2020, 12:29 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.

                A 1 Reply Last reply 20 Oct 2020, 06:04
                2
                • J JonB
                  19 Oct 2020, 12:29

                  @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.

                  A Offline
                  A Offline
                  Ayush92
                  wrote on 20 Oct 2020, 06:04 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.

                  M J 2 Replies Last reply 20 Oct 2020, 06:42
                  0
                  • A Ayush92
                    20 Oct 2020, 06:04

                    @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.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 20 Oct 2020, 06:42 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.

                    A 1 Reply Last reply 22 Oct 2020, 05:17
                    1
                    • A Ayush92
                      20 Oct 2020, 06:04

                      @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.

                      J Offline
                      J Offline
                      JonB
                      wrote on 20 Oct 2020, 08:15 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
                      • M mrjj
                        20 Oct 2020, 06:42

                        @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.

                        A Offline
                        A Offline
                        Ayush92
                        wrote on 22 Oct 2020, 05:17 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.

                        M 1 Reply Last reply 22 Oct 2020, 05:32
                        0
                        • A Ayush92
                          22 Oct 2020, 05:17

                          @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.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 22 Oct 2020, 05:32 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.

                          A 1 Reply Last reply 22 Oct 2020, 08:32
                          2
                          • M mrjj
                            22 Oct 2020, 05:32

                            @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.

                            A Offline
                            A Offline
                            Ayush92
                            wrote on 22 Oct 2020, 08:32 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?

                            M 1 Reply Last reply 22 Oct 2020, 08:36
                            0
                            • A Ayush92
                              22 Oct 2020, 08:32

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

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 22 Oct 2020, 08:36 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.

                              A 1 Reply Last reply 22 Oct 2020, 08:43
                              1
                              • M mrjj
                                22 Oct 2020, 08:36

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

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

                                A Offline
                                A Offline
                                Ayush92
                                wrote on 22 Oct 2020, 08:43 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?

                                M 1 Reply Last reply 22 Oct 2020, 09:35
                                0
                                • A Ayush92
                                  22 Oct 2020, 08:43

                                  @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?

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 22 Oct 2020, 09:35 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