找到一种兼容qml和c++的办法

This commit is contained in:
2020-04-10 15:46:03 +08:00
parent 30b19634ff
commit 29ab0b1a9a
9 changed files with 130 additions and 16 deletions
+2
View File
@@ -55,10 +55,12 @@ CONFIG += c++11 \
warn_off
SOURCES += \
commandmsg.cpp \
main.cpp \
mainwindow.cpp
HEADERS += \
commandmsg.h \
mainwindow.h
# Default rules for deployment.
+12 -11
View File
@@ -2,7 +2,9 @@ import QtQuick 2.6
import QtQuick.Controls 2.5
//导入指令模块,这样就能发指令了
import Drivers.LinkUI 1.0
//import Drivers.LinkUI 1.0
import CommandMsg 1.0
Rectangle {
@@ -18,14 +20,10 @@ Rectangle {
signal btnclicked_int(real param1, real param2, real param3, real param4, int x, int y, real z)
signal btnclicked_long(real param1, real param2, real param3, real param4, real param5, real param6, real param7)
/*
LinkUI {
id:linkui
setWindowTitle: "nini"
CommandMsg {
objectName: 'GetInfo'
id: data
}
*/
Rectangle {
@@ -71,8 +69,8 @@ Rectangle {
}
onClicked: {
console.log("clicked")
btnclicked_int(0,0,0,0,0,0,0)
data.text = "ARM"
}
}
@@ -89,6 +87,9 @@ Rectangle {
border.width:1
color: "lightgreen"
Text{
anchors.centerIn: parent
text: qsTr("TakeOff")
@@ -121,7 +122,7 @@ Rectangle {
}
onClicked: {
console.log("clicked")
data.text = "TakeOff"
}
}
@@ -172,7 +173,7 @@ Rectangle {
}
onClicked: {
console.log("clicked")
data.text = "Kill"
}
}
+27
View File
@@ -0,0 +1,27 @@
#include "commandmsg.h"
CommandMsg::CommandMsg()
{
}
QString CommandMsg::text() const
{
return m_text;
}
void CommandMsg::setText(const QString &text)
{
m_text = text;
emit textChanged(m_text);
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef COMMANDMSG_H
#define COMMANDMSG_H
#include <QObject>
class CommandMsg : public QObject
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText)
public:
CommandMsg();
QString text() const;
void setText(const QString &text);
signals:
void textChanged(QString str);
private:
QString m_text;
};
#endif // COMMANDMSG_H
+5 -2
View File
@@ -110,9 +110,12 @@ void LoadLang(QApplication *a)
void RegisterType(void)
{
qmlRegisterModule("Drivers.LinkUI",1,0);
//qmlRegisterModule("Drivers.LinkUI",1,0);
//qmlRegisterType<LinkUI>("Drivers.LinkUI",1,0,"LinkUI");
qmlRegisterType<LinkUI>("Drivers.LinkUI",1,0,"LinkUI");
}
+33
View File
@@ -70,6 +70,10 @@ MainWindow::MainWindow(QWidget *parent)
copk = new Cockpit(this);
copk->setGeometry(this->width() - copk->width(),0,300,340);
//作为一个位于本工程的类,是可以注册的
qmlRegisterType<CommandMsg>("CommandMsg", 1, 0, "CommandMsg");
quick = new QQuickWidget(this);
quick->setFocus();
quick->setResizeMode(QQuickWidget::SizeRootObjectToView);
@@ -80,6 +84,19 @@ MainWindow::MainWindow(QWidget *parent)
quick->show();
QObject *pRoot = (QObject*)quick->rootObject();
const QObjectList list = pRoot->children();
/* 这一段可以读取qml底下的第n个对象,例如CommandMsg位于第0个,所有读取第0个对象连接 */
m_data = static_cast<CommandMsg*>(list[0]);
qDebug() << list[0]->metaObject()->className();
qDebug() << list[0]->objectName();
connect(m_data, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
updateTimer = new QTimer();
connect(updateTimer,&QTimer::timeout,
@@ -102,6 +119,22 @@ MainWindow::MainWindow(QWidget *parent)
}
void MainWindow::textChanged(const QString &str)
{
QString currentData = QDateTime::currentDateTime().toString("hh:mm:ss") + " ";
currentData.append(str);
qDebug() << currentData;
}
MainWindow::~MainWindow()
{
map->close();
+18
View File
@@ -38,6 +38,14 @@
#include "QQuickView"
//cmd interface
#include "commandmsg.h"
QT_BEGIN_MOC_NAMESPACE
#pragma execution_character_set("utf-8")
@@ -76,6 +84,12 @@ private slots:
void updateUI();
protected slots:
void textChanged(const QString &str);
protected:
Cockpit *copk;
@@ -101,6 +115,10 @@ protected:
QTimer *updateTimer;
CommandMsg *m_data;
};
#endif // MAINWINDOW_H
+5
View File
@@ -69,6 +69,11 @@ protected:
};
QT_END_NAMESPACE
#endif // DLINK_H
+3 -3
View File
@@ -9,8 +9,8 @@ TARGET = Dlink
TEMPLATE = lib
DEFINES += DLINK_LIBRARY
DEFINES += DLINK_LIBRARY \
QtDlink
CONFIG += c++11 \
dll \
@@ -60,7 +60,7 @@ DESTDIR = $$PWD/../thirdpart/lib
MOC_DIR = $$PWD/../build
OBJECTS_DIR = $$PWD/../build
DEFINES += QtDlink
win32|win64 {
src_dir = $$PWD\\*.h