lundi 18 décembre 2017

Qt Version checking and QOverload not defined in scope

Good day all

Background:

I am at the point of building and test-deploying my application on debian systems, however I have run into some trouble

My application uses QOverload for the QNetworkReply, as suggested by the documentation page.

An example usage is (from doc page):

connect(networkReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
    [=](QNetworkReply::NetworkError code){ /* ... */ });

or another usage found in my application:

# header
QButtonGroup *grpProtocol;
QMetaObject::Connection conProtocol

# implementation
conProtocol = QObject::connect(ui->grpProtocol, QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled), [this](){
    //do some action when a radio button in a QButtonGroup is toggled
});

Problem:

When calling make on my application, make complains with an error:

mainwindow.cpp: In member function ‘void MainWindow::initConnectors()’:
mainwindow.cpp:462:53: error: ‘QOverload’ was not declared in this scope
     conProtocol = QObject::connect(ui->grpProtocol, QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled), [this](){
                                                     ^
mainwindow.cpp:462:79: error: expected primary-expression before ‘*’ token
     conProtocol = QObject::connect(ui->grpProtocol, QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled), [this](){
                                                                               ^
mainwindow.cpp:462:80: error: expected primary-expression before ‘,’ token
     conProtocol = QObject::connect(ui->grpProtocol, QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled), [this](){
                                                                                ^
mainwindow.cpp:462:82: error: expected primary-expression before ‘bool’
     conProtocol = QObject::connect(ui->grpProtocol, QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled), [this](){
                                                                                  ^
Makefile:476: recipe for target 'mainwindow.o' failed

Now, this leads me to check when QOverload is found and include the header.

QOverload can be found in qglobal.h but was only introduced in QT 5.7

  • Qoverload requires the use of C++11

  • Or qOverload, using C++14

The problem is that Ubuntu 16.04 uses Qt version 5.5:

qtchooser -print-env
QT_SELECT="default"
QTTOOLDIR="/usr/lib/x86_64-linux-gnu/qt5/bin"
QTLIBDIR="/usr/lib/x86_64-linux-gnu"

/usr/lib/x86_64-linux-gnu/qt5/bin/qmake -v
QMake version 3.0
Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnu

What I tried:

I resolved to using preprocessor directives to do the version checking for me.

#if QT_VERSION <= QT_VERSION_CHECK(5, 7, 0)
    qDebug(mainclient) << "Adding connector for settings prefered connection protocol";
    conProtocol = QObject::connect(ui->grpProtocol, QOverload<QAbstractButton *, bool>::of(&QButtonGroup::buttonToggled), [this](){
        // my code here which which handles events
    });
#endif

However, the above error of make resulted with the preprocessor directive included. i.e. Including the Qt version check still threw the QOverload out of scope.

Question:

Put simply, what did I do wrong? Why, with a specific QT version check set, does make ignore this and build everything still?

Aucun commentaire:

Enregistrer un commentaire