Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why doesn't the connect procedure connect?



  • 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.



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



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



  • 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.


  • Lifetime Qt Champion

    • 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.



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


  • Lifetime Qt Champion

    @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


  • Lifetime Qt Champion

    Hi,

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


  • Qt Champions 2017

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

Log in to reply