Adding new "SLOT" and "SIGNAL" into existing class
-
Dear all:
I want to add "SLOT" and "SIGNAL" of existing item in my .ui file. How should I do??I only know a way that is creating new class inherits " Qobject" like following:@#ifndef MODEL_H
#define MODEL_H
#include <QObject>class Model : public QObject {
Q_OBJECTpublic:
Model() { m_value = 0; }int value() const { return m_value; }
public slots:
void setValue(int);signals:
void valueChanged(int);private:
int m_value;
};#endif@
But if I want to add new "SLOT" and "SIGNAL" into QPushButton class, how can I do??
Thanks. -
Hi,
Please use code tags around your code(first button on the right). This will make it easier to read.
QPushButton and all other ui classes inherit from QObject, so you can subclass them just as easily as the basic QObject.
@
class CustomButton : public QPushButton {
Q_OBJECT
public:
CustomButton(QWidget* parent = 0)
: QPushButton(parent) {}
signals:
void customSignal(int);
slots:
void customSlot() { emit customSignal(42); }
}
@ -
Thanks for advice, I have already changed my article. Your suggestion is good, so if I want to add custom "SLOT" and "SIGNAL", the unique method is inherit that class like you said or I can do something more efficient !?
-
What's not efficient about it? It's what QPushButton and any other widget do underneath too.
-
I see. Thank you.