Important: Please read the Qt Code of Conduct -

What are the purposes of .ui and ui...h files?

  • Can anyone explain why there are both ui...h and .ui files for a form widget?
    I just cannot grasp the idea of having an XML file that defined the structure of a form and its UI elements, plus a .h file with the same.

    In JavaFX there is just XML to inject into an application, or you can define everything in the code. This is kind of intuitive, but both code and XML, which also can be out of sync (as I have demonstrated in the thread about disabling the shadow build to recover from QtCreator not updating the .h after X number of UI elements added to the form) sounds kind of redundant.

  • Moderators

    You can use the .ui files similarly to Java. There's a QUiLoader class for that. XML is also a good format to store ui for a visual editor (Qt Designer in this case).
    But this is C++. Performance matters. Loading an XML at runtime and parsing it may be fine for smaller UIs but in general that's sub-optimal and usually plain unacceptable.
    The uic tool parses the xml and generates a c++ header file. This file is then compiled like any other of your cpp/h files into native code and there's no additional I/O or parsing needed at runtime. The compiled ui is not a resource/data but an executable code.

  • That does not really answer my question as it does not address the reason for having both.

  • Moderators

    That does not really answer my question

    I thought it did. The reason is that XML is a better format for editors - generation, validation, merging etc. and c++ code generation and compilation is used to get better performance at runtime.

  • Treat the .h file as you treat moc_ files: you don't need them as you can generate them from the .ui files but if the source did not change you don't need to reparse and rebuild them.

    Basically, feel free to delete the generated headers

  • How can I force generation of the .h file in QtCreator?

  • Moderators

    If the .ui file is part of the project then qmake will detect changes to the file and run uic when you build. Running uic directly is not exposed in the IDE because there's no need to do that. That's the thing - the syncing you complained about does not require any interaction from you. It's done by the build system. Generated ui_...h files are implementation detail.

Log in to reply