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 };
Related
I've been following a simple QT tutorial and came up with a weird noob question.
https://www.youtube.com/watch?v=F56fSKoNCtk&list=PLS1QulWo1RIZiBcTr5urECberTITj7gjA&index=5
this is the tutorial.
Here's the my_window.h
#ifndef MY_WINDOW_H
#define MY_WINDOW_H
#include <QMainWindow>
namespace Ui {
class MyWindow;
}
class MyWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MyWindow(QWidget *parent = nullptr);
~MyWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::MyWindow *ui;
};
#endif // MY_WINDOW_H
and my_window.cpp.
#include "my_window.h"
#include "ui_mywindow.h"
MyWindow::MyWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyWindow)
{
ui->setupUi(this);
connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),
ui->progressBar,SLOT(setValue(int)));
}
MyWindow::~MyWindow()
{
delete ui;
}
void MyWindow::on_pushButton_clicked()
{
ui->label->setText("Clicked!");
}
void MyWindow::on_pushButton_2_clicked()
{
ui->label->setText("Why...?");
}
In my_window.cpp, the definition of the constructor,
it does the tasks via the member pointer, 'ui' which is a pointer of 'MyWindow type' instance.
I thought 'this' pointer is also a 'MyWindow' class type pointer so i thought i can access the horizontalSlider and progressBar with this pointer too.
But i couldn't.
what makes the difference between two of them?
Your class MyWindow that you define in my_window.h and implement in my_window.cpp is ::MyWindow. Then you have ::Ui::MyWindow which is a totally separate class, and is defined and implemented in auto-generated files by Qt and the Qt tools.
You can have many symbols with the same name, as long as they are defined in different scopes. These symbols can even be different types, like one could be a class, one could be a variable, one could be a function, etc.
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);
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().
I'm trying to use Qt with C++. I'd use QT for programming in Python before.
my simple test doesn't work. This is my tour.h file:
#ifndef TOUR_H
#define TOUR_H
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QTableView>
class TourTable;
class Tour : public QMainWindow
{
Q_OBJECT
public:
Tour();
/*protected:
void closeEvent(QCloseEvent *event);
private slots:
void newCompetitor();
void remCompetitor();
void finalReport();
void openPage();
void savePage();
void reloadTitle();*/
private:
TourTable _table;
};
class QStandardItem;
class QStandardItemModel;
class TourTable : public QTableView
{
Q_OBJECT
public:
TourTable();
/* bool isChanged();
QString windowName();
void finalReport();
void newCompetitor();
void remCompetitor();
bool savePage();
void setChanged(bool value);
void openPage();
protected:
void itemChanged(QStandardItem item);
private:*/
// bool _secondBetter(p1, p2);
Tour _parent;
// QStandardItemModel _model;
// bool _saved;
// bool _changed;
};
#endif
I'd commented almost everything in this code to isolate the problem, but I still don't know what is causing this. Is my first trying in C++.
The error message is:
tour.h:28:12: error: field ‘_table’ has incomplete type ‘TourTable’
TourTable _table;
^~~~~~
tour.h:7:7: note: forward declaration of ‘class TourTable’
class TourTable;
Can someone help me to solve that?
Forward declaration in C++ allows to reference a type before its definition. Problem here is that in your code:
class TourTable;
class Tour : public QMainWindow
{
// ...
TourTable _table;
};
You're not only referencing the type TourTable, you're instantiating it with TourTable _table;. This requires a complete definition of TourTable.
A solution could be to define TourTable before Tour, as follows:
class TourTable : public QTableView
{
// ...
Tour _parent;
}
class Tour : public QMainWindow
{
// ...
TourTable _table;
};
But that just moves the problem since you're instantiating Tour in TourTable.
Depending on the complete design, the solution may be to use pointers. Something like that:
class TourTable;
class Tour : public QMainWindow
{
Tour();
// forward declaration of the constructor,
// see below for the definition
TourTable* _table;
// no complete definition of TourTable at this stage,
// only a forward declaration:
// we can only declare a pointer
};
class TourTable : public QTableView
{
TourTable(Tour* parent):
QTableView(parent),
_parent(parent)
{
}
Tour* _parent;
}
Tour::Tour() // definition of Tour's constructor
{
_table = new TourTable(this);
// TourTable has been defined : we can instantiate it here
}
To be clear, this error is coming from your compiler -- not from qmake -- and is a limitation of the language. But it can be easily overcome by creating a class that defines a parent/child behavior and making any classes where you want that behavior to inherit from the base class, as Qt's QObject does.
Both QMainWindow and QTableView inherit from QObject, so if you make use of QObject's parent/child system this design may be redundant. To find the parent from the child call parent(), and to find the children from the parent you can call children().
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.