I have to reuse the Widget application in the Qml based application with latest Qt version (Qt 5.2). But as per most of the people its is very bad idea to do so.
Can someone explain, why it is bad idea?
Some of the code snippet,
*.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")
}
}
}
}
That's interesting, I didn't know that you could do that!
Here are some reasons that I can think of:
QWidgetinstance (this includes any signal/slot connections the widgets might make).QWidget::render()code path unnecessarily. I haven't looked into how complex this is, but it's something to consider.These all assume that you don't need the widget dependency for other things. If you do need it elsewhere within the same application for some reason, it doesn't sound so crazy (feels weird writing this...). I'd actually be very interested in hearing reasons why this is a bad idea myself.
So, the question is, why can't you just move the painting commands out of the widget and directly into your
QQuickPaintedItem::paint()implementation? Do you need widgets within your Qt Quick application for other things? If so, what?