[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();
}
@theball.h
@#include <QObject>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>class TheBall : public QSerialPort
{
Q_OBJECTpublic:
explicit TheBall(QObject *parent);~TheBall();
};@
theball.cpp
@#include "theball.h"TheBall::TheBall(QObject *parent)
: QSerialPort(parent)
{}
TheBall::~TheBall()
{}@
project.pro
@QT += core
QT += serialport
QT -= guiTARGET = BallTest_q001
CONFIG += console
CONFIG -= app_bundleTEMPLATE = app
SOURCES += main.cpp
theball.cppHEADERS +=
theball.h@rhx wally
-
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();
}
@@.../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 ? -
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.prothis gives errors:
@QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgets serialport@
rhis is OK :
@QT += core gui
QT += serialportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgets@
needless to say, i do not know why :)
-
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.
-
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 ? -
Sure you can, generally you would post the relevant piece of code