Qt 5 build error: extra characters after test expression

I am trying to learn Qt 5.3, and this is my first program (hello world). When I try to build, it displays this error:

extra characters after test expression.

I cannot understand at all why this error comes up. I just took some simple code from the Internet to check whether I have installed Qt properly or not. Here is the code:

#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
    QApplication app (argc, argv);
    QPushButton button ("Hello world !");
    return app.exec();

The error is displayed in lines 3, 5, 6, 8. I am completely new to Qt, so please give a simple explanation.


Check the .pro file. Sometimes it's because you don't have a blackslash at the end of one of the lines that isn't last:

HEADERS += Qt/mainwindow.h \
    Qt/MPrintableWidget.h \
    Qt/MPrintableWidgetGroup.h    # Oooops forgot the \ here
    Qt/MFixedSizeDialog.h \

Every line here except the bottom one needs to end in a backslash.

That code compiles and links and runs just fine here, so the only thing I can think of is a problem with the include files on your machine.

First, make sure that you're using Qt5 and that you've got Qt5 include files.

Second, you might try compiling it from the command line. Here's what I just used on my Linux box with Qt5 and g++:

g++ -fPIC -I /usr/include/qt5/QtWidgets -I /usr/include/qt5 qttest.cpp -o qttest -lQt5Core -lQt5Widgets

You haven't said which operating system you're using, or how you're building. Whatever you're using, you'll need to verify that it's actually using the correct files.

I just met the same error. It is in .pro file. You can open and check carefully. - You forgot to put blackslash at the end of a line. - You put another character after blackslash (in the same line).

I was experiencing this error message on Windows. Brand new projects created by Qt Creator were unable to parse the .pro or build. The core problem is a registry key I added to change the codepage for all cmd.exe instances, so that it would better support Unicode output.

The registry key was named Autorun with the string value chcp 65001. (There are various places in the registry where this key can live; use the search feature of Regedit to see if you have such a key. Mine happened to be buried inside a path involving a GUID for my specific user.) The symptoms I was having were error messages like this on brand new projects:

C:/Qt/5.11/msvc2017_64/mkspecs/features/toolchain.prf(208): Extra characters after test expression.
Project ERROR: Compiler produced empty value for Active.
C:/Qt/5.11/msvc2017_64/mkspecs/features/toolchain.prf(208): Extra characters after test expression.
Error while parsing file C:\Users\phrogz\Documents\FooBar\FooBar.pro. Giving up.

The problem, I surmise, is that Qt Creator uses cmd.exe on Windows to run code, and parses the text output from this. Qt Creator must have a fragile text parser of the output that fails when Autorun puts extra output in the command processor on startup. (Perhaps it's starting on a specific line number, skipping over over cmd.exe startup spam?)

The solution for me was to change the Autorun command from chcp 65001 to @chcp 65001>nul, which prevents it from spamming Active code page: 65001 into the output.

I found this problem being discovered and discussed in this Qt Forum thread.

Need Your Help

UICollectionView cellForItemAtIndexPath not registering cell

objective-c ios xcode ipad uicollectionview

I am trying to use UICollectionViewCell, since all I want to display is an image. I can add the image to the cell using UIColor colorWithImage: on the UICollectionViewCell's contentView property.