导入语句未列出我公开的自定义 C++ class

Import statement does not list my custom C++ exposed class

我正在使用 Qt 为我的客户开发一些演示项目,该项目使用 mysql 数据库和 QML。现在,我已经使用 QML 创建了基本的图形用户界面,创建了基本的数据库 class,这里是项目文件:

TEMPLATE = app

QT += qml quick sql

SOURCES += main.cpp \
    uesqldatabase.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)

HEADERS += \
    uesqldatabase.h

这里暴露了class头文件:

#ifndef UESQLDATABASE_H
#define UESQLDATABASE_H

#include <QObject>
#include <QtSql/QSqlDatabase>

class UeSqlDatabase : public QObject
{
    Q_OBJECT

    Q_PROPERTY(bool isConnected READ isConnected NOTIFY ueConnectedChanged)

private:
    bool m_ueConneted;

    inline void setConnected(const bool& ueConnected)
        { this->m_ueConneted=ueConnected; }

public:
    explicit UeSqlDatabase(QObject *parent = 0);

    inline const bool& isConnected() const
        { return this->m_ueConneted; }

    ~UeSqlDatabase();

signals:
    void ueConnectedChanged();

public slots:
    void ueConnectToDatabase (const QString& ueStrHost, const QString& ueStrDatabase,
                              const QString& ueStrUsername, const QString& ueStrPassword);
};

#endif // UESQLDATABASE_H

这里公开了class实现:

#include "uesqldatabase.h"

UeSqlDatabase::UeSqlDatabase(QObject *parent)
    : QObject(parent)
{
    this->setConnected(false);
}   // constructor

UeSqlDatabase::~UeSqlDatabase()
{
}   // destructor

void UeSqlDatabase::ueConnectToDatabase (const QString& ueStrHost, const QString& ueStrDatabase,
                                         const QString& ueStrUsername, const QString& ueStrPassword)
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL",
                                              "testDb");

    db.setHostName(ueStrHost);
    db.setDatabaseName(ueStrDatabase);
    db.setUserName(ueStrUsername);
    db.setPassword(ueStrPassword);

    if(db.open())
    {
        this->setConnected(true);
    }
}   // ueConnectToDatabase

这里是 main.cpp 代码:

#include <QtQml>
#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include "uesqldatabase.h"

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

    QQmlApplicationEngine engine;

    qmlRegisterType<UeSqlDatabase>("si.mikroelektronika",
                                   1,
                                   0,
                                   "UeSqlDatabase");

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

    return app.exec();
}

应用程序运行没有问题,但是当我尝试在 main.qml 中导入暴露的 class (UeSqlDatabase) 时,QtCreator 找不到我的 class。我在做什么 wrong/missing?

您需要导入模块,而不是 class。与您的 qmlRegisterType 调用对应的导入将是

import si.mikroelektronika 1.0