大家好
背景:
我正要在debian系统上构建和测试部署我的应用程序,但是我遇到了一些麻烦
正如QNetworkReply页面所建议的那样,我的应用程序将QOverload
用作documentation。
用法示例是(从文档页面开始):
connect(networkReply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
[=](QNetworkReply::NetworkError code){ /* ... */ });
或在我的应用程序中发现的其他用法:
# 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
});
问题:
在我的应用程序上调用
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
现在,这使我检查何时找到
QOverload
并包含 header 。QOverload可以在
qglobal.h
中找到,但仅在QT 5.7中引入问题是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
我尝试了什么:
我决定为我使用preprocessor directives to do the version check ing。
#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
但是,上述
make
错误是由于包含预处理程序指令导致的。即,包括Qt版本检查仍然使QOverload超出范围。问题:
简而言之,我做错了什么?为什么在设置了特定的QT版本检查的情况下,make会忽略它并仍然构建所有内容?
使用可能的替代解决方案进行更新
QOverload
并替换为QT 5.7之前的代码,这将使我的代码稍大一些,并增加一些冗余代码(可能)最佳答案
您可以使用static_cast
替换QOverload
:
connect(networkReply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
[=](QNetworkReply::NetworkError code){ /* ... */ });
从这里:Connecting overloaded signals and slots in Qt 5