我正在我的项目中进行数据库操作。我有一个文件“dataBaseMaster”,它有一个函数 updateIntoTestResult()。我通过这样设置上下文创建了一个 dbMasterObject
qml->setContextProperty("dbMasterObject", dbMasterObject);
但是当我尝试在我的 main.qml 中调用这个 updateIntoTestResult() 方法时,我收到这样的错误
TypeError:表达式 'dbMasterObject.updateIntoTestResult' [undefined] 的结果不是函数
数据库管理器.hpp
class DatabaseMaster : public QObject
{
public:
Q_OBJECT
public:
void updateIntoTestResult(int id, int result);
};
数据库管理器.cpp
void DatabaseMaster::updateIntoTestResult(int id, int result) {
QSqlDatabase database = QSqlDatabase::database();
QSqlQuery query(database);
query.prepare("update "+TEST_RESULT_MASTER+" set "+RESULT+" = :"+RESULT+" where "+TEST_ID+"= :"+TEST_ID+";");
query.bindValue(":"+RESULT, result);
query.bindValue(":"+TEST_ID, id);
query.exec();
调用main.qml
dbMasterObject.updateIntoTestResult(MICROPHONE_ID, TEST_STATE_PASS)
请帮帮我,为什么我会收到这个错误,而我已经正确地声明了那个方法。
提前致谢..!!
最佳答案
updateIntoTestResult
不为 qml 系统所知,因为它不是插槽或 Q_INVOKABLE
。
简单修复 - 插入 Q_INVOKABLE
:
class DatabaseMaster : public QObject
{
public:
Q_OBJECT
public:
Q_INVOKABLE void updateIntoTestResult(int id, int result);
};
关于c++ - 类型错误 : Result of expression 'dbMasterObject.updateIntoTestResult' [undefined] is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18611349/