Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding new "SLOT" and "SIGNAL" into existing class
Qt 6.11 is out! See what's new in the release blog

Adding new "SLOT" and "SIGNAL" into existing class

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ivan1120
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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); }
      }
      @

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Ivan1120
        wrote on last edited by
        #3

        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 !?

        1 Reply Last reply
        1
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Ivan1120
            wrote on last edited by
            #5

            I see. Thank you.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved