QML and Qt Creator - qt

I designed a Qt application by Qt creator. As you well know when you build a new form is possible to drag an drop the default items inside the main window. Instead of use classical "push button" I created a custom button by adding .qml file to the project. The problem now is that I don't know how I can use (or integrate) the new button inside the form of my project.
Thanks in advance

As long as your component is in the path of your application qml files, all you need to use your component is to place it somewhere. You don't need to include or import anything. Any user component is directly available to the entire project.
As long as the QML component is made from only built in components, it can even be safely loaded from arbitrary location on disk, over network or just from a source string. Check this answer for details on dynamic instantiation.
A friendly advice - type the code, do not use the visual editor - it is pretty weak.
EDIT: I don't know about you, but for me, seems like every custom qml file in the project qml folder is automatically added to the QML types in the designer library. So contrary to what I assumed, you shouldn't really need to do anything to get your custom type available for use in the designer.

I found this 1 year-old question when facing the same problem. This is what I found out:
If you save your custom button with an initial uppercase letter (CustomButton.qml instead of customButtom.qml), QtDesigner shows your component in the library panel correctly.
Sometimes you need to restart QtDesigner to work.

Related

Embed QML Dialog inside QDialog window at runtime

I've never used QML before and I don't know if the following is even possible at all.
I'm currently porting a C++ Qt Application to Qt 5.5 with existing code and lot of UI files. This application later loads a lot of C++ plugin dll at runtime. Now every plugin should have its own configuration dialog; although none of these plugins currently have a Qt dependency.
I'm currently thinking about extending the interface with something like:
class CPlugin {
public:
virtual std::string const& getQmlDescription() const;
virtual std::string const& getQmlFilePath() const;
};
So every plugin can return a set of QML data on how it wishes it's configuration dialog to look like.
When this plugin should get configured by the user, this application displays an empty QDialog and asks the plugin "give me your qml config data"; which is then rendered and executed inside the empty QDialog.
Can this QML data in terms of a string buffer or a file path to the QML data be interpreted and rendered into a empty QDialog at runtime?
Bottom line:
Can QML be handled like this at runtime?
Can I embed a QML Dialog description inside a traditional QDialog window or does these two types not mix?
Is this even remotely a good idea or should I do a hell lot differently? :)
There's no reason to use a QDialog. You could do it, but a QDialog is rendered using the raster paint engine, and you'd be using the CPU to composite that with the OpenGL frame buffer that Qt Quick renders into. It'd be a bad premature pessimization.
In your scenario, the QML from the plugin would be passed to QQuickView. You will also find that the plugins will need access to the QML engine to set up the context objects to interface C++ to QML, and to register their objects. You can use a global engine instance, and pass it to plugins. Or, to isolate them, use a dedicated engine that only services these plugins.
Qt Quick is flexible in that you are not forced to have the Qt Quick items from the plugins in separate windows. You can set them up in a flickable front-end for mobile devices, dockable front-end for desktop applications, etc. The visual items from the plugins can be instantiated any way you wish - multiple times, inside of other items, etc.
So, it might be a better idea for the plugins to register their visual Item types with the engine, and let your application decide how to use these types, via a Loader, instead of simply passing you raw QML to deal with.

Qt GUI in designer or in code?

When it comes to designing a GUI in Qt, I am hesitating between using the designer in Qt Creator, or doing everything in source code. I'm using Qt widgets and not QML.
If I use the designer I can easily create a GUI using qt standard widgets. But as soon as I need to subclass a widget to extend its functionality I have to build a Designer plugin to support my new widget. Is that correct? Or is there another way to it?
You can build all the GUI in Designer including custom widgets, and you can also build your custom widgets in Designer.
Designer does not need to interpret your custom widgets. Just use the promote functionality. With promote, you start with a plain widget within Designer and then tell the "real" class of it (your custom one) and the header file where it is decleared. The only drawback is that within Designer, it will stay looking like an empty widget.
In my experience, it is much better to use Designer for the GUI than writing source code yourself. You can easily change all the properties afterwards etc., and it is helpful even if you rely on custom widgets. Source code is not a good declarative language for GUI objects, with all the properties etc. Also you cannot play around, you would need to compile all the time just to tell "Is it better to have this text label in bold font?".
Sometimes I edit the XML files that are created by Designer by hand. For example, if I want to put a widget somewhere else in the object tree. If you don't mess up the XML, Designer will still read it and not destroy your changes. The only reason I see for writing GUI in source code is when you have repetitive elements, or dynamic changes based on data input, e.g. a for()-loop that produces elements. In my project I have some Selector Boxes that are filled with options in the source code.
And btw: If you prefer to write your GUI in code instead of using Designer, maybe you are not the right person to craft the GUI. Most programmers don't understand that while they are technically able to design a GUI, they are not always also competent in doing it.
http://hallofshame.gp.co.at/index.php?file=shame.htm&mode=original
It is a bit of a shortcut, but I often use a simple QWidget as a container for my custom widget. This way, I can setup sizing policies, put the whole thing in the layout I want before my custom widget is even in. Then, in C++, I add the custom widget as a child of the container widget.
edit: As ypnos mentioned you can promote the placeholder directly. You can find guidelines here

Good reference for editing Qt .ui files?

At times I find myself editing the XML files on Qt creator rather than using the drag and drop interface for example if I can't get my widgets in the correct row of my grid layout. Is there a good reference anywhere on how to lay UIs out using XML? For example, I'd been interested in altering the visibility of a checkbox in the XML rather than in C++ code... is this doable, or am I too much in the WPF mindset? Either way, I'd still be interested in that reference.
You may find the Qt Designer's UI File Format page useful.
I generally just use the designer to prototype something quickly, and then code it up in C++ once the design is correct. Breaking the UI up into custom widgets, rather than one massive form often produces something more manageable.
I've been using Qt for 4 years but I never modified the UI files directly. I always do it using Qt Designer. Not sure why you ever need to modify the XML file directly.
This is not answer to your question, but I believe you're missing how to use Qt Designer. Please check Qt Designer's Manual first.

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:
Create a new Qt GUI application with a main window, we'll call the window A.
Add a new widget to the project, the widget just uses standard UI components, say buttons. We'll call this widget B.
Add an instance of widget B to window A.
Now, I know one way to do that, and that is:
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.
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.
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.
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:
After promoting a widget to your custom widget, right-click it and choose "Change signals/slots..." from the context menu.
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.

How to make own input method in Qt application?

I would like to make an input method which is used only for Qt desktop application.
It like Chinese(Pinyin) input method in windows. Include script processing, rendering of words.
As it includes rendering of words, it can't be created with Keyboard Layout.
More over, when built-in with application, it can be use cross over other platform.
But, It not like on-screen keyboard.
Thanks for all
The Qt way to implement this is to provide an input method plugin, see general plugin development docs and the input method specific base class.
With this you should be able to implement your own input method. Stuff like script processing and rendering is then up to your own plugin.

Resources