无法在QT上单击鼠标时调用C ++函数

时间:2016-02-17 14:56:58

标签: c++ qt qml

我正在使用C ++和QML在QT中编写应用程序来复制车辆上的仪表。我的目标是每次在鼠标区域中单击鼠标时调用c ++函数。我用Google搜索并尝试了很多东西,但是仍然无法在鼠标点击时调用我的C ++函数。由于我是QT的新手,我不知道我做错了什么。请帮忙解决这个问题。

Gauge.pro文件

folder_01.SOURCES = qml/testlib
folder_01.TARGET = qml
DEPLOYMENTFOLDERS = folder_01

TEMPLATE = app

QT += qml quick
CONFIG += c++11
CONFIG += static

SOURCES += main.cpp \
    rotationThread.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

QT += declarative

HEADERS += \
    rotationThread.h

rotationThread.h文件

#ifndef ROTATIONTHREAD_H
#define ROTATIONTHREAD_H

#include <QThread>
#include <QObject>

class rotationThread : public QThread, public QObject
{
private:
    QObject *rootObject, *leftNeedleObj, *rightGaugeObj, *unacHeadObj, *mouseAreaObj;
    Q_OBJECT
protected:
    Q_INVOKABLE void run();
public:
    rotationThread(QObject *pobj);
};

#endif // ROTATIONTHREAD_H

Main.cpp的

#include <QGuiApplication>
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QQmlApplicationEngine>
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#include <QDebug>
#include <QPropertyAnimation>
#include <QDebug>
#include <QVariantAnimation>
#include <windows.h>
#include <QThread>
#include <rotationThread.h>
#include <QGraphicsObject>
#include <QQmlApplicationEngine>
#include <QtQml>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);


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

    //Get access to the root objects
    QObject *rootObject = (QObject *)engine.rootObjects().front();
    //QObject *mouseAreaObj = rootObject->findChild<QObject *>("mouseArea");
    rotationThread rot(rootObject);
    qmlRegisterType<rotationThread(rot)>("testlib", 1, 0, "rotationThread");

    //QEvent *event;
    //QMouseEvent *event;

   // QObject::connect(mouseAreaObj, SIGNAL(clicked), qGuiApp, SLOT(rot.start()));

    /*if(event->type() == QEvent::MouseButtonPress)
    {
    rot.start();
    }*/

    return app.exec();
}

rotationThread.cpp

#include "rotationThread.h"
#include <QVariant>
#include <QtMath>
#include <QCursor>
#include <QMouseEvent>

rotationThread::rotationThread(QObject *pobj)
{
    rootObject = pobj;
}

void rotationThread::run()
{
    leftNeedleObj = rootObject->findChild<QObject *>("leftNeedle");
    rightGaugeObj = rootObject->findChild<QObject *>("rightGauge");
    unacHeadObj = rootObject->findChild<QObject *>("unacHead");
    mouseAreaObj = rootObject->findChild<QObject *>("mouseArea");

    QVariant angle=0, dummyX=0, dummyY=0;
    qreal x=0, y=0, div=0, calcAng=0;
    QPoint pos;

    dummyX = mouseAreaObj->property("x");
    dummyY = mouseAreaObj->property("y");

    x = dummyX.toReal();
    y = dummyY.toReal();

    /*QMouseEvent *clicked;
    if(clicked->button() == Qt::LeftButton)
    {*/

    //pos = QCursor::pos();
    //x = pos.x();
    //y = pos.y();

    div = (y/x);

    //At Origin
    if((x==0) && (y==0) )
    {
        angle = 0;
    }

    //At first quadrant
    if((x>0) && (y>0) )
    {
        calcAng = qAtan(div);
        angle = calcAng;
    }

    //At second quadrant
    if((x<0) && (y>0) )
    {
        calcAng = qAtan(div);
        calcAng = -(90 + calcAng);
        angle = calcAng;
    }

    //At third quadrant
    if((x<0) && (y<0) )
    {
        calcAng = qAtan(div);
        calcAng = -(calcAng - 90);
        angle = calcAng;
    }

    //At fourth quadrant
    if((x>0) && (y<0) )
    {
        calcAng = qAtan(div);
        calcAng = -(calcAng - 90);
        angle = calcAng;
    }

    QThread::msleep(100);
    leftNeedleObj->setProperty("stepRot", angle);
    rightGaugeObj->setProperty("stepRot", angle);
    unacHeadObj->setProperty("stepRot", angle);

    /*//Rotating Clockwise
    while(angle<=360)
    {
        QThread::msleep(100);
        leftNeedleObj->setProperty("stepRot", angle);
        rightGaugeObj->setProperty("stepRot", angle);
        unacHeadObj->setProperty("stepRot", angle);
        angle = angle.toInt() + 5;
    }*/

    QThread::msleep(100);
    angle=0;
    //}

    //Rotating Anticlockwise
    /*angle=0;
    while(angle>=(-360))
    {
        QThread::msleep(100);
        leftNeedleObj->setProperty("stepRot", angle);
        rightGaugeObj->setProperty("stepRot", angle);
        unacHeadObj->setProperty("stepRot", angle);
        angle = angle.toInt() - 5;
    }
    angle=0;*/
}

