QObject::connect: Cannot queue arguments of type 'QVector<Position>&'解决办法
时间: 2024-03-22 14:41:16 浏览: 71
实现QObject与JavaScript通讯
可以尝试使用以下两种方法解决此问题:
1. 将 QVector<Position> 类型的参数转换为 QVariant 类型,然后再传递给信号和槽。这可以通过 QVariant::fromValue 函数实现。示例代码如下:
```
QVector<Position> positions;
QVariant variant = QVariant::fromValue(positions);
connect(sender, SIGNAL(signal(QVariant)), receiver, SLOT(slot(QVariant)));
emit signal(variant);
```
接收端的槽函数可以这样实现:
```
void Receiver::slot(QVariant variant)
{
QVector<Position> positions = variant.value<QVector<Position>>();
// 处理 positions
}
```
2. 自定义信号和槽,将 QVector<Position> 类型的参数作为自定义信号的参数。示例代码如下:
```
class Sender : public QObject
{
Q_OBJECT
public:
void sendPositions(const QVector<Position>& positions)
{
emit positionsSent(positions);
}
signals:
void positionsSent(const QVector<Position>& positions);
};
class Receiver : public QObject
{
Q_OBJECT
public slots:
void receivePositions(const QVector<Position>& positions)
{
// 处理 positions
}
};
Sender sender;
Receiver receiver;
connect(&sender, SIGNAL(positionsSent(QVector<Position>)), &receiver, SLOT(receivePositions(QVector<Position>)));
QVector<Position> positions;
sender.sendPositions(positions);
```
上述两种方法都可以解决 QObject::connect: Cannot queue arguments of type 'QVector<Position>&' 的问题。
阅读全文