Qt connect - 对象在发出连接之前发出信号

时间:2015-09-14 09:22:25

标签: qt qt-signals

我关注main.cpp

#include <QtQml>
#include <QApplication>
#include <QQmlApplicationEngine>

#include "database/uepeoplemodel.h"
#include "core/uestatus.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QQmlApplicationEngine engine;

    UeStatus* ueApplicationStatus=new UeStatus(qApp);
    UePeopleModel* uePeopleModel=new UePeopleModel(qApp);

    QObject::connect(uePeopleModel,
                     SIGNAL(ueSignalDatabaseConnectionChanged(UeTypeDatabaseConnectionStatus)),
                     ueApplicationStatus,
                     SLOT(ueSignalDatabaseConnectionChanged(UeTypeDatabaseConnectionStatus)));

    engine.rootContext()->setContextProperty("uePeopleModel",
                                             uePeopleModel);
    engine.rootContext()->setContextProperty("ueApplicationStatus",
                                             ueApplicationStatus);
    engine.addImageProvider(QLatin1String("uePeopleModel"),
                            uePeopleModel);

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

    return app.exec();
}

现在,在main.cpp内我从类中创建了两个对象:

UeStatus* ueApplicationStatus=new UeStatus(qApp);
UePeopleModel* uePeopleModel=new UePeopleModel(qApp);

我将信号从UePeopleModel连接到ueApplicationStatus中的插槽:

QObject::connect(uePeopleModel,
    SIGNAL(ueSignalDatabaseConnectionChanged(UeTypeDatabaseConnectionStatus)), 
    ueApplicationStatus,
    SLOT(ueSignalDatabaseConnectionChanged(UeTypeDatabaseConnectionStatus)));

问题是当uePeopleModel被创建时,信号在构造函数中被设置:

UePeopleModel::UePeopleModel(QObject* parent)
    : QSqlQueryModel(parent),
      QQuickImageProvider(QQmlImageProviderBase::Image,
                          QQmlImageProviderBase::ForceAsynchronousImageLoading)
{
    if(!QSqlDatabase::connectionNames().contains(UePosDatabase::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE,
                                                 Qt::CaseInsensitive))
    {
        this->ueSetDatabase(QSqlDatabase::addDatabase(UePosDatabase::DATABASE_DRIVER,
                                                      UePosDatabase::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE));
    }   // if

    this->ueDatabase().setHostName(/*this->uePosSettings()->ueDbHostname()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_HOSTNAME);
    this->ueDatabase().setDatabaseName(/*this->uePosSettings()->ueDbName()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_NAME);
    this->ueDatabase().setUserName(/*this->uePosSettings()->ueDbUser()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_USERNAME);
    this->ueDatabase().setPassword(/*this->uePosSettings()->ueDbPassword()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_PASSWORD);

    if(this->ueDatabase().open())
    {
        emit this->ueSignalDatabaseConnectionChanged(CONNECTED);
        this->setQuery(UePosDatabase::UeSqlQueries::UeTablePeople::SQL_QUERY_GET_ALL_PEOPLE,
                       this->ueDatabase());
/*
        if(this->lastError().isValid())
            qDebug() << this->lastError();
*/
    }
    else
    {
        emit this->ueSignalDatabaseConnectionChanged(NOT_CONNECTED);
//        qDebug() << this->ueDatabase().lastError();
    }

//    qDebug() << this->ueDatabase().connectionNames();
}   // default constructor
发出connect之前的

并且ueApplicationStatus对象未捕获的是插槽。有谁知道如何摆脱这种情况?

0 个答案:

没有答案