main.qml

import QtQuick 2.3
import QtQuick.Window 2.2
import testlib 1.0

Window {
    id: mainWindow
    objectName: "mainWindow"
    width: 800
    height: 800
    color: "#000000"
    visible: true

    rotationThread{
        id: thread
        objectName: "thread"
    }

    Rectangle {
        id: gaugeRec
        objectName: "gaugeRec"
        x: 0
        y: 0
        width: 800
        height: 361
        color: "#000000"
        gradient: Gradient {
            GradientStop {
                position: 0
                color: "#000000"
            }

            GradientStop {
                position: 1
                color: "#000000"
            }
        }

        Image {
            id: leftGauge
            objectName: "leftGauge"
            x: 32
            y: 29
            width: 290
            height: 290
            source: "leftGauge.png"

            Image {
                id: leftNeedle
                objectName: "leftNeedle"
                property real stepRot: 0
                rotation: 0
                x: 100
                y: 100
                width: 90
                height: 90
                source: "leftNeedle.png"
                transform:
                    Rotation { origin.x: 45; origin.y: 45; angle: leftNeedle.stepRot}
                smooth: true
            }
        }

        Image {
            id: rightGauge
            objectName: "rightGauge"
            property real stepRot: 0
            x: 471
            y: 31
            width: 290
            height: 290
            source: "rightGauge.png"
            transform:
                Rotation { origin.x: 145; origin.y: 145; angle: rightGauge.stepRot }
            smooth: true
        }

        Image {
            id: rightNeedle
            objectName: "rightNeedle"
            x: 571
            y: 131
            width: 90
            height: 90
            source: "rightNeedle.png"
        }
    }

    Rectangle {
        id: headRec
        objectName: "headRec"
        x: 48
        y: 386
        width: 709
        height: 394
        color: "#000000"
        gradient: Gradient {
            GradientStop {
                position: 0
                color: "#000000"
            }

            GradientStop {
                position: 1
                color: "#000000"
            }
        }

        Image {
            id: railTrack
            objectName: "railTrack"
            x: 279
            y: 100
            width: 154
            height: 294
            source: "railTrack.png"

            Image {
                id: unacHead
                objectName: "unacHead"
                property real stepRot: 0
                x: 63
                y: -165
                width: 26
                height: 328
                visible: true
                source: "unacHead.png"
                transform:
                    Rotation { origin.x: 13; origin.y: 164; angle: unacHead.stepRot }
                smooth: true
            }
        }
    }

    MouseArea {
        id: mouseArea
        objectName: "mouseArea"
        x : 0
        y : 0
        width: 800
        height: 800
        hoverEnabled: false
        visible: true
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: { thread.run(); }
    }
}

1 个答案:

答案 0 :(得分:0)

我不知道你对你的课程究竟想要什么(QThread,QObject ......),但是试试这个从qml调用c ++函数。

<强> myobject.cpp

#include "myobject.h"
#include <QDebug>
MyObject::MyObject(QObject *parent) : QObject(parent)
{

}

void MyObject::myFunction(double mouseX, double mouseY)
{
    qDebug() << "My cpp function" << mouseX << mouseY;
}

<强> myobject.h

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>

class MyObject : public QObject
{
    Q_OBJECT
public:
    explicit MyObject(QObject *parent = 0);

    Q_INVOKABLE void myFunction(double mouseX, double mouseY);

signals:

public slots:
};

#endif // MYOBJECT_H

<强>的main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "myobject.h"
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

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

    MyObject myObject;
    engine.rootContext()->setContextProperty("myObject", &myObject);


    return app.exec();
}

main.qml

import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.2

Window {
    id: mainWindow
    width: 100
    height: 100
    visible: true

MouseArea {
    id: mouseArea
    anchors.fill: parent
    onClicked: {
      myObject.myFunction(mouseArea.mouseX, mouseArea.mouseY);
    }
  }
}

希望这能帮到你!

阅读本文: http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html