我必须在具有最新 Qt 版本(Qt 5.2)的基于 Qml 的应用程序中重用 Widget 应用程序。但对于大多数人来说,这样做是非常糟糕的主意。
有人可以解释一下,为什么这是个坏主意?
一些代码片段,
*.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 文件
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")
}
}
}
}
最佳答案
因为您不想在不需要时添加对 3D 渲染的依赖。 3D 渲染可能会导致 massive金额trouble ,您可以在没有 Qt Quick 的 Qt Widgets 应用程序中避免这种情况。
如果您计划开发 Qt Quick 应用程序,那么在某些时候您可能需要 Qt Widgets 中的东西。例如,当您需要 proper file dialogs 时,这适用于, tray icons或者只是想获取 Qt Widgets QApplication
中拥有的 native 系统颜色,但 Qt Quick 的 QGuiApplication
中没有。因此,根据我自己的经验,我反对 Micht 的观点“对 Qt Widget 的不必要依赖”。
因此,对于使用现有 QWidget 的新 Qt Quick 应用程序,我认为这是一个很好的中间步骤。
关于qt - 为什么将 Widget 嵌入到基于 Qml 的应用程序中是不好的想法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31828189/