Important: Please read the Qt Code of Conduct -

Object::connect: No such slot

  • Hi, I've been searching on Google and trying fixes for a good hour and can't get this to work.
    I'm trying to connect an action of a custom context menu I've created to a slot, but it doesn't connect properly for some reason.
    I have a class PseudoTree that extends from QTreeWidget and a class PseudoItem that extends from QTreeWidgetItem. When I right click on an item of the tree I want a custom context menu to pop up, which it does. This menu creates some QAction's and then connects those to slots of PseudoTree but fails to do so.

    Here's the header of PseudoTree

    class PseudoTree : public QTreeWidget
    explicit PseudoTree(QWidget *parent=0);

    my private atributes and functions
    public Q_SLOTS:
    void onCustomContextMenu(const QPoint &);
    void addChild(const QPoint &);
    void addSibling(const QPoint &);

    Following code is written at the .cpp file
    Here's the connect() to be able to create a custom menu, which works perfectly
    connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onCustomContextMenu(const QPoint &)));

    And here's in italics the piece of code that fails within onCustomContextMenu slot
    void PseudoTree::onCustomContextMenu(const QPoint &p)
    QMenu contextMenu("Menu", this);
    QAction action("Add child", this);
    connect(&action, SIGNAL(triggered()), this, SLOT(addChild(const QPoint & p)));


    Lastly, here's how addChild is implemented, just in case it's helpful -remember it never gets to execute this-
    void PseudoTree::addChild(const QPoint & p)
    some code

    For some reason the connect() fails to connect my action with my slot and I really can't see why, hope you guys can help me get around this!
    Sorry if I didn't express myself very well and thanks in advance to everyone who reads and thinks about this (:

  • AFAIK you cannot connect an argumentless signal to a slot with arguments. How would the metaobject system guess what to pass to the slot?

    Edit: for completeness, the doc clearly says "The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)"

  • Moderators

    @Zlatty to add to @JohanSolo

    you can change it to a lambda function to pass it a default QPoint:

    connect(&action, &QAction::triggered, [=]{addChild(QPoint(0,0));});

  • @JohanSolo @J-Hilk Thanks for the replies, I see now why this wouldn't work.

    I can't make the lambda function solution work though. I get some nasty compilation errors

    Should I maybe override a clicking signal so that it captures the QPoint? I will keep investigating and update the post if I discover something.

  • You may want to create an intermediate slot. This slot would be connected to the triggered() signal and would extract the cursor position, which could then be emitted and captured by your addChild slot.

  • Thanks for your help @JohanSolo, got it working with that approach

Log in to reply