建立工程后,再新建一个新的类(如class kls),类声明在kls.h头文件中,在main.cpp中 #include "kls.h" 但是在链接过程中报错:类kls的所有成员函数(构造函数,析构函数等)均无法识别,被编译器标为“无法解析的外部符号”。
解决:
将当前qt工程所在的父目录下的 build-(工程名)-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug 文件夹删除,再构建项目即可运行
C++与qt quick混合编程
<kls.pro>
QT += quick
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
camera_get.cpp \
kls.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
camera_get.h \
kls.h
<main.cpp>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
/********************************/
#include "camera_get.h"
/********************************/
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
/********************************************/
qmlRegisterSingletonType<Camera_get>("fun",1,0,"Camera_get",backend_singletontype_provider);
/********************************************/
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
<main.qml>
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
/*************************/
import fun 1.0
/*************************/
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
/*****************************************/
Rectangle{
id:but1
anchors.top: parent.top
anchors.topMargin: 30
anchors.left: parent.left
anchors.leftMargin: 30
width: 150
height: 30
color: "#cccccc"
Label{
anchors.centerIn: parent
text:"yunxing"
}
Button{
anchors.fill: parent
opacity: 0.0
onClicked: {
console.log("click")
Camera_get.set_expose()
}
}
}
/*****************************************/
}
<camera_get.h>
#ifndef CAMERA_GET_H
#define CAMERA_GET_H
#include <QObject>
#include <QQmlEngine>
#include <QJSEngine>
#include <QString>
class Camera_get : public QObject
{
Q_OBJECT
public:
explicit Camera_get(QObject *parent = nullptr);
/********************************************************/
//定义类函数添加在这儿
Q_INVOKABLE void set_expose();
/********************************************************/
signals:
public slots:
};
/***************************************************************/
//添加这一句
static QObject *backend_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
Camera_get *example = new Camera_get();
return example;
}
/***************************************************************/
#endif // CAMERA_GET_H
<camera_get.cpp>
#include "camera_get.h"
/***********************************/
#include <QDebug>
#include "kls.h"
/************************************/
Camera_get::Camera_get(QObject *parent) : QObject(parent)
{
}
/******************************************/
void Camera_get::set_expose()
{
qDebug()<<"yuxing";
Kls::fun3();
//Kls::fun3();
}
/******************************************/
<kls.h>
#ifndef KLS_H
#define KLS_H
class Kls
{
public:
Kls();
static void fun3();
};
#endif // KLS_H
<kls.cpp>
#include "kls.h"
#include "QDebug"
Kls::Kls()
{
}
void Kls::fun3()
{
qDebug()<<"yunxingtest234";
}