I have 2 classes:
class Server : public QTcpServer
{
Q_OBJECT
public:
Server(QObject * parent = 0 , quint16 port = 1922);
void SendData(QString data);
virtual ~Server();
signals:
void RecieveMessage(QString);
private slots:
void acceptConnection();
void startRead();
void disconnected();
private:
QTcpServer *tcpServer;
QTcpSocket *client;
};
and
class ChessLanTEst : public QMainWindow
{
Q_OBJECT
public:
friend class Server;
friend class Client;
ChessLanTEst(QWidget *parent = 0);
~ChessLanTEst();
private:
Ui::ChessLanTEstClass ui;
Server *server_;
Client *client_;
private slots:
void createGame();
void ShowMessage(QString);
};
in Server class I have signal:
void RecieveMessage(QString);
in ChessLanTEst class I have slot:
void ShowMessage(QString);
and I connected it in the ChessLanTEst constructor:
connect(server_, &Server::RecieveMessage, this, &ChessLanTEst::ShowMessage);
but I don't receive this signal and I don't know why.
Based on comments, you emit in constructor of server_, before you do connect (because constructor has returned if you have valid value in server_ pointer).
Emits themselves are not queued ever, connect must have been done before emit for the slot to be called (or the call queued).
You need to use the SIGNAL and SLOT macros. Like:
connect(server_, SIGNAL(RecieveMessage(QString)), this, SLOT(ShowMessage(QString)));
If you search for the definitions of these macro's, you'll find that the arguments are actually converted to strings.
Related
class TDF : public QMainWindow
{
Q_OBJECT
public:
explicit TDF(QWidget *parent = nullptr);
~TDF();
private slots:
void on_umwelt_clicked();
void on_regional_clicked();
void on_pushButton_clicked();
void on_autofahrt_clicked();
void on_flugzeug_clicked();
void on_bestell_clicked();
int anzahl=3;
private:
Ui::TDF *ui;
};
#endif // TDF_H
This is my header. I tried starting the application, but it keeps saying Error: Not a signal or slot declaration.
Please help me :(
Solved the Problem. Int anzahl is not a slot but was in private slots as Seen above
I'm asking if there is a way to use signals in a class which inherits from QObject like this:
mysuperclass.cpp
#include "mysuperclass.h"
MySuperclass::MySuperclass(quint16 port, QObject *parent) :
QObject(parent), port(port)
{
this->connected = false;
}
mysuperclass.h
#include <QAbstractSocket>
class MySuperclass: public QObject
{
Q_OBJECT
public:
explicit MySuperclass(quint16 port = 0, QObject *parent = 0);
signals:
//there is nothing here
public slots:
virtual void newValue(){qDebug() << "newValue";}
virtual void connectionEstablished(){qDebug() << "connectionEstablished";}
virtual void disconnected(){qDebug() << "disconnected";}
protected:
QAbstractSocket* networkSocket;
quint16 port;
bool connected;
};
mysubclass.cpp
#include <QTcpSocket>
#include <QHostAddress>
MySubClass::MySubClass(quint16 ServerPort, QObject *parent) :
MySuperClass(ServerPort, parent)
{
this->networkSocket = new QTcpSocket(this);
...
connect(this->networkSocket, SIGNAL(connected()),this,
SLOT(connectionEstablished()));
connect(this->networkSocket, SIGNAL(disconnected()),this,
SLOT(disconnected()));
connect(this->networkSocket, SIGNAL(readyRead()),this, SLOT(newValue()));
}
mysubclass.h
#include <QObject>
#include "mysuperclass.h"
class MySubClass: public MySuperClass
{
public:
MySubClass(quint16 ServerPort, QObject* parent=0);
public slots:
void newValue();
void connectionEstablished();
void disconnected();
};
You must include the Q_OBJECT macro in the derived class too (but don't derive from QObject again). The macro is only mandatory if the derived class declares signals or slots. For emitting parent's signals or connecting with parent's slots it is not necessary (it also means that it is not necessary to re-define already existing signals or slots).
From Qt's documentation:
The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.
Example
class MySubClass : public MySuperClass {
Q_OBJECT
public:
MySubClass(quint16 ServerPort, QObject* parent=0);
public slots:
void newValue();
void connectionEstablished();
void disconnected();
};
On the other hand, if you want to connect to a slot in the parent class but implement it in a derived one, then you must make it virtual:
class MySuperclass : public QObject {
Q_OBJECT
// ...
public slots:
virtual void newValue(); // can be virtual pure also
};
class MySubClass : public MySuperClass {
public:
virtual void newValue() override; // overrides parent's
}
Note that there is no need to use the Q_OBJECT macro nor to use the slot: label in the derived class. Slots are normal methods after all. Of course, you have to use it if you add new slots or signals.
I have class printrectangle
class PrintRectangle : public QWidget
{
Q_OBJECT
public:
explicit PrintRectangle(QWidget *parent = 0);
private:
void resetClickedIndex();
void updateIndexFromPoint( const QPoint& point);
public:
int mXIndex;
int mYIndex;
QVector<QPoint> points;
bool clicked[5][5] = {};
teacher tech;
perceptron p[5][5];
double techconst = 0.1;
signals:
public slots:
protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *eventPress);
};
and MainWindow
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_learn_clicked();
void on_classify_clicked();
private:
Ui::MainWindow *ui;
};
When I click button I call to on_learn_clicked() function. I would like to transfer clicked[5][5] array into on_learn_clicked becasue I send this array to other object when user click button. How to do this?
It is not clear what is exactly the relation between MainWindow and the PrintRectangle widget. I suppose the button signal and PrintRectangle slot are connected somewhere in the MainWindow implementation.
One way to solve the problem would be to use to use the QSignalMapper as #Noidea stated.
Another way would be to use a lambda as a slot when connecting. This way you could capture the sender/receiver or other objects in scope and use their members.
You can find some information about the connect syntax in New Signal Slot Syntax
But basically you could write something like:
connect(button, &QPushButton::clicked, this, [this, printRectangle]()
{
// do smth with clicked[5][5] from printRectangle or just
// retrieve it and call another method like:
// this->processClicked(printRectangle->clicked);
// or pass it to another object
}
This way you could modify your on_classify_clicked slot to a regular method with bool[5][5] argument to do the processing.
I'm not able to receive my custom signal in the supposed SLOT. Here is my code:
mainwindow.h:
class HistoryItem {
public:
QString channel;
};
class dbThread : public QObject
{
Q_OBJECT
public:
dbThread();
signals:
void historyLoaded(QList<HistoryItem*> innerResult);
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void historyLoaded(const QList<HistoryItem*> innerResult);
mainwindow.cpp:
connect(dbtrad, SIGNAL(historyLoaded(QList<HistoryItem*>*)), this, SLOT(historyLoaded(QList<HistoryItem*>*)));
void MainWindow::historyLoaded(QList<HistoryItem*> innerResult) {
qDebug() << "historyLoaded()...";
}
And this is how I emit the signal:
QList<HistoryItem*> innerResult;
while (queryInner.next()) {
QString channelIDInner = queryInner.value(0).toString();
HistoryItem* item = new HistoryItem();
item->channel = channelIDInner;
innerResult.append(item);
}
qDebug() << "DONE LOADING.....";
emit historyLoaded(innerResult);
However, qDebug() << "historyLoaded()..."; is never executed.
Any ideas what the problem could be?
It seems you're using threads. Using QList when signaling across threads (or using Qt::QueuedConnection in general) requires some extra work. Basically you need to define the QList<T> type using typedef and then register it using qRegisterMetaType:
typedef QList<HistoryItem*> HistoryList_t;
...
qRegisterMetaType<HistoryList_t>("HistoryList_t");
Then use this type in your signals and slots:
public slots:
void historyLoaded(const HistoryList_t &list);
Check return value of your connect, it should fail. There is an extra * in SIGNAL(historyLoaded(QList<HistoryItem*>*)), should be SIGNAL(historyLoaded(QList<HistoryItem*>)). Fix your SLOT() too.
I am a newbie in Qt and C++ programming. I have some problem in my program that i need a solution for.
I have two files MainWindow.h and ChatWindow.h, which contains two classes of MainWindow and ChatWindow.
This is chatwindow.h
namespace Ui {
class ChatWindow;
}
class ChatWindow : public QMainWindow
{
Q_OBJECT
public:
explicit ChatWindow(QWidget *parent=0);
~ChatWindow();
private slots:
void send_chat_fn(pjsua_call_id call_id);
void rcv_chat_fn(pjsua_call_id call_id);
void rcv_msg_fn(QString msg);
void on_pushButton_clicked();
void on_actionQuit_triggered();
private:
Ui::ChatWindow *ui;
};
And this is mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void add_item(QString buddy_uri);
signals:
void send_chat(pjsua_call_id call_id);
void rcv_chat(pjsua_call_id call_id);
void rcv_msg(QString msg);
private slots:
void on_actionAdd_Buddy_triggered();
void on_actionQuit_triggered();
void on_actionStatus_triggered();
void on_actionNew_Chat_triggered();
void on_action_Configuration_triggered();
void on_listWidget_doubleClicked(const QModelIndex &index);
private:
Ui::MainWindow *ui;
};
Now i want to connect signals from mainwindow.h to slots in chatwindow.h.
I have tried connection in the constructor of class ChatWindow, but it does not work (i think that is because connections work on instances not on classes). Instance of MainWindow class which i want to connect is in mainwindow.cpp. Defining instances of class ChatWindow in MainWindow gives error:
Cannot set parent, parent is in different thread
And if i create a new instance in Constructor of ChatWindow, then it doesnt connect to the desired instance.
Its a complete mess. Please help me through this.