信号重载

最常见的重载信号就是QComboBox::currentIndexChanged,它有intconst QString &两种重载参数

1
2
3
4
5
6
7
8
9
10
11
12
13
connect(ui->algorithm, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [=](int index) {
if(index == 0) {
this->initTable(PID);
}
else {
this->initTable(Model);
}
emit this->changeAlgorithm(index);
});

connect(ui->algorithm, QOverload<const QString &>::of(&QComboBox::currentIndexChanged), this, [=](const QString &text) {
qDebug() << text;
});

槽函数重载

以传递自定义参数类型为例,自定义结构体需要注册成Qt的元数据类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma pack (4)
typedef struct {
...
} TAlgorithmModel;
#pragma pack ()

Q_DECLARE_METATYPE(TAlgorithmModel *)

#pragma pack (4)
typedef struct {
...
} TAlgorithmPid;
#pragma pack ()

Q_DECLARE_METATYPE(TAlgorithmPid *)

发送方信号

1
2
3
// 原始信号
void NetworkManager::updateModel(const TAlgorithmModel *);
void NetworkManager::updatePid(const TAlgorithmPid *);

接收方槽函数重载

1
2
3
// 重载槽函数
void OperatePage::onUpdateTable(const TAlgorithmModel *p);
void OperatePage::onUpdateTable(const TAlgorithmPid *p);

信号和槽写法参考

1
2
connect(NetworkManager::getInstance(), &NetworkManager::updateModel, operate, QOverload<const TAlgorithmModel *>::of(&OperatePage::onUpdateTable));
connect(NetworkManager::getInstance(), &NetworkManager::updatePid, operate, QOverload<const TAlgorithmPid *>::of(&OperatePage::onUpdateTable));

© 2025 hywing 使用 Stellar 创建
总访问 113701 次 | 本页访问 326