如何在 QML 中集成 QWidget(Qt Quick 2.0) [英] How to integrate QWidget in QML (Qt Quick 2.0)

查看:26
本文介绍了如何在 QML 中集成 QWidget(Qt Quick 2.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经关闭了返回 QFrame 的库.我的程序的 GUI 是用 QML (Qt Quick 2.0) 开发的.我需要将 QFrame (QWidget) 集成到 QML 的解决方案

I have closed library that return QFrame. GUI of my program is developed with QML (Qt Quick 2.0). I need solution to integrate QFrame (QWidget) to QML

注意:我找到了示例:Qt_DIR/Examples/Qt-5.3/declarative/cppextensions/qwidgets,可以根据需要做一些事情.在这个例子中,QWidged 被添加到 QGraphicsProxyWidget.我这样编写代码,但是当我运行我的应用程序时,它会在控制台中显示:无法将 QtQuick 1.0 项目(MyPushButton)添加到 QtQuick 2.0 场景中!".这是这段代码:

Note: I found example: Qt_DIR/Examples/Qt-5.3/declarative/cppextensions/qwidgets, that do something as I need. In ths example QWidged is addeed to QGraphicsProxyWidget. I write my code like this, but when I run my aplication it show me in console: "Cannot add a QtQuick 1.0 item (MyPushButton) into a QtQuick 2.0 scene!". This is this code:

class MyPushButton : public QGraphicsProxyWidget
{
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)

public:
    MyPushButton(QGraphicsItem* parent = 0)
        : QGraphicsProxyWidget(parent)
    {
        widget = new QPushButton("MyPushButton");
        widget->setAttribute(Qt::WA_NoSystemBackground);
        setWidget(widget);

        QObject::connect(widget, SIGNAL(clicked(bool)), this, SIGNAL(clicked(bool)));
    }

    QString text() const
    {
        return widget->text();
    }

    void setText(const QString& text)
    {
        if (text != widget->text()) {
            widget->setText(text);
            emit textChanged();
        }
    }

Q_SIGNALS:
    void clicked(bool);
    void textChanged();

private:
    QPushButton *widget;
};


private:
    QPushButton *widget;
};

推荐答案

QGraphicsProxyWidget 旨在与 QtQuick 1 一起使用.已经有答案 Qt5.在 QML 中嵌入 QWidget 对象

QGraphicsProxyWidget is intended to use with QtQuick 1. Already there is answer Qt5. Embed QWidget object in QML

另一个想法 - 您可以将 QWidget 嵌入到 QQuickItem 中.或者查看 QtQUickControls 如何使用 QtQuick2 绘制它们

Another thought - you can embed your QWidget inside QQuickItem. Or look into QtQUickControls how they are painted with QtQuick2

这篇关于如何在 QML 中集成 QWidget(Qt Quick 2.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