qt - 为什么将 Widget 嵌入到基于 Qml 的应用程序中是不好的想法

标签 qt qml qwidget qt5.2

我必须在具有最新 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/

相关文章:

qt - 将 QStackedWidget 调整为打开的页面

c++ - QLineEdit 在 Qt 中调用 SetText() 后无法撤消

qt - 通过 QTcpSocket 发送结构

c++ - 在我的 Qt 应用程序中收到 WM_DEVICECHANGE 但没有收到 DBT_DEVICEARRIVAL

c++ - 将 Qt 应用程序与 QML 静态链接

c++ - 为什么QTextDocument背景颜色仅更改一次?

c++ - QPainter 保存状态

c++ - 在 Linux 上的 QT 应用程序中识别控件名称/ID

c++ - Qt 在 Mac OSX 10.9 上的部署

c++ - 如何从保存的几何图形中获取屏幕编号?