Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Inheritance of QSerialPort question
Forum Updated to NodeBB v4.3 + New Features

[solved] Inheritance of QSerialPort question

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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()

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted28
        wrote on last edited by
        #3

        @#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 ?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deleted28
            wrote on last edited by
            #5

            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 :)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deleted28
                wrote on last edited by
                #7

                @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 ?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved