Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Why doesn't the connect procedure connect?

    General and Desktop
    5
    9
    2061
    Loading More Posts
    • 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
      Patou355 last edited by

      Hello all. I begin as a Qt programmer.

      It's a problem about the connect procedure. I feel I miss something simple, stupid but as I don't find it, here's the question:

      Here's the problematic code:

      QTableWidget* tabView = new QTableWidget(_view);
      
      connect(tabView, SIGNAL(itemChanged(QTableWidgetItem*)), (RppDataManagementWidget*)this, SLOT(modifyDataFrame(QTableWidgetItem*)));
      

      And here's the code of the called slot:

      void RppDataManagementWidget::modifyDataFrame(QTableWidgetItem* newItem){
          std::cout << "Here it is: " << newItem->text().toStdString() << std::endl;
      }
      

      Well, it's not the whole code of the slot but the 'cout' line is a test to know if the slot is actually called. And it isn't. Nothing happens on the standard output when I:

      • double-click on a cell;
      • type some new text;
      • press enter.
        I should read "Here it is: my new text in the cell"...

      Obviously, they aren't connected. Connect is used many times in the program in the same way and it works. This line for example:

      connect(tabView->horizontalHeader(), SIGNAL(sectionResized(int, int, int)), (RppDataManagementWidget*)this, SLOT(resizeColumns(int, int, int)));
      

      works well.

      I tried other signals, like itemChanged and others. Nothing more happens...

      The only difference I spotted with other connect utlisations is that the data given to the slot as an argument is a primitive type (int, string...) while here, it's a pointer to a QTableWidgetItem.

      I have no error nor warning at the compilation, and no error message during the execution. This code is a part of a big project I'm working on...

      Who knows what I forgot?

      Patrick.

      1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by

        try with connect(tabView, &QTableWidget::itemChanged, this, &RppDataManagementWidget::modifyDataFrame);

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply Reply Quote 3
        • P
          Patou355 last edited by Patou355

          I tried and I got no building error, but when the tabView appears, the program crashes:
          "The program has unexpectedly finished."

          1 Reply Last reply Reply Quote 0
          • P
            Patou355 last edited by

            Ok, it works, the problems are somewhere else!

            Your line works but doesn't look like what I could read in the tutorials. Why?

            Thanks a lot.

            1 Reply Last reply Reply Quote 0
            • mrjj
              mrjj Lifetime Qt Champion last edited by

              • Your line works but doesn't look like what I could read in the tutorials. Why?

              Its using the new connect syntax.

              https://wiki.qt.io/New_Signal_Slot_Syntax

              the old one is the SIGNAL() and SLOT()

              The new one provides better type check and allows for functions to be used as slot.

              The old one is easier to use but might fail silently.

              One note.
              The connect returns TRUE or FALSE and its always good to check.

              1 Reply Last reply Reply Quote 4
              • P
                Patou355 last edited by

                Ok, if I understand, my initial syntax is "legacy" syntax. Should I replace them by the new syntax in the entire program?

                1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion last edited by mrjj

                  @Patou355

                  Using the new syntax will help maintenance.
                  So I would considered it worth the time to replace.

                  Note that for a few overloaded function the needed syntax is pretty ugly

                  connect(cmbProfiles,
                  static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),
                  this,
                  &MyClass::loadProfilesDetails);

                  https://forum.qt.io/topic/20998/qt5-new-signals-slots-syntax-does-not-work-solved/8

                  1 Reply Last reply Reply Quote 3
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Hi,

                    To add to @mrjj, you can use the qOverload method avoid that ugly syntax.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    kshegunov 1 Reply Last reply Reply Quote 1
                    • kshegunov
                      kshegunov Moderators @SGaist last edited by

                      @SGaist said in Why doesn't the connect procedure connect?:

                      To add to @mrjj, you can use the qOverload method avoid that ugly syntax.

                      Also a rather nifty trick I recently became aware of:

                      connect<void (QComboBox::*)(const QString &)>(cmbProfiles, &QComboBox::currentIndexChanged, this, &MyClass::loadProfilesDetails);
                      

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post