Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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
如何从C++;? < >我想用C++从ListView创建标签。添加标签的一部分是:D,我想有一个模型,每个ListVIEW,并能够控制模型从C++侧。 到目前为止,我已经做到了:_C++_Qt_Listview_Model_Qml - Fatal编程技术网

如何从C++;? < >我想用C++从ListView创建标签。添加标签的一部分是:D,我想有一个模型,每个ListVIEW,并能够控制模型从C++侧。 到目前为止,我已经做到了:

如何从C++;? < >我想用C++从ListView创建标签。添加标签的一部分是:D,我想有一个模型,每个ListVIEW,并能够控制模型从C++侧。 到目前为止,我已经做到了:,c++,qt,listview,model,qml,C++,Qt,Listview,Model,Qml,C++部分: SmsModel *model = new SmsModel(); model->createDummyData(phoneNumber); QObject* tab = findTab(phoneNumber); if (tab == nullptr) { QObject* pRoot = mAppEngine->rootObjects()[0]; QObject* m_pTabView= pRoot->findChildren<QObject*&

C++部分:

SmsModel *model = new SmsModel();
model->createDummyData(phoneNumber);
QObject* tab = findTab(phoneNumber);
if (tab == nullptr)
{
  QObject* pRoot = mAppEngine->rootObjects()[0];
  QObject* m_pTabView= pRoot->findChildren<QObject*>("conversationTabView").first();
  if (m_pTabView)
  {
  QVariant returnedValue;
  QVariant title = phoneNumber;
  QQmlContext *context = new QQmlContext(mAppEngine, mAppEngine);
  context->setContextProperty(QString("myModel"), model);

  QQmlComponent *component = new QQmlComponent(mAppEngine, QUrl("qrc:/ChatView.qml"), this);
  QObject *object = component->create(context);
  QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
  QObject *p = object->findChild<QObject*>("chatView");
  p->setProperty("model", context->contextProperty("myModel"));
  qDebug() << p->property("model");
  object->setProperty("active", QVariant(true));
  component->setParent(m_pTabView);

  QMetaObject::invokeMethod(m_pTabView, "addTab",
  Q_RETURN_ARG(QVariant, returnedValue),
  Q_ARG(QVariant, title),
  Q_ARG(QVariant, QVariant::fromValue(component)));

  object->setProperty("anchors.fill", "parent");
  }
当我运行我的应用程序时,GUI中没有数据,我有以下错误
参考错误:未定义myModel


谢谢每一个响应。

< P>为了使用C++对象作为列表模型,必须有一个继承自<代码> QuastistListMys>代码>的类。看看这个例子。

< P>为了使用C++对象作为列表模型,必须有一个继承自<代码> QuastistListMease/Cuff>的类。看看这个例子。

是的,我知道。SmsModel继承自QAbstractListModel。我认为问题是在上下文中存在的。当我在rootContext中设置ContextProperty(“myModel”…)时,我没有任何错误,但是当我创建两个选项卡时,两个列表视图共享同一个模型。是的,我知道。SmsModel继承自QAbstractListModel。我认为问题是在上下文中存在的。当我在rootContext中设置ContextProperty(“myModel”…)时,我没有任何错误,但是当我创建两个选项卡时,两个ListView共享相同的模型。
import QtQuick 2.5
import QtQml.Models 2.2
import QtQuick.Window 2.2
import org.example 1.0

Rectangle {
    color: "#E0E0E0"
    ListView {
        objectName: "chatView"
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        clip: true
        model: myModel
        delegate: bubbleDelegate
        Component {
            id: bubbleDelegate
            Rectangle {
                implicitWidth: messageText.implicitWidth + 2*messageText.anchors.margins
                implicitHeight: messageText.implicitHeight + 2*messageText.anchors.margins
                anchors.left: model.received ? parent.left : undefined
                anchors.right: model.received ? undefined : parent.right
                anchors.margins: 5
                id: bubble
                smooth: true
                radius: 10
                color: model.received ? "#673AB7" : "#E040FB"
                Text {
                    id: messageText
                    anchors.fill: parent
                    anchors.margins: 5
                    text: model.message
                    wrapMode: Text.WordWrap;
                    horizontalAlignment: model.received ? Text.AlignLeft : Text.AlignRight
                    color: "white"
                }
            }
        }
    }
}