0

I want to add some functionality to the serialport class and therefore want to inherit it and add som functions. However, I got problems. I put the class in a header file like this:

class mySerialport : public QSerialPort
{

    public:

    void mySerialport(): QSerialPort(QObject*)
    {


    }


};

I'm modifying the Terminal example: http://qt-project.org/doc/qt-5.1/qtserialport/terminal-mainwindow-cpp.html

Here a serialport object is created in the MainWindow constructor by

serial = new QSerialPort(this);

However, after declaring mySerialport and trying

serial = new MySerialport(this); 

I get nothing but a myriad of error messages regarding the constructor.

Questions:
1. What could the error be? I guess it's basic. 2. Why is the serialport ineheriting the MainWindow? Is it the Qt thing that the serialport than will be deleted when the MainWindow destructor is called?

László Papp
  • 51,870
  • 39
  • 111
  • 135
user3050215
  • 185
  • 1
  • 5
  • 14
  • It might be more productive to learn some C++ first. See [this list of good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Jan 06 '14 at 16:41
  • 1
    "I get nothing but a myriad of error messages regarding the constructor." - telling us what the errors actually are (or at least the first few) will give us a chance of telling you where your problem actually lies. – benjymous Jan 06 '14 at 16:45
  • "Why is the serialport ineheriting the MainWindow?" Do you mean "Why is `this` being provided as a parameter to the serialport constructor?" That's not inheritance - it's just passing a pointer to the object (i.e. the parent) which is a common thing to do in OO – benjymous Jan 06 '14 at 16:46
  • The errors were so many that they didn't seem to provide much useful information other than that there was some kind of problem with the constructor. Btw, I got the feeling that minGw give errors that aren't very informative. At least AVR-GCC usually provides alot better information. Regarding the inheritance, typo from me. – user3050215 Jan 06 '14 at 17:11
  • Is this still unresolved one year later?? – László Papp Dec 21 '14 at 10:53

2 Answers2

2
  1. What could the error be? I guess it's basic.

Replace this line:

void mySerialport(): QSerialPort(QObject*)

with this:

explicit mySerialport(QObject *parent): QSerialPort(parent)

You could also consider composition instead of inheritance based on your exact use case. You would also need to have the Q_OBJECT macro, as well as the source and header files added in your qmake project file.

You would be writing something like this:

myserialport.h

class MySerialport : public QSerialPort
{
    Q_OBJECT
    public:
        explicit MySerialPort(QObject *parent);
        ~MySerialPort();
    ...
};

myserialport.cpp

...
MySerialPort::MySerialPort(QObject *parent)
    : QSerialPort(parent)
{
    ...
}

MySerialPort::~MySerialPort()
{
    ...
}

...

main.pro

...

HEADERS += \
    myserialport.h \
    ...

SOURCES += \
    myserialport.cpp \
    ...

...

You would also need to have the Q_OBJECT macro, as well as the source and header files added in your qmake project file.

  1. Why is the serialport ineheriting the MainWindow?

Our example does not seem to have constructed that way.

Also, do not take the terminal as a good example. I am currently working on a QML terminal example which will be cleaner.

I expect it to be pushed soon against gerrit, and I will share the url later in this post when that is ready.

Is it the Qt thing that the serialport than will be deleted when the MainWindow destructor is called?

No, in fact, QtSerialPort is a core functionality, or you could say "headless". I have written several command line based examples, like sync and async reader and writers. You can check it out in the examples folder of the project.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Hm that looks like something that would work. However it gives me the error "undefined reference to vtable for 'mySerialport'". And the constructor cannot be of void type neither right? C++ is a mess :) – user3050215 Jan 06 '14 at 17:07
  • @user3050215: the answer explains that, but I will no go much further since you would need to learn a bit more basic C++ first as others pointed out, already. – László Papp Jan 06 '14 at 17:13
  • So you wrote those, cool! I'm going to check out those examples. Do you have something with an implemented protocol for interfacing for instance an arduino? The problem regarding the "vtable error" seemed to be solved by running clean and rebuild. Thank you! – user3050215 Jan 06 '14 at 17:43
  • I think you are asking for a bit too much in this Q/A. Also, protocol depends on you, not me. :-) – László Papp Jan 06 '14 at 17:47
  • I'm not asking for much, but thats what I got ;) Was just thinking that you maybe had a link to a project regarding UART laying around. Thanks for thy help! – user3050215 Jan 06 '14 at 22:42
  • @user3050215: please select an answer if it solves your issue. – László Papp Jan 07 '14 at 02:06
  • @user3050215: why did you stop replying to people who are trying to help? – László Papp Jan 08 '14 at 07:05
0
  1. You should definitely learn the C++ basics like pointers, references, inheritance. You should pass the parent QObject to the mySerialport constructor and redirect it to the QSerialPort constructor.
  2. The serialport is not inheriting the MainWindow, it's just a "Qt way" child of the MainWindow. Qt parent-child relationships have nothing to do with inheritance. Yes, it seems you got it: serial will be deleted with its parent — MainWindow.
Dmitry Markin
  • 1,145
  • 8
  • 8