【问题标题】:Visual studio warning when using Qt connect使用 Qt 连接时的 Visual Studio 警告
【发布时间】:2019-12-12 13:17:35
【问题描述】:

所以我正在使用带有 Qt 5.12.2 C++ 的 Visual Studio 2019。我制作了一个非常基本的表单ui,如下所示:

MyApp::MyApp(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    connect(ui.pushButton, &QPushButton::clicked, this, &MyApp::onConnectClicked);
}

void MyApp::onConnectClicked()
{
    ui.pushButton->setText("Clicked");
}

程序运行良好,但在这一行:

    connect(ui.pushButton, &QPushButton::clicked, this, &MyApp::onConnectClicked);

Visual Studio 不断给我这个警告

Severity Code Description Project File Line Suppression State
Warning C26444  Avoid unnamed objects with custom construction and destruction (es.84).

还有这个错误:

Severity Code Description Project File Line Suppression State
Error (active)  E2524   the argument to a feature-test macro must be a simple identifier    
at C:\Dev\5.12.2\msvc2017_64\include\QtCore\qcompilerdetection.h line 1349  

我在这里做错了吗?请帮忙!

【问题讨论】:

    标签: c++ visual-studio qt qt5 visual-studio-2019


    【解决方案1】:

    2020 年 4 月 8 日更新: 微软已经解决了这个问题。最新的 VS2019 版本不再在每次使用 Qt 的 connect() 时发出警告。

    对于因任何原因无法更新的人,我将在下面保留旧答案。

    由于您按照预期的方式使用 Qt,并且由于我们可以假设 Qt 的作者知道他们在做什么,因此可以合理地假设警告是过分热心的,可以在没有危险的情况下摆脱它,并且不是值得付出很多努力。

    我用来摆脱它的方法是将connect()的结果转换为void,即

    (void)connect(ui.pushButton, &QPushButton::clicked, this, &MyApp::onConnectClicked);
    

    强制转换没有效果,但它清楚地表达了一个人打算忽略结果,它相当不干扰,它使警告消失。

    【讨论】:

    • 感谢您将旧答案留在那里,我们暂时使用较旧的 VS2019 版本
    【解决方案2】:

    不确定是否有帮助,但 VS 方面正在调查类似的问题:

    https://developercommunity.visualstudio.com/content/problem/520400/visual-studio-2019-with-qt-vs2019-detects-many-war.html

    它是以下一个的副本:

    https://developercommunity.visualstudio.com/content/problem/520393/vs-2019-detects-many-false-warnings-while-vs2017-w.html

    简而言之,他们声称这些警告是错误的标志,并提出了一种解决方法:

    如果您使用 VS2017 工具链重新配置项目,请清理并 重新生成项目,所有这些警告都会消失。

    【讨论】:

    • 感谢您的回答。但是,您是否建议我将 VS2019 降级到 VS2017?
    猜你喜欢
    • 2013-06-02
    • 2011-05-19
    • 1970-01-01
    • 2013-10-05
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2016-08-24
    相关资源
    最近更新 更多