Home > other >  How to generate program code from ui-forms?
How to generate program code from ui-forms?

Time:11-19

I have a UI, generated with Qt Designer. It's generates me a XML code like these:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ColorDialog</class>
 <widget class="QDialog" name="ColorDialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
   and etc

I want to remove this UI and edit a view by C code like these:

    for (auto button:m_buttonColorsList)
    {
        m_colorsLayout->addWidget(button);
    }

    m_mainGroupBox->setLayout(m_colorsLayout);

    m_mainLayout->addWidget(m_mainGroupBox);

    setLayout(m_mainLayout);

How I can convert the exiting form to code?

CodePudding user response:

If you want to generate code using the .ui information then you must use uic tool:

uic filename.ui -g cpp -o filename_ui.h

CodePudding user response:

The Qt Designer generates this XML, which is then, in turn, used by QMake to convert into actual code. However there is nothing in there you cannot write yourself.

The easiest way for you to achieve what you want is, to simply use the means Qt already gives you: Create the form with the designer, run QMake over it and then look at the generated .h file.

The entire code ( some extras) is in there, ready to be copied to your application without the whole ui-> business.

If you don't want to use QMake no matter what, you'll have to go about it manually, though in my experience it's best to not look at the XML for that, but instead do the entire layouting by yourself.

It's a bit of a hassle, but definitely doable.

  • Related