Search code examples
qtqmlqtquick2qtserialport

How do I register QSerialPort in QML?


I would like to register QSerialPort as a non-instantiable type in QML so that I can use QSerialPort's ENUMs.

I tried to register QSerialPort in my main.cpp:

 qmlRegisterType<QSerialPort>("com.myProject", 1, 0, "serialPort")

I get the following error:

/Users/user/qt/5.4/clang_64/lib/QtQml.framework/Headers/qqml.h:234: error: incomplete type 'QSerialPort' named in nested name specifier QML_GETTYPENAMES ^~~~~~~~~~~~~~~~

Any hints appreciated! Thanks.


Solution

  • Include #include <QtSerialPort/QSerialPort> and don't forget to add serialport at .pro file:

    QT += serialport
    

    Works fine for me.