为什么QML“连接”看不到C ++的信号

时间:2019-11-08 13:07:43

标签: c++ qt qml signals

我有一个由setContextProperty连接的Worker类,在该类中,我有一个要在qml Connection中处理的名为“ Good”的信号。

IDE QtCreator 4.5.0 基于Qt 5.10.0(GCC 5.3.1 20160406(Red Hat 5.3.1-6),64位)

OS Ubuntu 16.04

但是我有一个错误

 QML Connections: Cannot assign to non-existent property "onGood"

也许我写错了,请帮助我

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "setpoint.h"
#include "setmode.h"
#include "Bsb/worker.h"
#include <QtQuick>
#include <QQmlContext>
int main(int argc, char *argv[])
 {
  #if defined(Q_OS_WIN)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
 #endif

QGuiApplication app(argc, argv);
QString fullpath = QCoreApplication::applicationDirPath() + "/setPerms.sh";
system(fullpath.toLocal8Bit());

QQuickView view;
Worker *worker = new Worker();
view.engine()->rootContext()->setContextProperty("Worker",worker);
qmlRegisterType<SetPoint>("src.bsb.setpoint", 1, 0, "SetPoint");
qmlRegisterType<SetMode>("src.bsb.setmode", 1, 0, "SetMode");

view.engine()->addImportPath("qrc:/qml/imports");
view.setSource(QUrl("qrc:/qml/MainApp.ui.qml"));

if (!view.errors().isEmpty())
   return -1;
view.show();

return app.exec();
}

Worker.h

#include <QObject>
#include <QThread>
#include "port.h"
#include "telegram.h"
#include "../setpoint.h"
#include "asker.h"
#include "answerchecker.h"
class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = nullptr);
    ~Worker();
    Q_INVOKABLE bool isAns();
    AnswerChecker *getChecker();
private:
    Port* port;
    Asker* asker;
    AnswerChecker *checker;
    bool answer = false;
    void check(QByteArray data);
signals:
    void Good();
    void write(QByteArray);
    void send_to_check(Telegram *);
public slots:
    void writeData(QByteArray);
    void startAsk(int _pageId,QString mode);
    void setAns(bool);
    };

Worker.cpp

 Worker::Worker(QObject *parent) : QObject(parent), port(new 
 Port()),checker(new AnswerChecker())
 {
    QThread* thread = new QThread;
    port->moveToThread(thread);
    connect(thread,SIGNAL(started()),port,SLOT(processPort()));
    connect(port,SIGNAL(finishedPort()),thread,SLOT(quit()));   
    connect(thread,SIGNAL(finished()),port,SLOT(deleteLater()));

    connect(this,SIGNAL(write(QByteArray)),
    port,SLOT(writeToPort(QByteArray)));

    connect(port,SIGNAL(outPort(Telegram*)),
    checker,SLOT(checkReturned(Telegram*)));

    connect(this,SIGNAL(send_to_check(Telegram*)),checker,
    SLOT(sent(Telegram*)));

   connect(checker,SIGNAL(goodAns(bool)),this,
   SLOT(setAns(bool)));

   port->connectPort();
   thread->start();
  }


  Worker::~Worker()
  {
    port->disconnectPort();
  }

 bool Worker::isAns()
{
    return answer;
}

 AnswerChecker *Worker::getChecker()
 {
       return checker;
 }

void Worker::check(QByteArray data)
{
answer = false;
for(auto &it : data)
{
    it ^= 0xff;
}
Telegram *packet = new Telegram(data);
emit send_to_check(packet);
}

void Worker::writeData(QByteArray data)
{
   emit write(data);
   check(data);
}


 void Worker::startAsk(int _pageId, QString mode)
 {

 }

void Worker::setAns(bool answer)
{
   this->answer = answer;
   emit Good();
}

Qml文件

import QtQuick 2.8
import Controller 1.0
import src.bsb.setpoint 1.0
Item {
id: element
width: 200
height: 385

Timer{
    id:timer
    triggeredOnStart: false
    interval: 2000
    repeat: false
    running: false
    onTriggered: {
        console.log("triggered")
        Worker.writeData(point.setTemperature())
    }
}

Connections{
    target: Worker
    onGood:{
        console.log("yeeeaaa")
    }
}

SetPoint{
    id: point
    small: smallSetPoint.text
    big: bigSetPoint.text
    onDataChanged: {
        smallSetPoint.text = small
        bigSetPoint.text = big
    }
}

IncreaseButton {
    id: increaseButton
    x: 40
    anchors.top: parent.top
    anchors.topMargin: 48
    anchors.right: parent.right
    anchors.rightMargin: 65
    onClicked: {
        point.clickIncrease()
        room.primary_color = "#f54e42"
        timer.stop()
        timer.start()
    }
}

ReduceButton {
    id: reduceButton
    x: 14
    y: 214
    anchors.bottom: parent.bottom
    anchors.bottomMargin: 58
    anchors.right: parent.right
    anchors.rightMargin: 65
    onClicked: {
        point.clickReduce()
        room.primary_color = "#f54e42"
        timer.stop()
        timer.start()
    }
}
}

0 个答案:

没有答案