Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio warning when using Qt connect

So I am using Visual Studio 2019 with Qt 5.12.2 C++. I made a very basic form ui like this:

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

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

The program runs fine but at this line:

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

Visual Studio keeps giving me this warning

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

Also this error:

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  

Am I doing something wrong here? Please help!

like image 729
aleadinglight Avatar asked Aug 05 '19 01:08

aleadinglight


1 Answers

Update 2020-04-08: Microsoft has fixed this. The latest VS2019 release no longer issues a warning on every single use of Qt's connect().

I'm leaving the old answer in place below for people who cannot update for whatever reason.

Since you're using Qt the way it's intended, and since we can assume that Qt's authors know what they are doing, it is reasonable to assume that the warning is overzealous, can be gotten rid off without danger, and is not worth much effort.

The method I use to get rid of it is to cast the result of connect() to void, i.e.

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

The cast has no effect, but it clearly expresses that one intends to ignore the result, it is fairly unintrusive, and it makes the warning disappear.

like image 86
tobi_s Avatar answered Oct 04 '22 14:10

tobi_s