Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QdeCrativeListProperty是否可以直接访问,而不是作为模型访问?_Qt_Qt4_Qml_Qt4.8 - Fatal编程技术网

Qt QdeCrativeListProperty是否可以直接访问,而不是作为模型访问?

Qt QdeCrativeListProperty是否可以直接访问,而不是作为模型访问?,qt,qt4,qml,qt4.8,Qt,Qt4,Qml,Qt4.8,我试图使用QDeclarativeListProperty来管理参数列表,主要是为了在ListView中显示它们。但是,我也希望能够直接从QDeclarativeListProperty访问QML中的参数,以便在不同的屏幕上显示/修改各个参数 我的类称为ParameterClass,我为其创建了一个QList: class SystemData : public QObject { Q_OBJECT Q_PROPERTY(QDeclarativeListProperty<P

我试图使用QDeclarativeListProperty来管理参数列表,主要是为了在ListView中显示它们。但是,我也希望能够直接从QDeclarativeListProperty访问QML中的参数,以便在不同的屏幕上显示/修改各个参数

我的类称为ParameterClass,我为其创建了一个QList:

class SystemData : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QDeclarativeListProperty<ParameterClass> parameters READ parameters CONSTANT)
    QDeclarativeListProperty<ParameterClass> parameters();

...

    QList<ParameterClass *> m_parameterList;
}
我只是没有让它发挥作用:我一直在回来[未定义]。我是在浪费时间,还是错过了什么

编辑: 我已经改变了一些东西来使用来自……的建议。下面是我更新代码中的一些选择

main.cpp:

#include <QApplication>
#include <QSplashScreen>
#include <QLocale>
#include <QLibraryInfo>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QObject>
#include <QDeclarativeListProperty>

#include "systemdata.h"
#include "parameterclass.h"

static const QString contentPath = "qrc:/qml/qml/pk_ui/";
static const QString filename(contentPath + "main.qml");

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QDeclarativeView mainView;
    SystemData* systemData = SystemData::getInstance();

    QThread thread;
    UpdateWorker updateWorker;
    QObject::connect((const QObject*)systemData, SIGNAL(startWork()),
                     (const QObject*)&updateWorker, SLOT(doWork()));
    updateWorker.moveToThread(&thread);
    thread.start();
    systemData->startUpdates();

    QFont defaultFont;
    defaultFont.setFamily("Sans Serif");
    QApplication::setFont(defaultFont);

    // Register types to be available in QML
    qmlRegisterType<ParameterClass>();
    qmlRegisterUncreatableType<SystemEnum>("SystemEnums", 1, 0, "SystemEnum", QString());

    mainView.engine()->rootContext()->setContextProperty("SystemData", systemData);

    // Set view optimizations not already done for QDeclarativeView
    mainView.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    mainView.setAttribute(Qt::WA_OpaquePaintEvent);
    mainView.setAttribute(Qt::WA_NoSystemBackground);

    mainView.setSource(QUrl(filename));
    mainView.show();

    return app.exec();
}
Rectangle {
    id: frame
    property int val: SystemData.parameters[4].currentValue
    ...
}
#include <QApplication>
#include <QtDeclarative>

class MyPropertyObject : public QObject {
    Q_OBJECT
    Q_PROPERTY(int value READ value CONSTANT)

public:
    MyPropertyObject(int value = -1) : m_value(value) { }

    int value() const {
        return m_value;
    }

private:
    int m_value;
};

class MyObject : public QObject {
    Q_OBJECT
    Q_PROPERTY(QDeclarativeListProperty<MyPropertyObject> props READ props CONSTANT)

public:

    MyObject() {
        m_props.append(new MyPropertyObject(55));
        m_props.append(new MyPropertyObject(44));
        m_props.append(new MyPropertyObject(33));
    }

    QDeclarativeListProperty<MyPropertyObject> props() {
        return QDeclarativeListProperty<MyPropertyObject>(this, m_props);
    }

private:
    QList<MyPropertyObject *> m_props;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QDeclarativeView view;
    view.engine()->rootContext()->setContextProperty(QLatin1String("tester"), new MyObject);
    qmlRegisterType<MyPropertyObject>();
    view.setSource(QUrl("qrc:///qml/main.qml"));
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.resize(300, 300);
    view.show();

    return a.exec();
}

#include "main.moc"
import QtQuick 1.1

Rectangle {
    property variant foo: tester.props[2].value

    Text {
        anchors.centerIn: parent
        text: parent.foo
    }
}
和我的QML文件:

#include <QApplication>
#include <QSplashScreen>
#include <QLocale>
#include <QLibraryInfo>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QObject>
#include <QDeclarativeListProperty>

#include "systemdata.h"
#include "parameterclass.h"

static const QString contentPath = "qrc:/qml/qml/pk_ui/";
static const QString filename(contentPath + "main.qml");

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QDeclarativeView mainView;
    SystemData* systemData = SystemData::getInstance();

    QThread thread;
    UpdateWorker updateWorker;
    QObject::connect((const QObject*)systemData, SIGNAL(startWork()),
                     (const QObject*)&updateWorker, SLOT(doWork()));
    updateWorker.moveToThread(&thread);
    thread.start();
    systemData->startUpdates();

    QFont defaultFont;
    defaultFont.setFamily("Sans Serif");
    QApplication::setFont(defaultFont);

    // Register types to be available in QML
    qmlRegisterType<ParameterClass>();
    qmlRegisterUncreatableType<SystemEnum>("SystemEnums", 1, 0, "SystemEnum", QString());

    mainView.engine()->rootContext()->setContextProperty("SystemData", systemData);

    // Set view optimizations not already done for QDeclarativeView
    mainView.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    mainView.setAttribute(Qt::WA_OpaquePaintEvent);
    mainView.setAttribute(Qt::WA_NoSystemBackground);

    mainView.setSource(QUrl(filename));
    mainView.show();

    return app.exec();
}
Rectangle {
    id: frame
    property int val: SystemData.parameters[4].currentValue
    ...
}
#include <QApplication>
#include <QtDeclarative>

