Why is that bad? Idea to embed widget in Qml based application
I need to reuse Widget app in Qml based app with latest Qt (Qt 5.2). But according to most people, it is a very bad idea to do this.
Can someone explain why this is a bad idea?
Some code snippets,
*. H
class MyAppItem: public QQuickPaintedItem{
Q_OBJECT
public:
explicit MyAppItem(QQuickItem *parent = 0);
void paint(QPainter *painter);
private:
CMyAppWidget *bp;
};
class RouteBWExtensionPlugin: public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
/**
* @brief register the plugin
* @param[in] uri to be registered
*/
void registerTypes(const char * uri);
};
*. CPP
MyAppItem::MyAppItem(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
bp = new CMyAppWidget();
}
void MyAppItem::paint(QPainter *painter)
{
bp->render(painter);
}
void RouteBWExtensionPlugin::registerTypes(const char * uri)
{
qmlRegisterType<MyAppItem>(uri, 1, 0, "MyAppItem");
}
*. qml file
import MyAppWidget 1.0
Item {
width: 300
height: 10
anchors.right: parent
MyAppItem {
width: 94
height: 240
anchors.right: parent
MouseArea{
anchors.fill: parent
onClicked: {
console.log("[veo] onClicked - capture triggered")
}
}
}
}
source to share
Because you don't want to add a 3d rendering dependency when you don't need to. 3D rendering can cause a massive amount of problem that you may be able to avoid in a Qt Widgets application without Qt Quick.
If you are planning to develop a Qt Quick application, it is likely that you will need things from Qt Widgets at some point. This applies, for example, when you need the correct file dialogs , tray icons, or simply want your own system colors, which you have in Qt Widgets QApplication
but not in Qt Quick QGuiApplication
. So I would mind the Micht point. An unnecessary dependency on Qt Widgets based on my own experience.
So, for new Qt Quick applications using existing QWidgets, I think this is a good intermediate step.
source to share
It's interesting, I didn't know you could do this!
Here are some reasons I might be thinking:
- Waste memory overhead for storing and building each instance
QWidget
(this includes any signal / slot connections that the widgets can create). - Executing code
QWidget::render()
unnecessarily. I didn't consider how difficult it is, but it is something to consider. - Unnecessary dependency on Qt Widgets.
- Loss of interaction (mouse, keyboard, touch, etc.), suggesting the widget has something in the first place.
This all assumes that you don't need the widget dependency for other things. If for some reason you need it elsewhere within the same application, it doesn't seem so crazy (seems weird after writing this ...). I would be very interested to know the reasons why this is a bad idea.
So the question is, why can't you just move the drawing commands out of the widget and directly into your implementation QQuickPaintedItem::paint()
? Do you need widgets in your Qt Quick application for other things? If so, what?
source to share