Solved Gui from xml in c++
-
@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.
-
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 -
@jsulm Can you give an idea of how to manually generate UI widgets using XML?
-
@Ayush92
The.ui
file exported from Qt Creator/Designer is an example of just such an XML file. And theuic
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 theQUILoader
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 newQWidget
s off it as specified. -
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?
-
@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. -
@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.
-
@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.htmlAlso, 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. -
@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.
-
@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.
-
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. -
@mrjj I have tried but not getting results. Can you elaborate on how can I control both UI with the same-named widgets?
-
@Ayush92
Hi
What did you try and where did you get stuck ?Do note the ObjectName and variable name is 2 different things.
-
@mrjj I am not getting a point i.e I have to mention a UI ( Ui::Landscape landscape;
Ui::Portrait portrait;) hereconnect(portrait.pushButton, SIGNAL(clicked()), this, SLOT(close()));
How can I get control of the same-named widget other UI?
-
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.