gpt4 book ai didi

c++ - QT:使用 move 语义将信号连接到插槽

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:08:05 25 4
gpt4 key购买 nike

好的,我有这样的东西:

void EmailReceiverThread::foreachEmailAccount(ConfigEmailAccount account)
{
Email email;
EmailReader emailReader(account);
while (emailReader.pollEmail(email))
{
writeEmailToDatabase(email);
}
}

ForeachEmailAccount 是 EmailReceiverThread 中的一个插槽。它是从 run() 方法调用的:

GetConfigEmailDAO* accountReader = new GetConfigEmailDAO(type, m_channel);
connect(accountReader, SIGNAL(emailAccount(ConfigEmailAccount)),
this, SLOT(foreachEmailAccount(ConfigEmailAccount)), Qt::QueuedConnection);
DbThread::postAction(accountReader);

GetConfigEmailDAO 进行查询,获取电子邮件帐户并发出:

emit emailAccount(account);

到目前为止,这是有效的。问题是,我正在复制 ConfigEmailAccount 类。我想避免这种性能下降。所以,我的想法是使用 c++11 move 语义发出帐户:

emit emailAccount(std::move(account));

我用新语法重写了槽和信号:

void foreachEmailAccount(ConfigEmailAccount&& account);

不幸的是,这不起作用。我有下一个编译错误:

moc_emailreceiverthread.cpp:88: error: cannot bind ‘ConfigEmailAccount’ lvalue to ‘ConfigEmailAccount&&’
case 1: _t->foreachEmailAccount((*reinterpret_cast< ConfigEmailAccount(*)>(_a[1]))); break;

我能做什么?

此处可验证的最小示例:

#include <QCoreApplication>
#include <QObject>

class ConfigEmailAccount
{
QString some_data;
};

class GetConfigEmailDAO : public QObject
{
Q_OBJECT
public:
void execute();

signals:
void emailAccount(ConfigEmailAccount&& account);
};

void GetConfigEmailDAO::execute()
{
ConfigEmailAccount account;
emit emailAccount(std::move(account));
}

class EmailReceiverThread : public QObject
{
Q_OBJECT
public:
void execute();
public slots:
void foreachEmailAccount(ConfigEmailAccount&& account);
};

void EmailReceiverThread::execute()
{

GetConfigEmailDAO* accountReader = new GetConfigEmailDAO;
connect(accountReader, SIGNAL(emailAccount(ConfigEmailAccount)),
this, SLOT(foreachEmailAccount(ConfigEmailAccount)), Qt::QueuedConnection);
accountReader->execute();
}

void EmailReceiverThread::foreachEmailAccount(ConfigEmailAccount&& account)
{

}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qRegisterMetaType<ConfigEmailAccount>("ConfigEmailAccount");
EmailReceiverThread reader;
reader.execute();
return a.exec();
}

#include "main.moc"

最佳答案

最后采用了QT的浅拷贝机制:
http://doc.qt.io/qt-5.6/qshareddatapointer.html#details

关于c++ - QT:使用 move 语义将信号连接到插槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46521742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com