qt 2或3d bool列表从qml访问

时间:2018-04-09 11:40:30

标签: c++ qt qml

我是QT中的一名初学者,我试图将2d bool QList作为Q_PROPERTY传递给QML,但不幸的是它失败了。当我尝试简单(1d)QList和qml rootItem.chkbool [0]是正确的(它工作),但2d QList失败:

的main.cpp

 Object myObj;
        QQmlContext* ctx = engine->rootContext();
        ctx->setContextProperty("rootItem", (QObject *)&myObj);

QML:

import QtQuick 2.9
import QtQuick.Controls 2.3
import QtQuick.Window 2.2
import QtWebEngine 1.5
import QtQml.Models 2.2

Window {
    flags:  Qt.ToolTip | Qt.FramelessWindowHint | Qt.WA_TranslucentBackground
    color: "#00000000"
    visible: true
    width: 1920
    height: 1080
    title: qsTr("Hello World")

    CheckBox {
            id:chk
            checked: rootItem.chkbool[0][0]
        }

}

object_.h

#ifndef RECEIVER_H
#define RECEIVER_H
#include <QObject>
#include <QtGui>
#include <QList>

class Object : public QObject
{
    Q_OBJECT
    Q_PROPERTY( QList<QList<bool>> chkbool READ getBoolChange NOTIFY changeOfStatus )

 public:
  Object() {
      enbeddedList.push_back(true);
      enbeddedList.push_back(false);
      list.append(enbeddedList);

      emit changeOfStatus();
  }

  QList<QList<bool>> getBoolChange(){
      return list;
  }

  signals:
   void changeOfStatus();

  private:
   QList<bool> enbeddedList;
   QList<QList<bool>> list;
   bool changeMe;
   int theValue;
   QTimer *myTimer;

};
#endif // RECEIVER_H

任何人都知道正确的代码吗?

1 个答案:

答案 0 :(得分:0)

数据类型转换是正确的(来自folibis)。

这是正确的代码:

object_.h

#ifndef RECEIVER_H
#define RECEIVER_H
#include <QObject>
#include <QtGui>
#include <QList>
#include <QMetaType>

class Object : public QObject
{
    Q_OBJECT
    Q_PROPERTY( QVariantList chkbool READ getBoolChange NOTIFY changeOfStatus )

 public:
  Object() {

      QVariantList variant;
      variant.append(true);
      variant.append(false);
      list.insert(list.size() ,variant);

      emit changeOfStatus();
  }

  QVariantList getBoolChange(){
      return list;
  }

  signals:
   void changeOfStatus();

  private:
   QVariantList list;
   bool changeMe;
   int theValue;
   QTimer *myTimer;

};
#endif // RECEIVER_H

感谢所有人!