Qt signals slots, cast type in new notation [dupli

2020-07-13 08:22发布

Given the following two:

connect(ui->comboBox, SIGNAL(activated(QString)), ps, SLOT(requestPlotsAvailable(QString)));
connect(ui->comboBox, &QComboBox::activated, ps, &PlotSystem::requestPlotsAvailable);

The first uses the old notation, which works. The second uses the new notation and gives the error

error: no matching function for call to 'PlotSystemGui::connect(QComboBox*&, <unresolved overloaded function type>)'

How to avoid the error using the new notation?

标签: c++ qt
1条回答
迷人小祖宗
2楼-- · 2020-07-13 09:02

This should work

connect(ui->comboBox, 
        static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
        ps,
        &PlotSystem::requestPlotsAvailable);

Qt 5.7 introduces qOverload (or QOverload for C++11) for convenience/readability:

connect(ui->comboBox, 
        qOverload<const QString &>(&QComboBox::activated),
        ps,
        &PlotSystem::requestPlotsAvailable);

See this question about pointers to overloaded functions

查看更多
登录 后发表回答