Qt Connecting SIGNAL and SLOT in object member of MainWindow - c++

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.

Related

create Class in Qt with QDialog inside for specific Dialogs

I just want to make a custom Dialog, so I want to make a class around the standard QDialog. The goal is to call the constructor which creates the Dialog, and the show() function should be called to make it shown. Next step would be to make a connect between my Widget (which calls the Dialog constructor) Pushbutton and the show() function.
My header looks like this:
#include <QDialog>
class Dialog_Setting : public QDialog
{
Q_OBJECT
public:
Dialog_Setting();
public slots:
void show(void);
private:
QDialog * dialog;
};
my .cpp:
#include "Dialog_Setting.h"
Dialog_Setting::Dialog_Setting()
{
dialog = new QDialog;
}
void Dialog_Setting::show()
{
dialog->show();
}
I have taken out my connect and get a new error.
What is wrong with my class?
undefined reference to `vtable for Dialog_Setting'
thanks for your help, I love StackOverflow
Make sure that show() is implemented as a slot so you can connect() stuff to it:
#include <QDialog>
class Dialog_Setting : public QDialog
{
Q_OBJECT
public:
Dialog_Setting();
public slots:
void show();
};
You also forgot to inherit from QObject or some other QObject-based class like QDialog and to declare the macro Q_OBJECT. All of these things are required to make your custom classes communicate with other classes through connect().

QT c++ Ui::HelloQtClass ui

Ui::HelloQtClass ui;
What is the meaning of this line in the following code of QT c++ application:
class HelloQt : public QMainWindow
{
Q_OBJECT
public:
HelloQt(QWidget *parent = 0);
~HelloQt();
private:
Ui::HelloQtClass ui;
qint32 port;
private slots:
void exit();
void OnClick();
};
#endif // HELLOQT_H
Ui::HelloQtClass ui; declares the member variable ui of type Ui::HelloQtClass. HelloQtClass is a class in the namespace Ui. This class will be defined in ui_helloqt.h, which is created only after you build your project. (To quickly access the file, select Ui::HelloQtClass and press F2, if you're using QtCreator.) This class is generated from helloqt.ui, which is generated from QtCreator's Designer, if you're using that.
The ui object declares a User Interface for this class, which, because it's derived from QMainWindow, is probably what defines what the application's main window looks like, assuming you're following general Qt standards.

Qt - No matching function for connect?

I'm trying to connect a signal and a slot. I had it working, but I accidentally deleted a .h file. Now I tried to rewrite it, and everything's gone to hell. I've got:
#ifndef GAMEMANAGER_H
#define GAMEMANAGER_H
#include "gamepersistence.h"
class GameManager
{
Q_OBJECT
public:
GameManager();
~GameManager();
GamePersistence* _gamePersistece;
// other stuff
signals:
void refreshPlease();
void gameOverSignal();
};
#endif // GAMEMANAGER_H
And then I'm trying to connect it in another class:
GameWindow::GameWindow(QWidget *parent)
: QWidget(parent)
{
setFixedSize(900,200);
setWindowTitle(trUtf8("Amőba"));
//this->setStyleSheet("background-color: white;");
_gameManager = new GameManager();
// _gameManager->setFocusPolicy(Qt::StrongFocus);
connect(_gameManager, SIGNAL(gameOverSignal()), this, SLOT(gameOver()));
connect(_gameManager, SIGNAL(refreshPlease()), this, SLOT(refreshTable()));
//other stuff
}
This is in a class called GameWindow. Now I'm getting errors for the two connect lines:
error: no matching function for call to 'GameWindow::connect(GameManager*&, const char*, GameWindow* const, const char*)'
connect(_gameManager, SIGNAL(gameOverSignal()), this, SLOT(gameOver()));
What did I mess up in the header? I think I've rewritten it as it was...
Figured it out, I have to use the : public QObject base class.
in gamemanager.h add the public inheritance from QObject for signal and slot can be called.
class GameManager : public QObject{ //your class definition };

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.

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.