Qt World Summit: Register Today!

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 {

    Model() { m_value = 0; }

     int value() const { return m_value; }

    public slots:
    void setValue(int);

    void valueChanged(int);

    int m_value;


    But if I want to add new "SLOT" and "SIGNAL" into QPushButton class, how can I do??

  • Moderators


    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 {
    CustomButton(QWidget* parent = 0)
    : QPushButton(parent) {}
    void customSignal(int);
    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 !?

  • Moderators

    What's not efficient about it? It's what QPushButton and any other widget do underneath too.

  • I see. Thank you.

Log in to reply