6

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?

nsejxT nsejxT
  • 87
  • 1
  • 4

1 Answers1

8

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

derke
  • 469
  • 4
  • 6
ftynse
  • 787
  • 4
  • 9
  • It returns another error: no matches converting function 'activated' to type 'void (class QComboBox::*)(class QString)' – nsejxT nsejxT Feb 11 '15 at 22:42
  • 1
    yep, because it should be `const QString &` instead of `QString`. (edited answer) – ftynse Feb 11 '15 at 22:44
  • The problem is, as described in the first message, that the compiler cannot resolve which of the overloaded functions to take. So `static_cast` needed to tell explicitly. – ftynse Feb 11 '15 at 22:45
  • Thanks for the explanaton, I guess I understand the problem. – nsejxT nsejxT Feb 11 '15 at 22:50
  • Let's say I want to link an int signal with a QString slot, Is it possible to link diferent types? – nsejxT nsejxT Feb 12 '15 at 14:11
  • I assume it will work if an implicit conversion sequence exists between types. Thus connecting an `int` signal to a `double` slot will work, but not to the `QString` slot since there is no implicit conversion. – ftynse Feb 12 '15 at 14:39