当绑定变量在C ++状态上更改时,QML自定义项状态不会更改

时间:2015-09-29 15:34:06

标签: c++ qt qml

我有以下两种状态的自定义QML项目:

import QtQuick 2.0

import si.testfirm 1.0

Item
{
    id: ueDatabaseStatusIndicator

    property string ueParamImageStatusOn
    property string ueParamImageStatusOff

    state: ueApplicationStatus.m_ueBluetoothPrinterConnectionStatus===UeTypeBluetootPrinterConnectionStatus.NOT_PAIRED?
           "ueStatusIndicatorBluetoothNotConnected":
           "ueStatusIndicatorBluetoothConnected"

    Image
    {
        id: ueStatusIndicatorCurrentImage

        smooth: true

        fillMode: Image.PreserveAspectFit

        width: 96
        height: 96

        sourceSize.width: 96
        sourceSize.height: 96
    }   // Image

    states:
    [
        State
        {
            name: "ueStatusIndicatorBluetoothConnected"

            PropertyChanges
            {
                target: ueStatusIndicatorCurrentImage
                source: ueParamImageStatusOn
            }   // PropertyChanges
        },  // State

        State
        {
            name: "ueStatusIndicatorBluetoothNotConnected"

            PropertyChanges
            {
                target: ueStatusIndicatorCurrentImage
                source: ueParamImageStatusOff
            }   // PropertyChanges
        }   // State
    ]   // states
}   // Item

在名为ueStatusIndicatorBluetoothNotConnected的第一个状态中,它显示红色图标,类似于蓝牙打印机未配对。在第二个状态,名为ueStatusIndicatorBluetoothConnected,它显示蓝色图标,类似于蓝牙打印机与app配对。现在,当我使用以下 main.cpp 文件运行此应用程序时:

#include <QtQml>
#include <QApplication>
#include <QQmlApplicationEngine>

#include "database/uepeoplemodel.h"
#include "core/ueapplicationstatus.h"
#include "core/uedatabaseconnectionstatus.h"
#include "core/uebluetoothmanager.h"
#include "core/uebluetoothprinterconnectionstatus.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QQmlApplicationEngine engine;

    UeApplicationStatus* ueApplicationStatus=new UeApplicationStatus(qApp);
    UePeopleModel* uePeopleModel=new UePeopleModel(qApp);
    UeBluetoothManager* ueBtManager=new UeBluetoothManager(qApp);

    QObject::connect(uePeopleModel,
                     SIGNAL(ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)),
                     ueApplicationStatus,
                     SLOT(ueSlotDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)));

    QObject::connect(ueBtManager,
                     SIGNAL(ueSignalBtPrinterConnectionChanged(UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus)),
                     ueApplicationStatus,
                     SLOT(ueSlotBtPrinterConnectionChanged(UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus)));

    engine.rootContext()->setContextProperty("uePeopleModel",
                                             uePeopleModel);
    engine.rootContext()->setContextProperty("ueApplicationStatus",
                                             ueApplicationStatus);
    engine.rootContext()->setContextProperty("ueBtManager",
                                             ueBtManager);

    engine.addImageProvider(QLatin1String("uePeopleModel"),
                            uePeopleModel);

    qmlRegisterUncreatableType<UeDatabaseConnectionStatus>("si.testfirm",
                                                           1,
                                                           0,
                                                           "UeTypeDatabaseConnectionStatus",
                                                           "Database Connection Status");

    qmlRegisterUncreatableType<UeBluetoothPrinterConnectionStatus>("si.testfirm",
                                                                   1,
                                                                   0,
                                                                   "UeTypeBluetootPrinterConnectionStatus",
                                                                   "Bluetooth Printer Connection Status");

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    uePeopleModel->ueConnectToDatabase();
    ueBtManager->ueStartPairing();

    ueApplicationStatus->ueUpdate(uePeopleModel->ueFetchUsers());

    return app.exec();
}
显示应用红色图标中的

,这是正常的,因为打印机未配对,但是,使用行ueBtManager->ueStartPairing();我开始搜索蓝牙打印机并且打印机找到并配对,因为您可以从屏幕截图中看到调用插槽void UeBluetoothManager::ueSlotPairingFinished(const QBluetoothAddress& address,QBluetoothLocalDevice::Pairing pairing)Slot gets called 在最后一行中,正如您从屏幕截图中看到的那样,我发出信号UeBluetoothManager::void ueSignalBtPrinterConnectionChanged(const UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus& newStatus);ueApplicationStatus对象在 main.cpp 中调用,以更新其状态。这是UeApplicationStatus标题:

#ifndef UEAPPLICATIONSTATUS_H
#define UEAPPLICATIONSTATUS_H

