I creati a simple class which extends two classes QObject and QThread.
When I compile it with MOC compiler there is an error:
expected class-name before ‘{’ token
Class started with this code:
class QSmartecVideoAudio : public QObject, public QThread
{
Q_OBJECT
...
};
I implement run method but it doesnot work.
I include qthread.h at the beginning.
Looks like an include issue. Try to add:
#include <QThread>
#include <QObject>
before your class definition.
QThread inherits QObject already, so you must not inherit from QObject.
try:
class QSmartecVideoAudio : public QThread
{
Q_OBJECT
...
};
Related
I want to create SerialPort class,
and it can receive msg automatic and then emit a signal.
but when I compile it show the error message:
error: 'QObject' is an ambiguous base of 'SerialPort'
QObject::connect(&uartObj, SIGNAL(readDone(QByteArray)), this, SLOT(hdlRxDone(QByteArray)));
have someone can help me to solve it ?
thanks.
#ifndef SERIALPORT_H
#define SERIALPORT_H
#include <QObject>
#include <QSerialPort>
#include <QThread>
class SerialPort : public QSerialPort, public QThread
{
Q_OBJECT
public:
explicit SerialPort(QObject *parent = 0);
~SerialPort();
void stop();
signals:
void readDone(QByteArray data);
public slots:
private:
void run();
};
#endif // SERIALPORT_H
Both QSerialPort and QThread derived from QObject, so in your code you have multiple inheritance from QObject, which is forbidden in Qt. You should derive only from QThread, but it is not very good way. The best way is to create simple Worker class derived from QObject, which HAS QSerialPort, and move this class to some thread with moveToThread().
You can find more information about correct usage here or in the documentation.
Full example about serial port in separate thread you can find here, it is in russian language, but you need only code which is not very complex.
I have a class MyClass with:
- private:
pushButton *button;
void connectSignalAndSlot();
- private slot:
void buttonAction();
I want to connect these in MyClass using connectSignalAndSlot(), like so:
void MyClass::connectSignalAndSlot()
{
QObject::connect(button,SIGNAL(clicked()),this,SLOT(buttonAction()));
}
This gives me an error of
no matching function for call to 'QObject::connect(QPushButton*&, const char*, MyClass* const, const char*)';
If I inherit QObject with MyClass, the program compiles and starts, but then I get the following issues displayed in my Application Output pane:
QObject::connect: No such slot QObject::buttonAction() in ..\MyProject\myclass.cpp:48
Do I have to make the button and slot public and use them in the MainWindow class only? Is there no way to keep this at the MyClass level?
Thanks for your help!
You must have MyClass inherit from QObject AND add Q_OBJECT macro in your MyClass definition (header file) to have slots/signals work.
class MyClass : public QObject
{
Q_OBJECT
public:
....
};
Inheriting QObject is the right way, but your still missing Qt-Meta Object Code. Your header-file for your class should look like this:
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass : public QObject {
Q_OBJECT
// your methods, variables, slots and signals
}
#endif
Don't forget to create the moc file, the easiest way is to use qmake or the QtCreator IDE.
I'm trying to use QTimer, which inherits QObject, in my newly created class. However I try it I keep getting the error 'QObject' is an ambiguous base of 'Recorder' . I did try my best to avoid ambiguity in my simple program but still got stuck with it.
Here's the structure of my classes.
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
dialog.h: mainwindow UI
#ifndef DIALOG_H
#define DIALOG_H
#include "detector.h"
#include <QDialog>
#include <QtCore>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
Detector myDetector;
detector.h: detector window UI
#ifndef DETECTOR_H
#define DETECTOR_H
#include <QDialog>
#include <QtCore>
#include <QObject>
#include "actualrec.h"
namespace Ui {
class Detector;
}
class Detector : public QDialog
{
Q_OBJECT
public:
explicit Detector(QWidget *parent = 0);
~Detector();
void run();
private:
ActualRec theDetector;
Ui::Detector *ui;
actualrec.h: detector code
#ifndef ACTUALREC_H
#define ACTUALREC_H
#include <QtCore>
#include <QObject>
#include <QImage>
#include "recorder.h"
class ActualRec : public QThread
{
public:
ActualRec();
void run();
private:
Recorder theRecorder;
recorder.h: recorder code, where I want to use my QTimer
#ifndef RECORDER_H
#define RECORDER_H
#include <QtCore>
class Recorder : public QThread, public QObject
{
public:
Recorder();
void run();
private:
QTimer* theTimer;
recorder.cpp constructor has
*theTimer = new QTimer(this);
the output is following:
http://i.imgur.com/Awb6qhd.png
Any help would be much appreciated
You have several issues in your code:
1) Wrong usage of thread with Qt
class Recorder : public QThread, public QObject
a) It is enough to inherit QThread without explicitly inheriting QObject since QThread inherits QObject.
b) Even if you did this, historically, QObject ought to be the first base in the list in a general case.
c) However, you may wish to reconsider how to use your threads. This is one way, but necessarily the best.
2) Allocating an object for QTimer on the heap
Why are you allocating memory on the heap for a timer in the first place? It is OK to allocate it on the stack, especially since it is a member. That way, you would not need to deal with the this hassle either. The whole memory management becomes a lot simpler.
3) Not utilizing Q_NULLPTR
You ought to use it instead of 0 for the default values of the parents.
4) Including the whole QtCore module
#include <QtCore>
You should only include the parts that you eventually use. This is a brute-force way of including things.
Therefore, write something like this instead:
class Recorder : public QThread
{
public:
Recorder();
void run();
private:
QTimer theTimer;
Of course, if you use the threading mechanism the other way around in Qt, then it is perfectly fine to write this instead for the inheritance:
class Recorder : public QObject
but then your code would need some other change, so the code is broken as it is now, either way.
QThread already inherits QObject, and you can't inherit from two classes both inheriting QObject themselves.
You shall no inherit QObject twice. This is because signals and slots are mapped by integers and the ids can collide with each.
This also applies to any object that inherits from QObject.
class BadClass : public QTimer, public Dialog
{
};
This class no problem:
#include <QThread>
class LiveImageItem : public QThread
{
Q_OBJECT
public:
LiveImageItem(QPixmap pimg);
signals:
public slots:
};
BUT this class get problem associated with "Q_OBJECT" macro defined in header file
#include <QGraphicsPixmapItem>
class LiveImageItem : public QGraphicsPixmapItem
{
Q_OBJECT //this line will generate many errors in compiling
public:
LiveImageItem(QPixmap pimg);
signals:
public slots:
};
both their cpp file is the same:
#include "LiveImageItem.h"
LiveImageItem::LiveImageItem(QPixmap pimg)
{
}
I thought every QT object essentially inherited from QObject so if I inherit any of the subclass of QObject, I could have all the magics QObject offers. The 2nd version of the above (which is inherited from, say, QGraphicsPixmapItem) seems proved I was wrong. It turns out to be having lots of errors while compiling, all from moc files(automatically generated by QT). What happens?
Some of these errors are:
[qobject.h] error: 'QScopedPointer QObject::d_ptr' is
protected
[moc_LiveImageItem.cpp] error: within this context
...
According to the documentation QGraphicsPixmapItem is not a QObject, thus you cannot treat it as if it is. I would try to extend your class inheritance and do:
class LiveImageItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT //this line will generate many errors in compiling
[..]
As #vahancho said, QGraphicsPixmapItem is not a QObject. In fact, that can be said of most of the QGraphics*Item classes.
However, if you want to use signals and slots with QGraphicsSystem classes, you can inherit from QGraphicsObject: -
class LiveImageItem : public QGraphicsObject
{
Q_OBJECT
public:
private:
QPixmap m_pixmap;
};
You would then override the paint function in this class and draw the pixmap from there.
Here is my sample code:
class hoho : public QObject
{
Q_OBJECT
public:
hoho()
{
httpFetch = new HttpFetch(QUrl("http://www.google.com/"));
connect(httpFetch, SIGNAL(Fetched()), this, SLOT(PrintData(QByteArray)));
}
void PrintData(QByteArray http)
{
qDebug()<<http;
}
HttpFetch *httpFetch;
};
When I try to compile this, following error pops up
1>main.cpp(15): error C2243: 'type cast' : conversion from 'HttpFetch *' to 'const QObject *' exists, but is inaccessible
This error comes as the class is derived from QObject (which is necessary for signal and slot mechanism).
Can anyone tell me how to fix this?
You probably did not derive HttpFetch publicly, but privately from QObject. So just change
class HttpFetch : QObject { // ...
to
class HttpFetch : public QObject { // ...
and it should work.
If your design requires to make the inheritance non-public (I had this requirement because I inherited from a QWidget for a multithreading purpose and didn't want to expose all functions to the user), you can do this:
class FilesQueueQList : protected QWidget
{
Q_OBJECT
public:
using QWidget::QObject; //This is the solution!
//...
}
Now the members of QWidget are private/protected, but QObject is accessible as public.
Did you forget the Q_OBJECT macro in your class HttpFetch ?
Can I see your class HttpFetch ?