In this link there is the following code:
class FileDownloader : public QObject
{
Q_OBJECT
public:
explicit FileDownloader(QUrl imageUrl, QObject *parent = 0);
virtual ~FileDownloader();
QByteArray downloadedData() const;
signals:
void downloaded();
private slots:
void fileDownloaded(QNetworkReply* pReply);
private:
QNetworkAccessManager m_WebCtrl;
QByteArray m_DownloadedData;
};
What do the colons in signals: and private slots: mean? Code generally uses this symbol for labels but these don't seem like labels.
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.
How to inherit QLabel and QPushButtton too, what I tried shows error messages
/home/test.cpp:206: error: reference to 'setText' is ambiguous
setText(text);
^
Here is what I tried:
class virtualLabel: virtual public QLabel
{
Q_OBJECT
public:
explicit virtualLabel(const QString& text="", QWidget* parent=0){}
~virtualLabel(){}
};
class virtualPushButton: virtual public QPushButton
{
Q_OBJECT
public:
explicit virtualPushButton(const QString& text="", QWidget* parent=0){}
~virtualPushButton(){}
};
class customLabel : public virtualLabel, public virtualPushButton
{
Q_OBJECT
// Q_DECLARE_INTERFACE
//Q_INTERFACES(YourInterface OtherInterface)
public:
explicit customLabel(const QString& text="", QWidget* parent=0);
~customLabel();
QString folderName;
};
Any help appreciated Thank You
You don't.
Apart from the issue with ambiguity you will have a lot more issues.
From the Qt documentation on moc:
Virtual inheritance with QObject is not supported.
(Trust them on that)
Rather use composition and expose the signals, slots and functions you need.
class customLabel : public QWidget
{
Q_OBJECT
public:
explicit customLabel(const QString& text="", QWidget* parent=0);
~customLabel();
protected:
QPushButton* button;
QLabel* label;
};
I have inherited a class MainTree from QTreeview
maintree.cpp file
void MainTree::LaunchTree()
{
//Tree launching
connect(this, SIGNAL(customContextMenuRequested(const QPoint& )),this,SLOT(showCustomContextMenu(const QPoint&)));
}
void MainTree::showCustomContextMenu(const QPoint &pos)
{
//Add actions
}
But i get the following error
QObject::connect: No such slot QTreeView::showCustomContextMenu(const QPoint&)
I could not understand why, am i missing something ??
Definition of the class MainTree
class MainTree : public QTreeView
{
public:
MainTree();
MainTree(QWidget *parent = 0);
public slots:
private slots:
void showCustomContextMenu(const QPoint& pos);
private:
void launchTree();
};
You are missing the Q_OBJECT macro out, so try this:
class MainTree : public QTreeView
{
Q_OBJECT
// ^^^^^
public:
MainTree();
MainTree(QWidget *parent = 0);
public slots:
private slots:
void showCustomContextMenu(const QPoint& pos);
private:
void launchTree();
};
Do not forget to re-run qmake after this to regenerate the moc files properly. Make sure you have the moc include at the end of your source code, or you handle the moc generation without that.
Also, note that if you used Qt 5.2 or later with C++11 support, you would get a static assertion about the missing Q_OBJECT macro, so you would not get runtime issues anymore. I suggest to follow that if you can.
When referring to slot and signals you have to omnit all decoration: const & and so on (only star can remain).
connect(this, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(showCustomContextMenu(QPoint)))
Also you forgot about Q_OBJECT macro.
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.