#include <QObject>
#include <QList>
#include <QDebug>

#include "../core/uetypes.h"
#include "../core/uedatabaseconnectionstatus.h"
#include "../core/uebluetoothprinterconnectionstatus.h"

class UeApplicationStatus : public QObject
{
    Q_OBJECT

    Q_PROPERTY(UeTypeUsers* m_ueUsers
               READ ueUsers
               WRITE ueSetUsers
               NOTIFY ueSignalUsersChanged)
    Q_PROPERTY(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus m_ueDatabaseConnectionStatus
               READ ueDbConnectionStatus
               WRITE ueSetDbConnectionStatus
               NOTIFY ueSignalDatabaseConnectionChanged)
    Q_PROPERTY(UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus m_ueBluetoothPrinterConnectionStatus
               READ ueBtPrinterConnectionStatus
               WRITE ueSetBtPrinterConnectionStatus
               NOTIFY ueSignalBtPrinterConnectionChanged)

private:
    UeTypeUsers* m_ueUsers;
    UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus m_ueDatabaseConnectionStatus;
    UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus m_ueBluetoothPrinterConnectionStatus;

public:
    explicit UeApplicationStatus(QObject *parent = 0);
    ~UeApplicationStatus();

    inline UeTypeUsers* ueUsers() const
        { return this->m_ueUsers; }
    inline UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus ueDbConnectionStatus() const
        { return this->m_ueDatabaseConnectionStatus; }
    inline UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus ueBtPrinterConnectionStatus() const
        { return this->m_ueBluetoothPrinterConnectionStatus; }

    inline void ueSetUsers(UeTypeUsers* const users)
        { this->m_ueUsers=users; }
    inline void ueSetDbConnectionStatus(const UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus& status)
        { this->m_ueDatabaseConnectionStatus=status; }
    inline void ueSetBtPrinterConnectionStatus(const UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus status)
        { this->m_ueBluetoothPrinterConnectionStatus=status; }

signals:
    void ueSignalUsersChanged();
    void ueSignalDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus& newStatus);
    void ueSignalBtPrinterConnectionChanged(const UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus& newStatus);

public slots:
    void ueSlotDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus& newStatus);
    void ueSlotBtPrinterConnectionChanged(const UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus& newStatus);
    void ueUpdate(UeTypeUsers* const users);
};

#endif // UEAPPLICATIONSTATUS_H

及其实施:

#include "ueapplicationstatus.h"

UeApplicationStatus::UeApplicationStatus(QObject *parent)
    : QObject(parent)
{
    this->ueSetUsers(new UeTypeUsers());
    this->ueSetDbConnectionStatus(UeDatabaseConnectionStatus::NOT_CONNECTED);
    this->ueSetBtPrinterConnectionStatus(UeBluetoothPrinterConnectionStatus::NOT_PAIRED);

    connect(this,
            SIGNAL(ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)),
            this,
            SLOT(ueSlotDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)));

    connect(this,
            SIGNAL(ueSignalBtPrinterConnectionChanged(UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus)),
            this,
            SLOT(ueSlotBtPrinterConnectionChanged(UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus)));
}   // constructor

UeApplicationStatus::~UeApplicationStatus()
{
    delete this->ueUsers();
}   // destructor

void UeApplicationStatus::ueSlotDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus& newStatus)
{
    this->ueSetDbConnectionStatus(newStatus);
}   // ueSignalDatabaseConnectionChanged

void UeApplicationStatus::ueSlotBtPrinterConnectionChanged(const UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus& newStatus)
{
    this->ueSetBtPrinterConnectionStatus(newStatus);
}   // ueSlotBtPrinterConnectionChanged

void UeApplicationStatus::ueUpdate(UeTypeUsers* const users)
{
    this->ueSetUsers(users);
}   // ueUpdate

问题是,为什么在自定义项ueDatabaseStatusIndicator内的QML端没有切换图像,哪个代码首先列出?

1 个答案:

答案 0 :(得分:3)

这可能是因为您初始化应用程序的方式。您在发出信号后启动事件循环。试试这个。

engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

QTimer::singleshot(150, this, [&]() {
  uePeopleModel->ueConnectToDatabase();
  ueBtManager->ueStartPairing();
  ueApplicationStatus->ueUpdate(uePeopleModel->ueFetchUsers());
});

return app.exec();

这只是我的猜测。祝你好运。

<强>更新

inline void ueSetBtPrinterConnectionStatus(const UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus status)
{ 
    m_ueBluetoothPrinterConnectionStatus=status; 
    emit ueSignalBtPrinterConnectionChanged(m_ueBluetoothPrinterConnectionStatus);
}

您必须删除connect调用,因为这会产生无限循环。