[solved] Inheritance of QSerialPort question



  • Hello,
    i want to make a customclass inheriting QSerialPort and have problems.
    i get error messages when build:
    @.../BallTest_q001/main.cpp:19: error: invalid use of 'this' in non-member function
    TheBall ball = new TheBall(this);
    ^@

    and with line 9
    @ TheBall ball = new TheBall();@
    @...l/BallTest_q001/main.cpp:18: error: no matching function for call to 'TheBall::TheBall()'
    TheBall ball = new TheBall();
    ^@

    What is the problem and how to solve ?
    main.cpp
    @#include <QCoreApplication>
    #include <QObject>
    #include <theball.h>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    TheBall ball = new TheBall(this);
    
    return a.exec&#40;&#41;;
    

    }
    @

    theball.h
    @#include <QObject>
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>

    class TheBall : public QSerialPort
    {
    Q_OBJECT

    public:
    explicit TheBall(QObject *parent);

    ~TheBall(&#41;;
    

    };@

    theball.cpp
    @#include "theball.h"

    TheBall::TheBall(QObject *parent)
    : QSerialPort(parent)
    {

    }

    TheBall::~TheBall()
    {

    }@
    project.pro
    @QT += core
    QT += serialport
    QT -= gui

    TARGET = BallTest_q001

    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp
    theball.cpp

    HEADERS +=
    theball.h@

    rhx wally


  • Lifetime Qt Champion

    Hi,

    There's no "this" in main.

    main is purely a function and "this" represent the current object you're into. Also note that you should pass a QObject as parameter and again main is not one.

    @
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    TheBall ball;
    return a.exec();
    }@

    would be enough. If you really want to allocate ball on the heap then you must also delete it before returning the result of a.exec()



  • @#include <QCoreApplication>
    #include <QObject>
    #include <theball.h>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    TheBall ball;
    
    return a.exec&#40;&#41;;
    

    }
    @

    @.../BallTest_q001/main.cpp:18: error: no matching function for call to 'TheBall::TheBall()'
    TheBall ball;
    ^@

    what, when i do a GUI application and construct ball in MainWindow.cpp
    MainWindow is an Object and i can use "this", right ?


  • Lifetime Qt Champion

    Because TheBall's constructor requires a QObject * to be passed, either give it a default value of 0 or give ball the address of a QObject.

    MainWindow is a class, main(int argc, char*argv[]) is a function



  • found reason for problems when using Gui and MainWindow class:
    in BallTest.pro

    this gives errors:
    @QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets serialport@

    rhis is OK :
    @QT += core gui
    QT += serialport

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets@

    needless to say, i do not know why :)


  • Lifetime Qt Champion

    You're using Qt 4 and that check verifies if your are using Qt 5. If it's the case the new widgets module is added (meaningful only if you are doing a widget based application). With Qt 4 the widgets are in the gui module.



  • @SGaist

    i got both (console and Gui) work now. thanks for help!
    Does it make sense to post the working results here or
    this not wanted ?
    If yes, is there a way to post entire project folder ?


  • Lifetime Qt Champion

    Sure you can, generally you would post the relevant piece of code


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.