Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt generate .ui file from old c++ project?

Tags:

c++

qt

I am new to Qt but have a good grasp of C++. I just learned qt design (QT5) to generate GUI and .ui file, and do coding in VS 2012.

Now there is an old project which was written purely by hand, without the qt design. To maintain it, the code still needs to be written by hand.

Is there any qt tool that can reverse-engineer the code to produce something that qt design can work on? So that any future GUI modifications can be don in qt design?

many thanks

like image 360
user1866880 Avatar asked Apr 14 '13 20:04

user1866880


2 Answers

A well prepared project will isolate application logic from the UI layer, making it easier to make changes to or entirely replace the latter. If the project you want to convert is such, it should be doable to some extent.

if you keep the same naming, the most significant difference between your imperative UI and your .ui generated UI will be the extra ui-> when you access UI members.

There is no tool to do all the work for you, but you could use QObject::dumpObjectTree() to get a tree-like representation of the UI, something like this:

QTabDialog::dialog <574,451,133,123>
      QBoxLayout::unnamed
        QBoxLayout::unnamed
        QBoxLayout::unnamed
      QPushButton::ok <45,91,82,26>
      QTabWidget::tab widget <6,6,119,75>
        QTabBar::tab control <0,0,48,24>
          QToolButton::qt_right_btn I
          QToolButton::qt_left_btn I
          QAccel::tab accelerators
        QWidget::tab base I
        QWidgetStack::tab pages <0,22,119,51>
          QFrame::first <2,2,115,47>
          QObject::unnamed
          QWidgetStackPrivate::Invisible::unnamed <2,2,115,47>

As you see, you can easily parse that and get the type and name of each object currently in the UI, you can use that information plus the meta system provided functionality to query properties and generate a XML .ui file.

Of course, you could have a more dynamic UI with elements dynamically created or removed, in which case it will be harder do to this. In that case you will have to do everything by hand.

like image 64
dtech Avatar answered Sep 21 '22 02:09

dtech


Short answer: No.

Well, there could be, but at least I do not know of any tool, which could take a C++ GUI and generate an .ui file from it. If such a thing exists, it most likely does not parse C++ code, but instead needs to be somehow attached to running application, and will then parse the GUI from it. This would actualy not be too hard to implement, as far as I can see, thanks to Qt meta object and property system allowing reflection, but I don't know of a tool like that and I doubt it exists. There are GUI testing tools which do this, but they do not produce .ui file out of the data they collect out of a running application.

However, it's perfectly reasonable to write Qt GUIs by hand, and often it is more maintainable too (as long as maintainer is fluent in Qt style C++ and QWidgets).

Also, if you need to add new widgets to the GUI (or radically modify existing widgets, so that rewriting from scratch is reasonable) then it's no problem to implement those widgets with Qt Designer (either the stand-alone version or the one integrated into Qt Creator IDE, and not to be mixed with QML Designer, even though they're behind the same icon in Qt Creator). In my opinion and experience, it is generally better to design individual widgets (for example, a page in QTabWidget, or a dialog) with Designer, but not have the entire UI as one honking big .ui file.

like image 27
hyde Avatar answered Sep 18 '22 02:09

hyde