Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use qOverload()?



  • I checked Qt Instructions and found out a qOverload function to deal with overload functions.
    It requires C++ 14 and I put CONFIG += c++14 to the .pro file.

    However there is always an error saying qOverload was undecleared. What's the problem?

    error: C2065: 'qOverload': undeclared identifier

    I'm using Qt 5.11 and MSVC 2017. Ref

    http://doc.qt.io/qt-5/qtglobal.html#qOverload


  • Lifetime Qt Champion

    Hi,

    Can you show the code where you use it ?


  • Moderators

    qOverload is not supported with MSVC. See QTBUG-61667 comments for discussion on it and rationale.

    As a side note - CONFIG switches for c++ standard do nothing for MSVC. It only recently added support for choosing standard version and to use that you need to pass them as explicit compiler flags e.g. QMAKE_CXXFLAGS += /std:c++14, but that's the default anyway so there's no point.



  • You can try the helper class QOverload which can be used directly:

    class Test : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Test(QWidget *parent = nullptr);
        void overloadedFunction();
        void overloadedFunction(QByteArray, QString);
        ~Test();
    };
    
    Test::Test(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Test)
    {
        QOverload<>::of(&Test::overloadedFunction);
        QOverload<QByteArray, QString>::of(&Test::overloadedFunction);
    }
    

    or you can use for example the static cast:

    static_cast<void (Test::*)(QByteArray, QString)>(&Test::overloadedFunction);



  • @Chris-Kawa said in How to use qOverload()?:

    qOverload is not supported with MSVC. See QTBUG-61667 comments for discussion on it and rationale.

    As a side note - CONFIG switches for c++ standard do nothing for MSVC. It only recently added support for choosing standard version and to use that you need to pass them as explicit compiler flags e.g. QMAKE_CXXFLAGS += /std:c++14, but that's the default anyway so there's no point.

    I tried MinGW earlier and found the same problem. That's why I switched to MSVC.



  • @SGaist said in How to use qOverload()?:

    Hi,

    Can you show the code where you use it ?

    It's a standard usage.

    connect(ui->combobox, qOverload<int>(&QComboBox::currentIndexChanged),  ...);
    

    doesn't work while

    connect(ui->combobox, QOverload<int>::of(&QComboBox::currentIndexChanged),  ...);
    

    works.



  • @Cobra91151 said in How to use qOverload()?:

    You can try the helper class QOverload which can be used directly:

    class Test : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Test(QWidget *parent = nullptr);
        void overloadedFunction();
        void overloadedFunction(QByteArray, QString);
        ~Test();
    };
    
    Test::Test(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Test)
    {
        QOverload<>::of(&Test::overloadedFunction);
        QOverload<QByteArray, QString>::of(&Test::overloadedFunction);
    }
    

    or you can use for example the static cast:

    static_cast<void (Test::*)(QByteArray, QString)>(&Test::overloadedFunction);

    Yes you are right. QOverlord works but it needs an of() function. qOverlord doesn't need it. That's their only difference to me.

    connect(ui->combobox, qOverload<int>(&QComboBox::currentIndexChanged),  ...);
    connect(ui->combobox, QOverload<int>::of(&QComboBox::currentIndexChanged),  ...);
    

  • Moderators

    I tried MinGW earlier and found the same problem. That's why I switched to MSVC.

    I just tried with MinGW 5.3.0 and Qt 5.11.1 and it works. Make sure that after you add CONFIG += c++14 in your .pro file you run qmake (Build menu -> Run qmake) and rebuild your project, otherwise it might not pick up the change.


Log in to reply