Passing QBitArrya to QML
-
Hi,
In my class I have code from this property:
Q_PROPERTY(QBitArray statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged)In QML code I would like read this bits, I try in this way :
Image { id: test anchors { left: parent.left leftMargin: 30 top: keyInsert.bottom topMargin: 20 } width: 70 height: 70 source: { if (systemController.statusPrinter(0) === 0) return "qrc:/UI/Assets/ok.png" if (systemController.statusPrinter(0) === 1) return "qrc:/UI/Assets/warrning.png" } }but it doesn't work. What can be wrong ?
-
I do not know how use it.... so in *.h I use in this way:
Q_PROPERTY(QBitArray statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged) public: Q_INVOKABLE bool isBitSet(int bit);in definition in *.cpp
const QBitArray &SystemController::statusPrinter() const { return m_statusPrinter; } void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter) { if (m_statusPrinter == newStatusPrinter) return; m_statusPrinter = newStatusPrinter; emit statusPrinterChanged(); } bool SystemController::isBitSet(int bit) { return statusPrinter[bit]; } void SystemController::statusFromPrinter(QBitArray status) { setStatusPrinter(status); }In this way I get error in
return statusPrinter[bit];line ::
systemcontroller.cpp:46:12: Reference to non-static member function must be called; did you mean to call it with no arguments? (fix available) insert '()'@Damian7546 said in Passing QBitArrya to QML:
return statusPrinter[bit];Where do you get a C++ array named
statusPrinterfrom?[Maybe I'm wrong, I don't do any QML :) ]. The error message actually tells you what is wrong; this is what you have:const QBitArray &SystemController::statusPrinter() const -
Probably I cannot pass an array directly to QML, so I need try in other way, like this:
class SystemController : public QObject { Q_PROPERTY(QList<StatusPrinter> statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged) . . . struct StatusPrinter { bool tempalert; bool printoutlost; bool paperjam; bool paperlow; bool outofpaper; bool poweron; }; };And in QML:
var a = systemController.statusPrinter[0].tempalertBut still doesn't work, I have error:
TypeError: Cannot read property '0' of undefined -
I do not know how use it.... so in *.h I use in this way:
Q_PROPERTY(QBitArray statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged) public: Q_INVOKABLE bool isBitSet(int bit);in definition in *.cpp
const QBitArray &SystemController::statusPrinter() const { return m_statusPrinter; } void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter) { if (m_statusPrinter == newStatusPrinter) return; m_statusPrinter = newStatusPrinter; emit statusPrinterChanged(); } bool SystemController::isBitSet(int bit) { return statusPrinter[bit]; } void SystemController::statusFromPrinter(QBitArray status) { setStatusPrinter(status); }In this way I get error in
return statusPrinter[bit];line ::
systemcontroller.cpp:46:12: Reference to non-static member function must be called; did you mean to call it with no arguments? (fix available) insert '()' -
I do not know how use it.... so in *.h I use in this way:
Q_PROPERTY(QBitArray statusPrinter READ statusPrinter WRITE setStatusPrinter NOTIFY statusPrinterChanged) public: Q_INVOKABLE bool isBitSet(int bit);in definition in *.cpp
const QBitArray &SystemController::statusPrinter() const { return m_statusPrinter; } void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter) { if (m_statusPrinter == newStatusPrinter) return; m_statusPrinter = newStatusPrinter; emit statusPrinterChanged(); } bool SystemController::isBitSet(int bit) { return statusPrinter[bit]; } void SystemController::statusFromPrinter(QBitArray status) { setStatusPrinter(status); }In this way I get error in
return statusPrinter[bit];line ::
systemcontroller.cpp:46:12: Reference to non-static member function must be called; did you mean to call it with no arguments? (fix available) insert '()'@Damian7546 said in Passing QBitArrya to QML:
return statusPrinter[bit];Where do you get a C++ array named
statusPrinterfrom?[Maybe I'm wrong, I don't do any QML :) ]. The error message actually tells you what is wrong; this is what you have:const QBitArray &SystemController::statusPrinter() const -
Can you explain me ? And how to pass this bits to QML?
-
Can you explain me ? And how to pass this bits to QML?
@Damian7546
You have a "getter" method/functionQBitArray &SystemController::statusPrinter(). You wrotestatusPrinter[bit]. But you have to call the function, sostatusPrinter()is necessary. The error message suggested(fix available) insert '()'To do it in one statement you will want
return statusPrinter()[bit]; // or return statusPrinter().at(bit); -
Thank you very much.
Now I know how Q_INVOKABLE working.The Q_INVOKABLE macro allows calling the function directly from QML, but I need
auto update bits value in QML when they changeIn the public slots
statusFromPrinter(QBitArray status)I pass 3 bits :const QBitArray &SystemController::statusPrinter() const { return m_statusPrinter; } void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter) { if (m_statusPrinter == newStatusPrinter) return; m_statusPrinter = newStatusPrinter; emit statusPrinterChanged(); } bool SystemController::isBitSet(int bit) { return statusPrinter()[bit]; } void SystemController::statusFromPrinter(QBitArray status) { qDebug()<< "Bits: " << status ; setStatusPrinter(status); }How update code in QML when bits on change
source: { if (systemController.isBitSet(1) === true) return "qrc:/UI/Assets/ok.png" if (systemController.isBitSet(1) === false) return "qrc:/UI/Assets/warrning.png" }@JonB Can you help me ?
-
Thank you very much.
Now I know how Q_INVOKABLE working.The Q_INVOKABLE macro allows calling the function directly from QML, but I need
auto update bits value in QML when they changeIn the public slots
statusFromPrinter(QBitArray status)I pass 3 bits :const QBitArray &SystemController::statusPrinter() const { return m_statusPrinter; } void SystemController::setStatusPrinter(const QBitArray &newStatusPrinter) { if (m_statusPrinter == newStatusPrinter) return; m_statusPrinter = newStatusPrinter; emit statusPrinterChanged(); } bool SystemController::isBitSet(int bit) { return statusPrinter()[bit]; } void SystemController::statusFromPrinter(QBitArray status) { qDebug()<< "Bits: " << status ; setStatusPrinter(status); }How update code in QML when bits on change
source: { if (systemController.isBitSet(1) === true) return "qrc:/UI/Assets/ok.png" if (systemController.isBitSet(1) === false) return "qrc:/UI/Assets/warrning.png" }@JonB Can you help me ?
@Damian7546
No, because I said I know nothing about QML. "How update code in QML when bits on change": if you mean the bits change externally and you want something to update in QML when they do, I would guess you need to emit a signal when the bits get changed so that QML knows? -
@JonB
CallingsetStatusPrinter(status)I emited signalstatusPrinterChanged(), so in QML I should readstatusPrinterbut it doesn't work- I can't read array in QML in this waysystemController.statusPrinter()........ -
So, instead of using only one
Q_PROPERTY(QBitArray status READ status WRITE setStatus NOTIFY statusChanged)a need use severalQ_PROPERTY(bool statusX READ statusX WRITE setStatusX NOTIFY statusXChanged)to send my status to QML.
It not look optimally