Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding user built widgets to a ui file in qt creator

The qt designer portion of qt creator has many built in widgets. But let's say I want to add custom widgets created in the same qt project to the ui file of the window. By taking these steps:

  1. Create a new Qt GUI application with a main window, we'll call the window A.
  2. Add a new widget to the project, the widget just uses standard UI components, say buttons. We'll call this widget B.
  3. Add an instance of widget B to window A.

Now, I know one way to do that, and that is:

  1. In window A, add a blank widget (or widget container, from the containers section of the list of possible widgets. We'll call this widget C.
  2. Promote it (widget C) to widget B.

However, the problem with this is that Qt Creator's designer treats it like a generic QWidget. And as such, you can't do things like add it to a splitter, or connect signals/slots that are specific to the widget.

So is there any other ways to add widget B to window A in the ui file using qt creator? Thank you.

like image 300
Leif Andersen Avatar asked Feb 26 '12 18:02

Leif Andersen


People also ask

How do I add widgets to Qt?

To add widgets in Qt Designer: In the Qt Creator Edit mode, double-click the notepad. ui file in the Projects view to launch the file in the integrated Qt Designer. Drag and drop widgets Text Edit (QTextEdit) to the form.

How do you edit a .ui File?

ui file in the project explorer, click Open With and then Plain Text Editor. This gives you the same editor but with write mode. Note that the disabled write mode for ui files when double clicking and changing to "Edit" mode rather than "Design" mode isn't a bug but a feature.

How do I create a Qt ui File?

To create a . ui file go to File -> New File or Project... In the window that appears select Qt under Files and Classes on the left, then select Qt Designer Form on the right. You'll notice the icon has "ui" on it, showing the type of file you're creating.


2 Answers

I'm not sure to understood your question well so I could ask the wrong question. Are you sure your "B" widget is a subclass of QDesignerCustomWidgetInterface? This should expose all stuff that your widget/plugin offers...

Last note: a friend of mine tried to add a custom widget like you. And at the end of the described procedure that Lol4t0 told you, he found you must compile plugin with the same compiler with wich qtcreator/designer was compiled. This happens because as we know c++ doesn't keep ABI compability (instead of i.e. C language) stuff like: Name handling can change from compiler to compiler, how data is loaded into registers can change...and so on. My friend tried to compile plugin with mingw, but he found that qtcreator was compiled with visual studio compiler. Therefore if you want to deploy your plugin on Windows or you compile your plugin with visual studio, or you have to compile qtcreator/designer from scratch.

like image 175
Alberto Avatar answered Sep 16 '22 15:09

Alberto


I know this is a very old question, and I'm not sure what capabilities Designer had in 2012, but I came across this in a Google search for something else and figured I'd add some missing info:

However, the problem with this is that Qt Creator's designer treats it like a generic QWidget. And as such, you can't do things like add it to a splitter, or connect signals/slots that are specific to the widget.

Generic QWidgets can be added to splitters with no issues these days.

As far as signals and slots go, you can use them like so:

  1. After promoting a widget to your custom widget, right-click it and choose "Change signals/slots..." from the context menu.
  2. Add the signatures for any custom signals and slots you want to be able to use in Designer / Creator here.

Now those signals and slots will be accessible in Designer like any other signals and slots.

The only thing you can't really get with promoted widgets is access to custom properties in the property panel; for that, yeah, you'll need to go through the custom widget creation process.

like image 41
Jason C Avatar answered Sep 17 '22 15:09

Jason C