Qt World Summit: Register Today!

QT5 style signal/slot events / doc of QT5 QAction provide QT4 style unique exemple...

  • I try to learn about QT5 style signal slot event... and bind this with simple QAction::triggered like (in the constructor):
    supprimer = new QAction(tr("Supprimer"), this);
    QObject::connect(supprimer, &QAction::triggered, delete_row());

    my QAction is defined as private pointer,
    my function is defined as public (for try first) void function and declared like:

    void My_TreeView::delete_row() {
    QMessageBox::information(this, "Delete row", "OK");

    but this générate an error: "invalid use of void expression"

    also, i'm surprise to read inside the doc at QAction Qt C++ class of QT5, that the exemple is QT4 style (what is the logic here ?)
    also... i'm surprise because i followed this: http://qt-project.org/wiki/New_Signal_Slot_Syntax

    so my questions are:

    what i forgot or do wrong ?

    where i can find a complete exemple who works ?

    where i can find the correct link for QT5 C++ class with QT5 exemples inside ?

    thanks for help.

  • Qt Champions 2017

    You are not using the new syntax correctly. deleterow is class method. You need appropriate scope resolution to make it happen.

  • Lifetime Qt Champion


    IIRC you need to use:

    @QObject::connect(supprimer, &QAction::triggered, this, &My_TreeView ::delete_row);@

    For the documentation part you have it "here":http://doc.qt.io/qt-5/signalsandslots.html#advanced-signals-and-slots-usage

    Hope it helps

  • here: http://qt-project.org/wiki/New_Signal_Slot_Syntax
    *i can read this: *

    New: connecting to simple function

    The new syntax can even connect to functions, not just QObjects:
    connect(sender, &Sender::valueChanged, someFunction);

    here: http://qt.developpez.com/tutoriels/qt-5/woboq-signaux-slots/

    i can read this:
    connection to functions▲

    In the last exemple, the slot has been simply declared as public function and not slot. Qt directly use pointer of function and no need introspection of the moc , but this one allways necessary for generate signals.

    It is also possible to connect a slot directly to a function:

    static void someFunction() {
    qDebug() << "appuyé";
    // other part
    QObject::connect(button, &QPushButton::clicked, someFunction);

    could you please tell me more about wich syntax could correctly be write ? i'm really confused with docs i read around this. could you provide in my situation an exemple of correct syntax please ?

  • yes SGaist, this is perfect.

    i was confused because of some read around docs i paste here talk about direct connection (i maybe not really understand the syntax of the doc).
    Also, QT5 part of QAction official doc not use QT5 syntax.

    thank you for your help again.

  • 2jerom_isAviable? The problem is not the new/old syntax style but the usage.

    The old Qt4 style syntax takes const char* as signals and slots names and connects them on runtime if found in the meta system.

    The new Qt5 syntax uses compile time connection and takes pointer to function as signals and slots arguments. These are usually obtained like this:


    Notice the reference symbol and lack of parenthesis. So in your example

    QObject::connect(supprimer, &QAction::triggered, delete_row());

    Your delete_row() is wrong because it is a function call and returns void hence the error “invalid use of void expression”. You need to provide reference to that function (if delete_row is in your current namespace/class but I would still recommend using its namespace anyway...):

    QObject::connect(supprimer, &QAction::triggered, &delete_row);

    Note that for overloaded signals/slots you will need to explicitly cast the function to the function pointer matching the desired signature:

    QObject::connect(supprimer, &QAction::triggered, static_cast<void () ()>(&delete_row));
    //or with namespace/class
    QObject::connect(supprimer, &QAction::triggered, static_cast<void (MyClass::
    ) ()>(&MyClass::delete_row));

    Recap on function pointers syntax in layman terms: They have 3 elements, first is the return value, then comes the location in parenthesis (i.e. namespace, class) followed by an asterisk and then the third part is the arguments list in parenthesis and without names, only types. Google it for more details but despite its somewhat awkward syntax there is no mystery about it.

    There really is no magic here but I agree that documentation does not often cover the exact details of all this. However that is apparently on purpose to spare Qt users the need to go and understand everything what is going on under the hood. But at least for signals and slots this imho should be explained in more or clearer detail.

  • really good explication, thanks for do it.

    when i write that i think the syntax in the doc is "QT4 style" inside a "QT5 doc", i not said that the syntax not works in QT5, but i do *the total affirmation * that for learn, this practice of mix titles and different syntax style just make more confusion, much more when ther is only one poor exemple and when you need to open many tabs for read one line on each to try to understand the mechanism (imagine that peoples who comes to read doc is also for learn around the stuff provided... right ?), it would be more clear if the QT5 doc provide QT5 syntax exempleS inside (and i think this is simple to understand, also, not only: this is a good and constructive critic that have to be accepted... i hope so).

    I know a doc is really difficult to write, as soon as teacher is also a real job... you can perfectly know how to do the code and write a bad doc or be apsolutly bad for teach something you know (and bad more if you think about the fault of peoples not understand is from they are stupids... except if they never understand nothing everywhere for everythinks...), because lake of exemples, lake of coherence for someone has to read and don't know nothing around QT and just want to learn. I know also that accept a critic is difficult for humans...

    Definitly, the QT doc is (not all the time, but lot of time) an obscure an difficult doc to read. It is not the first doc i read...

    i hope this can not be a big problem to affirm an opinion and make the demonstration of it.

    I'm really happy there is a forum because peoples here help me a lot (also quick more if i read all the doc around a question 3 times...) and provide great explications. Also, it is maybe more simple to answer a question than write a pragmatic and usefull doc... the interactivity of the communication help for sure.

    At the end, there is also not only one link in QT project for show signal and slot (you have, for understand a beginner... and maybe a futur client, to consider this seriously), but more. Consider that people who want to learn by read docs don't know directly wich doc is good doc... the last link SGaist give me was great... the link i provide and read first make me more confused... but if there is only one good link, it would be better definitly. this is so logical.

    Also, after be able to understand (thanks again all here who provide quickly great explications), it seems that QT is not so complicate... but sure, the doc is really not a good doc (some parts are ok... near some other...).

    I think you know it allready, i can not imagine you not see that, it is really flagrant.

Log in to reply