我应该怎么做才能使用自定义类对象的 QVector 将信号发送到插槽
What should I do to be able to send signals to slots with QVector of my custom class objects
我应该怎么做才能将信号发送到带有QVector
自定义类对象作为参数的插槽?
struct LicenseInfo
{
QString company_name;
QString server_name;
QString product_name;
int product_version;
QString license_end;
QString last_update;
QString comment;
};
用法
connect(_worker, SIGNAL(newLicensesActivated(QVector<LicenseInfo>)),
this, SLOT(newLicensesActivated(QVector<LicenseInfo>)));
可以执行以下操作吗?
#include "licenseinfo.h"
Q_DECLARE_METATYPE(LicenseInfo)
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qRegisterMetaType<QVector<LicenseInfo>>();
MainWindow w;
w.show();
return a.exec();
}
在这种情况下,我应该同时使用Q_DECLARE_METATYPE
宏和qRegisterMetaType
函数吗?
当尝试传递对象的Qt容器时,我最担心的事情是确保容器中的对象不会超出范围。 如果内容是简单或简单的类型,则不需要这样做。
我更喜欢在堆上制作项目(使用 new
(,并传递指针容器。 然后我确保在其生命周期结束时正确清理它们......经常使用qDeleteAll()
. 它需要预先进行更多的计划,但以后会减少很多错误。
我也喜欢确保我为工作使用正确的容器。 就像使用 QSet 而不是 QVector 一样,甚至只是使用 QList 可能是有意义的。 但是,您可能需要实现一个比较运算符来使其可以比较和排序对象。
另一条建议是,小心在你的qRegisterMetaType或任何声明中>>
。 在中间添加一个空格,这样就不会模棱两可。 qRegisterMetaType< QVector<LicenseInfo> >();
(注意夸张的空格(。
希望有帮助。
相关文章:
- Qt VTK交互风格的信号到小部件
- 将 Qvector<uint8_t> 转换为 QString
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何在没有信号的情况下从C++执行QML插槽
- 线程之间的布尔停止信号
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 正确清洁 QVector
- 有可能在信号处理程序中设置promise吗
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 在条件变量中触发错误信号的频率是多少
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 调试和自由执行中的信号处理
- QT - 如何从 QVector<QComboBox* 获取信号"currentIndexChanged">
- 通过排队的QT信号插槽连接发送QSHAREDPOINTERS的QVECTOR
- 在Qt信号中发射QVector参考会导致复制
- 我应该怎么做才能使用自定义类对象的 QVector 将信号发送到插槽
- 将QVector从一个信号传递到不同线程中的插槽会导致数据复制吗?