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. Objects and connect
Qt 6.11 is out! See what's new in the release blog

Objects and connect

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 7 Posters 1.3k Views 3 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.
  • P Offline
    P Offline
    pixbyte
    wrote on last edited by
    #1

    In my code I try to receive the name of the control where the text input occour.

    For this I use:

    QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );
    

    and the function:

    void MainWindow::onEnterCDTextText(QString strText)
    {
        qDebug() << sender()->objectName();
    }
    

    But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?

    K JonBJ Pablo J. RoginaP Pl45m4P 4 Replies Last reply
    0
    • P pixbyte

      In my code I try to receive the name of the control where the text input occour.

      For this I use:

      QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );
      

      and the function:

      void MainWindow::onEnterCDTextText(QString strText)
      {
          qDebug() << sender()->objectName();
      }
      

      But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @pixbyte

      Try to set an object name for the object.

      According to https://doc.qt.io/qt-5/qobject.html#objectName-prop the default name is an empty string.

      Vote the answer(s) that helped you to solve your issue(s)

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

        So where do you set your object name to the one you expect?

        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
        3
        • W Offline
          W Offline
          wrosecrans
          wrote on last edited by
          #4

          Do you ever set the object's name? You won't get the name of an object that you used in the C++ source -- you only get a name explicitly set with setObjectName(). C++ doesn't allow that sort of introspection automatically.

          1 Reply Last reply
          1
          • P pixbyte

            In my code I try to receive the name of the control where the text input occour.

            For this I use:

            QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );
            

            and the function:

            void MainWindow::onEnterCDTextText(QString strText)
            {
                qDebug() << sender()->objectName();
            }
            

            But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?

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

            @pixbyte
            By default there would be no object name set. To do it this way you would need to use setObjectName() to have set the name previously.

            Consider instead of sender() using a lambda connection, like:

            QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged,
                              this, [cdTextArrangerEdit, this](const QString &strText) { onEnterCDTextText(cdTextArrangerEdit->objectName(), strText); } );
            
            // or, you might want to pass the literal name here:
            QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged,
                              this, [this](const QString &strText) { onEnterCDTextText("cdTextArrangerEdit", strText); } );
            
            void MainWindow::onEnterCDTextText(const QString &objectName, const QString &strText)
            {
                qDebug() << objectName;
            }
            

            You can do more things with lambdas than via sender(), which also has issues.

            1 Reply Last reply
            3
            • P pixbyte

              In my code I try to receive the name of the control where the text input occour.

              For this I use:

              QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );
              

              and the function:

              void MainWindow::onEnterCDTextText(QString strText)
              {
                  qDebug() << sender()->objectName();
              }
              

              But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @pixbyte you may want to check this document to see how they're printing the object name.

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2
              • P pixbyte

                In my code I try to receive the name of the control where the text input occour.

                For this I use:

                QObject::connect( cdTextArrangerEdit, &QLineEdit::textChanged, this, &MainWindow::onEnterCDTextText );
                

                and the function:

                void MainWindow::onEnterCDTextText(QString strText)
                {
                    qDebug() << sender()->objectName();
                }
                

                But I always recive an empty qDebug string. My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @pixbyte said in Objects and connect:

                My idea was to receive the name "cdTextArrangerEdit". Is there anything I missunderstood

                Yes!
                objectName != name of your variable

                The objectName is a (not only) Qt specific thing. You can give every QObject its own unique name.
                QObjects you create in QtDesigner have a default name, others don't. So you have to set one.

                lineEditObject.png

                Having a QLineEdit created in Designer before,

                this

                    QLineEdit * lineEd = new QLineEdit(this);
                    qDebug() << ui->lineEdit->objectName();
                    qDebug() << lineEd->objectName();
                

                prints:

                "lineEdit"
                ""
                

                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                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