Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt signals slots, cast type in new notation [duplicate]

Tags:

c++

qt

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?

like image 947
nsejxT nsejxT Avatar asked Feb 11 '15 22:02

nsejxT nsejxT


1 Answers

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

like image 178
ftynse Avatar answered Oct 19 '22 20:10

ftynse