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. Object::connect: No such slot
QtWS25 Last Chance

Object::connect: No such slot

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

    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
    {
    Q_OBJECT
    public:
    explicit PseudoTree(QWidget *parent=0);
    ~PseudoTree();

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

    @
    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)));
    contextMenu.addAction(&action);

    contextMenu.exec(mapToGlobal(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 (:

    J.HilkJ 1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #2

      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.)"

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      4
      • Z Zlatty

        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
        {
        Q_OBJECT
        public:
        explicit PseudoTree(QWidget *parent=0);
        ~PseudoTree();

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

        @
        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)));
        contextMenu.addAction(&action);

        contextMenu.exec(mapToGlobal(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 (:

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

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

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • Z Offline
          Z Offline
          Zlatty
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by
            #5

            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.

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            4
            • Z Offline
              Z Offline
              Zlatty
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved