0

I have a class which derived QObject.

class SerialPortWidgetBackend : public QObject
{
Q_OBJECT

public:
SerialPortWidgetBackend();
QSerialPort *sp;
Q_INVOKABLE QStringList refreshPorts(void);
Q_INVOKABLE bool openPort(QString portName,QString baudRate);
Q_INVOKABLE bool closePort();
};

In QML file define object like this.

SerialPortWidgetBackend{
    id: backend
}

The QML file actually implements for search availible comports , cofigure it and open it. This works fine but i want access sp object. The qml registeration is below

qmlRegisterType<SerialPortWidgetBackend>("com.company.serialportwidgetbackend",1,0,"SerialPortWidgetBackend");

The registered object private. How can access it?

3
  • Does QML really need to know about the serial port? Commented Dec 11, 2020 at 13:35
  • No it dont need. But i want open a comport. The SerialPortWidgetBackend class will used for another works. Commented Dec 11, 2020 at 13:51
  • Exactly! You do not need to access QSerialPort from QML. Commented Dec 11, 2020 at 15:14

1 Answer 1

2

I think it's a bad practice to declare public attributes within your class. You can instead define properties with getters/setters which can be accessible from your qml file with the Q_PROPERTY Macro.

Please follow this link for more information: https://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html

Sign up to request clarification or add additional context in comments.

5 Comments

I'd suggest to add an example of the question with suggested Q_PROPERTY
how will this answer help solve my problem?
Can you please read the documentation (i shared with you a link with all the explanation you need), i don't have it in mind..
Thank you for your answer even if this doesn't work for me
It does not make sense to make a serial port property of a class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.