[英]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 我正要在Debian系统上构建和测试部署我的应用程序,但是我遇到了一些麻烦
My application uses QOverload
for the QNetworkReply , as suggested by the documentation page. 根据文档页面的建议,我的应用程序对QNetworkReply使用QOverload
。
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: 在我的应用程序上调用make
时, make
报错:
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
并包含标题。
QOverload can be found in qglobal.h
but was only introduced in QT 5.7 可以在qglobal.h
中找到QOverload ,但仅在QT 5.7中引入
Qoverload requires the use of C++11 Qoverload需要使用C ++ 11
Or qOverload, using C++14 或qOverload,使用C ++ 14
The problem is that Ubuntu 16.04 uses Qt version 5.5: 问题是Ubuntu 16.04使用Qt版本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 check ing 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. 但是,上面的make
错误是由于包含预处理程序指令而导致的。 ie Including the Qt version check still threw the QOverload out of scope. 即包括Qt版本检查仍然使QOverload超出范围。
Question: 题:
Put simply, what did I do wrong? 简而言之,我做错了什么? Why, with a specific QT version check set, does make ignore this and build everything still? 为什么在设置了特定的QT版本检查后,make忽略了这一点并仍然构建了所有内容?
Update with possible alternative solutions 更新可能的替代解决方案
QOverload
entirely and replace with pre QT 5.7 code which will make my code slightly bulkier and some redundant code (possible) 完全消除QOverload
并替换为QT 5.7之前的代码,这将使我的代码稍大一些,并增加一些冗余代码(可能) You can use static_cast
to replace QOverload
: 您可以使用static_cast
替换QOverload
:
connect(networkReply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
[=](QNetworkReply::NetworkError code){ /* ... */ });
from here: Connecting overloaded signals and slots in Qt 5 从这里开始: 在Qt 5中连接过载信号和插槽
You have an error in logical expression here: 您在此处的逻辑表达式中有错误:
#if QT_VERSION <= QT_VERSION_CHECK(5, 7, 0)
Which basically says "enable following block if the version is less or equal than 5.7". 基本上说“如果版本小于或等于 5.7,则启用以下块”。 And Qt 5.5 meets this criteria. 并且Qt 5.5满足此标准。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.