How do I add a Custom Widget to Qt Creator?



  • Hello,

    What is the correct way to add a custom widget to Qt Creator?

    I followed the tutorial at http://doc.qt.nokia.com/4.7/designer-customwidgetplugin.html in conjunction with Qt Creator's "custom widget" new project wizard.

    But, I'm not exactly sure how to build and install the plugin. Running "mingw32-make release" & "mingw32-make release-install" seem to work, and if I launch the designer.exe program directly, my widget shows up in the list of tools.

    However, my widget does not show up in the the Qt Creator integrated designer.

    How can I rectify this?

    I'm also not entirely clear on how I would now use my custom widget in a new project, but that is separate question, perhaps.

    Thank you



  • I think that to use the plugin with designer, you might have to either compile your plugin with MSVC (like designer is by default), or compile designer with MinGw. The compilers (and compilation settings) for a program and its plugins need to match.



  • Ahh. Ok.

    Is there such a thing as a "compiler agnostic" Qt plugin system?

    The current restriction is fine for a Qt development application, but this seems un-workable if I want to allow users to write plugins for a binary-distributed Qt based application.

    Perhaps I've misunderstood the concept of Qt plugins, though.

    Is there a Qt solution for my use case?



  • No, not really. The compilers will always need to match, unless you restrict yourself to using JavaScript only/QML kind of extensions, or to calling exported C-functions only*.

    Note that this is not a Qt issue per se. You'll run into this for any application that loads extensions into its address space.

    *) I think, not really sure about this.



  • Andre, thank you for explaining.



  • As a follow up:

    I asked this question because I've got a few related (but separate) projects that I'm building with Qt Creator, and and I'd like to share a custom widget between them.

    The widget was built using the Qt Creator "custom designer widget" wizard, and is showing up in Qt Designer just fine. (albeit not the integrated version, as we discussed.)

    However, I'm pretty new to both Qt and C/C++, and I'm a bit lost in the syntax to make this sharing happen.

    Can you clarify the steps required?

    I guess the idea would be to somehow get Qt Designer to specify the right files/dlls to include?

    Thank you again



  • What I do, is to move such widgets to a separate directory "common", and create a .pri file for them. A .pri file is just like a .pro file, only it only contains what is needed for a specific component (your widget, in this case). A typical .pri file may look like this:

    @
    SOURCES *= src/fileselector.cpp
    HEADERS *= src/fileselector.h
    FORMS *= ui/fileSelectorUI.ui
    @

    Note the *= operator instead of the +=. It prevents code from being included twice by accident.

    Then, in the application .pro file, I add this:
    @
    VPATH = ../common/
    INCLUDEPATH = ../common/src/

    include( $${VPATH}fileselector.pri )
    @

    Then, I don't make designer plugins. Instead, I use Designers widget promotion feature. Just put a widget that looks a bit like what you want to have in Designer, right click it, and promote it to the widget you created. It won't show in the preview, but it will in your final code.



  • Thank you Andre, I'll try this.



  • Hey everybody!

    I hope this thread is still ok to go on with, otherwise just let me know.

    First of all, I'm very familiar with C/C++ development but yet quite new to Qt.

    What I did so far:
    Installed Qt SDK and run through some of the tutorials. So far everything works fine (at least for Windows desktop sample applications, which is actually not what I'm eventually going for - but let's skip that issue for now).

    What I would like to do:
    Get some (self-programmed or downloaded) custom widgets to be properly included into the "Qt Creator"-inherent designer, so that other people can use it to create their own applications (just as it is already possible with the built-in widgets).

    There are several reasons, why I don't agree with the above mentioned solutions:

    • The way I understand the concept of "pri"-files, the whole code would then be included into the actual application per occurrence, and therefore compiled several times (where in fact only one compilation run would suffice). This is not what a modular system should behave like. Instead, I expect widgets to be something like pre-compiled libraries, as it in plain C/C++ software development - which is possible through using them as actual libraries though.
    • "Placeholder" widgets to be promoted on compilation neglect the advantage of WYSIWYG application creation using the "Qt Creator"-inherent designer tool. However, this is a vital requirement for what I'm eventually trying to do (namely make other developers less familiar with C/C++ develop Qt applications).

    Hence, again the question as stated in the very first post:
    How is it possible to get pre-built custom widgets (for which the source code is available though) as modular components into the Qt Creator design-tool for WYSIWYG application development?

    Please be gentle if I've violated any posting rule or the like - this is my very first posting here.

    Regards



  • First of all, welcome to Qt DevNet!

    There are several ways to use self-created widgets in designer, as you found out. If you are providing a library with created widgets for others to use, then it is probably best to create a Qt Designer plugin. Qt Creator basically embeds Designer, so that works for Creator as well. Creating such a plugin basically constitutes creating a separate library that contains your widgets, as well as some classes that implement one or more specific interfaces that allow Designer to query some important meta information on your widgets. You can find a primer in the documentation "here":http://developer.qt.nokia.com/doc/qt-4.8/designer-creating-custom-widgets.html . The resulting library can then be placed at a location where Designer can find it, and it will automatically load and display the widget(s) the library supplies.

    Note that the .pri approach is not as bad you seem to think. Yes, the widgets are compiled for each application you use them in, but they are not compiled for each occurrence of the widgets. A .pri file is nothing more than a snippet of a .pro file; the file that you use for your Qt projects and that lists all your sources, headers, resource files, form/ui files, etc.



  • Hi Andre!

    Thanks for your quick reply.
    What you said on the .pri approach sounds reasonable, if not even trivial - in plain C a function from an object called several times is also only compiled once. Hence, sorry for that stupid misunderstanding.

    As for the library approach, I think I understood the concept of plugins so far: It is some sort of standardized interface for the Qt designer to determine the way the widget works. I already worked through the documentation you posted - yet, I get stuck at a stage which, unfortunately, you also do not describe in further detail: "The resulting library can then be placed at a location where Designer can find it, and it will automatically load and display the widget(s) the library supplies."
    Where exactly is that? I mean, as for the desktop designer, the libraries are to be placed into QT_SDK_ROOT_DIR\Desktop\Qt\4.7.4\mingw\plugins\designer (at least for my installation).
    If I start the desktop designer located in QT_SDK_ROOT_DIR\Desktop\Qt\4.7.4\mingw\bin, the implementation of the widgets as plugin works just fine, meaning I can add the custom widgets to UI files just as it is possible with the default widgets.
    However, the Qt Creator (located in QT_SDK_ROOT_DIR\QtCreator) does not know about that, and so far I found no way to tell the creator where the custom plugins can be found. By the way, there's also a second designer.exe located in QT_SDK_ROOT_DIR\QtCreator\bin (which btw does not know about the plugins either). What about that? What's its purpose? And why is this exe of different size than the exe located in QT_SDK_ROOT_DIR\Desktop\Qt\4.7.4\mingw\bin?

    Too many questions, I know... :-)

    Regards



  • Hi everybody!

    I just follow this "document":http://developer.qt.nokia.com/doc/qt-4.8/designer-creating-custom-widgets.html and build custom widget XMLTreeViewer with profile MSVC. Qt Creator work fine when files: xmltreeviewer.h and xmltreeviewerplugin.dll were placed in: QtSDK\Desktop\Qt\4.7.4(msvc2008 or mingw)\include\ and QtSDK\QtCreator\bin\designer\ accordingly.



  • Hi rimen!

    Unfortunately that doesn't work for me - either because I'm using the MinGW toolchain instead, or because I'm missing something important.
    But am I understanding this correctly? You put:

    • the header file into QtSDK\Desktop\Qt\4.7.4(msvc2008 or mingw)\include\
    • the DLL into QtSDK\QtCreator\bin\designer\

    What about the plugin directory in QtSDK\Desktop\Qt\4.7.4(msvc2008 or mingw)? Shouldn't plugin DLLs (or static libraries) usually be placed there?

    It's really annoying. Prehaps someone could follow up the steps I've done and track down my mistake:
    First of all, I created the textfinder according to the tutorial supplied with QtCreator.
    Then, I created the customwidgetplugin (see "here":http://developer.qt.nokia.com/doc/qt-4.8/designer-customwidgetplugin.html ): I copied the project and its source code from the "QtSDK\Examples" folder into my workspace (which should be the finished working example) and built it from there.
    Now, as written above, if I copy the customwidgetplugin.a and .dll into the plugins directory (see earlier post), the QtDesigner located in the MinGW directory works fine (i.e. I can select the new plugin from the plugin dropdown list, it even has a proper preview icon there) but the QtCreator-inherent designer tool won't see it when I want to add it to my textfinder UI using the designer.
    Perhaps there is something wrong with the customwidgetplugin example, which is why it won't work?
    Or is there something special I need to do with my textfinder project to be able to include custom widget plugins?

    Regards



  • Indeed, you cannot install a MinGW compiled plugin into a MSVC compiled application like QtCreator. That means that you either:

    • have to recompile Creator with MinGW, or
    • you have to install MSVC (you can get the Visual Studio 2008 Express edition for this, perhaps) and compile your plugin using that compiler


  • Ok, that might be the explanation. In my understanding, this also explains why there are two versions of the Qt Designer - one as part of the (MSVC-based) QtCreator and another in the MinGW desktop development environment - of which only the MinGW-version can recognize the plugin.

    But that also means, that plugins cannot be used independently from the IDE build type, right?
    That's very inconvenient, as my eventual purpose is to cross-compile Qt Applications (using custom widgets) on Windows PCs for QNX.

    Is there any way to do this?
    Of course, I could go on without using the designer, but as written in a previous post, the people which finally will create the Qt applications aren't very familiar with C/C++ and hence should be able to use the Qt Creator as a visual WYSIWYG IDE. Well, some would set up a virtual machine running Linux or QNX and do all that stuff from there, but that isn't really a solution for me, unfortunately.



  • An application can not load a plugin that has been compiled with a non-compatible toolchain or -settings. That is true for any application. It is not a limitation of Qt, but of the way binaries are structured and libraries can be loaded into addressespaces, functions resolved, etc. Don't ask me for the details on that though, I only know the very vague outlines of that.

    You should realize that the Designer plugin is just that: a plugin for Designer (and Qt Creator). It is not needed to successfully build or run your application itself!

    If you are worried that your users won't be able to setup their development environment, then I suggest you build them a complete package and distribute that. That way, you can make sure that you have all the right versions of the needed tools available.

    One final note: I am not sure if it is smart to target people that are supposed to build Qt applications, but are not very familiar with C++. Creator is not a complete point-and-click-to-build-applications kind of solution.



  • Hi Andre!

    First of all, thanks for your extensive and quick support.

    Sure, I know about the way binaries and libraries in C/C++ work, hence it is clear to me that I cannot build a Windows MinGW (or MSVC) widget and use that in a non-Windows Qt application.

    But, of course, what I CAN do is building C/C++ applications for a specific hardware/OS target and use libraries therein, which are equally built for the same target. That is actually what C/C++ is all about. But here comes the trick with cross-compiling: Both libraries and binaries created (for the same target) are completely independent from the host environment (setting aside the fact, that I need an appropriate toolchain with all includes, binaries, libraries, tools and the like available, which are required for cross-compilation).

    But as you mentioned, I need a Qt Creator IDE which is built with the same (!) toolchain as the widget plugin is built with to make it available to the Qt Designer. Of course, I could cross-compile without using the designer - that would make me independent from the host system (as for plain C/C++ development) as required, but neglect the advantage of sort of WYSIWYG development.

    But probably you're right: In the end, Qt is C/C++ and requires at least basic knowledge of what's going on there - particularly if you want someone to develop for a more "exotic" purpose like cross-compiling for QNX.

    Nevertheless, thanks for all your support.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.