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. Same slot name executing on parent.
Forum Updated to NodeBB v4.3 + New Features

Same slot name executing on parent.

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 751 Views
  • 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.
  • N Offline
    N Offline
    Nekreg45
    wrote on last edited by Nekreg45
    #1

    Are parents suppose to execute slots of the same name as their children? This seems like a glitch to me.

    I came across this issue and created a test project. Sure enough. If I have a slot (even if both are private), it will execute the slot on both the child and parent if the name is the same. Here is the code:

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),
    _dialog(new TestDialog(this))
    {
    ui->setupUi(this);
    _dialog->show();
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_close_clicked()
    {
    qDebug()<<"Close on MainWindow"<<endl;
    }

    TestDialog::TestDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TestDialog)
    {
    ui->setupUi(this);
    }

    TestDialog::~TestDialog()
    {
    delete ui;
    }

    void TestDialog::on_pushButton_close_clicked()
    {
    qDebug()<<"Close on TestDialog"<<endl;
    }

    The display is just a pushbutton on both the MainWindow and TestDialog. If I press "Close" on TestDialog, then the debugger will display the following:

    Close on TestDialog

    Close on MainWindow

    This means it is executing the TestDialog slot then the MainWindow slot. Even though there is no connection. It just has the same name.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      signal, child, qwidget?

      What's your question?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • N Nekreg45

        Are parents suppose to execute slots of the same name as their children? This seems like a glitch to me.

        I came across this issue and created a test project. Sure enough. If I have a slot (even if both are private), it will execute the slot on both the child and parent if the name is the same. Here is the code:

        MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow),
        _dialog(new TestDialog(this))
        {
        ui->setupUi(this);
        _dialog->show();
        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        void MainWindow::on_pushButton_close_clicked()
        {
        qDebug()<<"Close on MainWindow"<<endl;
        }

        TestDialog::TestDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::TestDialog)
        {
        ui->setupUi(this);
        }

        TestDialog::~TestDialog()
        {
        delete ui;
        }

        void TestDialog::on_pushButton_close_clicked()
        {
        qDebug()<<"Close on TestDialog"<<endl;
        }

        The display is just a pushbutton on both the MainWindow and TestDialog. If I press "Close" on TestDialog, then the debugger will display the following:

        Close on TestDialog

        Close on MainWindow

        This means it is executing the TestDialog slot then the MainWindow slot. Even though there is no connection. It just has the same name.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Nekreg45
        You appear to be using the auto-connect-by-name feature of slots via the UI Designer? You might look at the code generated, and try changing over to manually naming and connecting slots?

        Christian EhrlicherC N 2 Replies Last reply
        0
        • JonBJ JonB

          @Nekreg45
          You appear to be using the auto-connect-by-name feature of slots via the UI Designer? You might look at the code generated, and try changing over to manually naming and connecting slots?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I love people who heavily modify their posts afterwards... where's the downvote button??

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          N 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            I love people who heavily modify their posts afterwards... where's the downvote button??

            N Offline
            N Offline
            Nekreg45
            wrote on last edited by
            #5

            @Christian-Ehrlicher Sorry, I accidentally submitted the post before I wrote it. I was scrambling to write it up. It was not my intention.

            1 Reply Last reply
            1
            • JonBJ JonB

              @Nekreg45
              You appear to be using the auto-connect-by-name feature of slots via the UI Designer? You might look at the code generated, and try changing over to manually naming and connecting slots?

              N Offline
              N Offline
              Nekreg45
              wrote on last edited by
              #6

              @JonB I am using the auto-connect-by-name feature. I was able to fix this for my program by changing the name.

              My question was whether this behavior was intended by Qt or is this a glitch. It seems very odd to me. I'm not sure why anyone would want this to happen.

              JonBJ 1 Reply Last reply
              0
              • N Nekreg45

                @JonB I am using the auto-connect-by-name feature. I was able to fix this for my program by changing the name.

                My question was whether this behavior was intended by Qt or is this a glitch. It seems very odd to me. I'm not sure why anyone would want this to happen.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Nekreg45
                Since you ask that, and your original questions states

                Are parents suppose to execute slots of the same name as their children? This seems like a glitch to me.

                I would start by reading QMetaObject::connectSlotsByName(QObject *object)

                Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form:
                void on_<object name>_<signal name>(<signal parameters>);

                Note the recursively.

                This only adds to the case that you should not be using the connect-by-name feature in the first place, because of such an issue and others.

                N 1 Reply Last reply
                3
                • JonBJ JonB

                  @Nekreg45
                  Since you ask that, and your original questions states

                  Are parents suppose to execute slots of the same name as their children? This seems like a glitch to me.

                  I would start by reading QMetaObject::connectSlotsByName(QObject *object)

                  Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form:
                  void on_<object name>_<signal name>(<signal parameters>);

                  Note the recursively.

                  This only adds to the case that you should not be using the connect-by-name feature in the first place, because of such an issue and others.

                  N Offline
                  N Offline
                  Nekreg45
                  wrote on last edited by
                  #8

                  @JonB Interesting. Thanks for the info. It still feels odd why it would search through other objects, but maybe there is grander purpose designed by Qt than what fits in my little example.

                  I've been using the connect-by-name feature for years and this is the first time I stumbled on this problem. I guess I should change up my habit in the future and create my own connects.

                  Thanks again!

                  JonBJ 1 Reply Last reply
                  0
                  • N Nekreg45

                    @JonB Interesting. Thanks for the info. It still feels odd why it would search through other objects, but maybe there is grander purpose designed by Qt than what fits in my little example.

                    I've been using the connect-by-name feature for years and this is the first time I stumbled on this problem. I guess I should change up my habit in the future and create my own connects.

                    Thanks again!

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Nekreg45
                    See Nailing 13 signal and slot mistakes with clazy 1.3. That's KDAB and from 2018. It suggests:

                    1. connect-by-name

                    Warns when auto-connection slots are used. They’re also known as “connect by name”, an old and unpopular feature which shouldn’t be used anymore.

                    1 Reply Last reply
                    1

                    • Login

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