create signals in a custom QSlider class - c++

I'm new in Qt and I'm trying to create a custom QSlider class (it inherits from QSlider).
This works perfectly but when I try to create new signals and slots, it doesn'ts work. Actually, I think it's the Q_OBJECT macro which doesn't work... Indeed, I get this message from the compiler :
erreur : 1 duplicate symbol for architecture x86_64
erreur : linker command failed with exit code 1 (use -v to see invocation)
If I remove Q_OBJECT it doesn't work either as the compilers tells me :
erreur : Error: Class declaration lacks Q_OBJECT macro.
Finally, if I make my class inherit from QWidget, all is working even the Q_OBJECT macro...
Here is my .h code :
#include <QWidget>
#include <QSlider>
class mySlider : public QSlider
{
Q_OBJECT
public:
explicit mySlider();
signals:
void test();
public slots:
void nTest();
};
Here is my .cpp code (there isn't much things, I'm only trying to make this simple code work for now) :
#include "myslider.h"
mySlider::mySlider()
{
}
void mySlider::test(){
}
void mySlider::nTest() {
}

Related

QT connect no matching function to call to

Hey guys I know there are already some threads for this question but I think I made none of the mistakes others did which led to the problem. So here is my code:
#include "consolerender.h"
consoleRender::consoleRender(QObject *parent) :
QObject(parent) {
connect(Enviroment::instance, &Enviroment::enviromentChanged,
this, &consoleRender::renderField);
}
And the header:
class consoleRender : public QObject
{
Q_OBJECT
public:
explicit consoleRender(QObject *parent = 0);
public slots:
void renderField();
};
And the Enviroment.h
class Enviroment : public QObject
{
Q_OBJECT
public:
static Enviroment& instance();
virtual ~Enviroment();
//stuff...
signals:
void enviromentChanged();
I already tried to do the connect in a separate class, I tried to use the old connect syntax (SIGNAL/SLOT(function)) and tried it with >>all<< my classes inheriting from QObject but it showed the same error. Also it says something that the function expects 3 arguments but gets 4. and seems to point at the connect(...renderField). I heard of a solution to just do all of that in the MainWindow class but that is not an option for me.
You have to pass the instance pointer:
connect(&Enviroment::instance(), &Enviroment::enviromentChanged,
this, &consoleRender::renderField);

QObject signals and slots not accessing function

I have a function which I am trying to connect via signals and slots but the function isn't being called when I run the executable file. I have put a debug error in my function so I know it isn't being called. My code is shown below:
My header file:
#include "MyWidget.h"
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass: public QObject{
Q_OBJECT
public:
MyClass( QObject *parent = nullptr);
MyWidget *myWidget;
public slots:
void setTranslation(int value);
};
#endif
My source file:
#include "MyClass.h"
MyClass::MyClass(QObject *parent): QObject(parent)
{
some code here....
QSlider *xSlider = new QSlider(Qt::Vertical);
xSlider->setRange(0, 10);
QObject::connect(xSlider, SIGNAL(valueChanged(int)),
this, SLOT(setTranslation(int)));
some code here...
}
void MyClass::setTranslation(int value)
{
some code here...
}
You forgot to add the Q_OBJECT macro immediately after the class declaration. It is what the MOC (Meta Object Compiler) uses to generate additional meta information for your code.
class MyClass: public QObject
{
Q_OBJECT
// all other class data, member function declaration
public:
MyClass( QObject *parent = nullptr );
};
This should be good enough.
EDIT:
Also remember to take a nullptr defaulted QObject pointer in your default constructor.

error: 'QObject' is an ambiguous base of 'SerialPort' QObject::connect(&uartObj, SIGNAL(readDone(QByteArray)), this, SLOT(hdlRxDone(QByteArray)));

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.

Q_OBJECT generates many errors

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.

Strange situation with QThread QT3

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
...
};