class MyPropertyObject : public QObject {
    Q_OBJECT
    Q_PROPERTY(int value READ value CONSTANT)

public:
    MyPropertyObject(int value = -1) : m_value(value) { }

    int value() const {
        return m_value;
    }

private:
    int m_value;
};

class MyObject : public QObject {
    Q_OBJECT
    Q_PROPERTY(QDeclarativeListProperty<MyPropertyObject> props READ props CONSTANT)

public:

    MyObject() {
        m_props.append(new MyPropertyObject(55));
        m_props.append(new MyPropertyObject(44));
        m_props.append(new MyPropertyObject(33));
    }

    QDeclarativeListProperty<MyPropertyObject> props() {
        return QDeclarativeListProperty<MyPropertyObject>(this, m_props);
    }

private:
    QList<MyPropertyObject *> m_props;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QDeclarativeView view;
    view.engine()->rootContext()->setContextProperty(QLatin1String("tester"), new MyObject);
    qmlRegisterType<MyPropertyObject>();
    view.setSource(QUrl("qrc:///qml/main.qml"));
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.resize(300, 300);
    view.show();

    return a.exec();
}

#include "main.moc"
import QtQuick 1.1

Rectangle {
    property variant foo: tester.props[2].value

    Text {
        anchors.centerIn: parent
        text: parent.foo
    }
}

在这种情况下,我似乎仍然得到一个未定义的值。我正在尝试调试,以便提供更多信息。

这是非常可能的。关键是要确保在QDeclarativeView上设置源之前注册QML类型并设置context属性

这里有一个有效的例子-

main.cpp:

#include <QApplication>
#include <QSplashScreen>
#include <QLocale>
#include <QLibraryInfo>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QObject>
#include <QDeclarativeListProperty>

#include "systemdata.h"
#include "parameterclass.h"

static const QString contentPath = "qrc:/qml/qml/pk_ui/";
static const QString filename(contentPath + "main.qml");

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QDeclarativeView mainView;
    SystemData* systemData = SystemData::getInstance();

    QThread thread;
    UpdateWorker updateWorker;
    QObject::connect((const QObject*)systemData, SIGNAL(startWork()),
                     (const QObject*)&updateWorker, SLOT(doWork()));
    updateWorker.moveToThread(&thread);
    thread.start();
    systemData->startUpdates();

    QFont defaultFont;
    defaultFont.setFamily("Sans Serif");
    QApplication::setFont(defaultFont);

    // Register types to be available in QML
    qmlRegisterType<ParameterClass>();
    qmlRegisterUncreatableType<SystemEnum>("SystemEnums", 1, 0, "SystemEnum", QString());

    mainView.engine()->rootContext()->setContextProperty("SystemData", systemData);

    // Set view optimizations not already done for QDeclarativeView
    mainView.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    mainView.setAttribute(Qt::WA_OpaquePaintEvent);
    mainView.setAttribute(Qt::WA_NoSystemBackground);

    mainView.setSource(QUrl(filename));
    mainView.show();

    return app.exec();
}
Rectangle {
    id: frame
    property int val: SystemData.parameters[4].currentValue
    ...
}
#include <QApplication>
#include <QtDeclarative>

class MyPropertyObject : public QObject {
    Q_OBJECT
    Q_PROPERTY(int value READ value CONSTANT)

public:
    MyPropertyObject(int value = -1) : m_value(value) { }

    int value() const {
        return m_value;
    }

private:
    int m_value;
};

class MyObject : public QObject {
    Q_OBJECT
    Q_PROPERTY(QDeclarativeListProperty<MyPropertyObject> props READ props CONSTANT)

public:

    MyObject() {
        m_props.append(new MyPropertyObject(55));
        m_props.append(new MyPropertyObject(44));
        m_props.append(new MyPropertyObject(33));
    }

    QDeclarativeListProperty<MyPropertyObject> props() {
        return QDeclarativeListProperty<MyPropertyObject>(this, m_props);
    }

private:
    QList<MyPropertyObject *> m_props;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QDeclarativeView view;
    view.engine()->rootContext()->setContextProperty(QLatin1String("tester"), new MyObject);
    qmlRegisterType<MyPropertyObject>();
    view.setSource(QUrl("qrc:///qml/main.qml"));
    view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    view.resize(300, 300);
    view.show();

    return a.exec();
}

#include "main.moc"
import QtQuick 1.1

Rectangle {
    property variant foo: tester.props[2].value

    Text {
        anchors.centerIn: parent
        text: parent.foo
    }
}

注意:请阅读QDeclarativeListProperty构造函数的文档。我在本例中使用的不是首选的,但适用于快速原型。

我已经移动了很多东西,以便将我的所有代码从QDeclarativeView的包装类移到main.cpp中。通过这种方式,我可以保证在为视图设置源代码之前注册了所有类型,并且我仍然得到了我的对象是未定义的。我将更新上面的代码,以了解我现在的情况。与其使用参数更改信号,不如将该属性更改为与我相同的常量。我在编辑后不久进行了更改。现在我的代码编译和运行时没有报告问题,但我得到的不是未定义的值,就是0,而不是我期望的值。调试显示正在调用我的get_CV方法,但我不确定QML实际得到了什么值。记录在案,调试QML是很乏味的。好的,我现在已经开始工作了。我不完全确定在这一点上我还做了什么更改,但我认为这取决于我在实际的QML代码中如何处理一些事情,这些事情破坏了数据发生的情况。