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. Problem with Context Menu
Qt 6.11 is out! See what's new in the release blog

Problem with Context Menu

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.5k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I'm trying to show a contextual menu on a QTreeWidgetItem into a QTreeWidget, but I get the following error "Object::connect: No such slot QTreeWidget::contextMenu(QPoint)"

    Here I leave my source code to see if any of you can help me to get off this error.

    @#ifndef CUSTOMTREEWIDGET_H
    #define CUSTOMTREEWIDGET_H
     
    #include <QTreeWidget>
     
    class CustomTreeWidget : public QTreeWidget
    {
    public:
        CustomTreeWidget(QWidget *parent = 0);
        ~CustomTreeWidget();
     
    public slots:
        void contextMenu(const QPoint &pos);
    };
     
    #endif // CUSTOMTREEWIDGET_H
     
    #include "customtreewidget.h"
    #include "defaulttreeitem.h"
     
    /*constructor aqui*/
     
    void CustomTreeWidget::contextMenu(const QPoint &pos)
    {
        DefaultTreeItem* selected =
                reinterpret_cast<DefaultTreeItem*>(currentItem());
     
        if ( selected != 0 )
            selected->contextMenu(pos);
    }
     
     
    #ifndef DEFAULTTREEITEM_H
    #define DEFAULTTREEITEM_H
     
    #include <QTreeWidgetItem>
     
    class DefaultTreeItem : public QTreeWidgetItem
    {
    public:
        explicit DefaultTreeItem(QTreeWidget *view, int type = Type);
        explicit DefaultTreeItem(QTreeWidgetItem *parent, int type = Type);
        ~DefaultTreeItem();
     
        void contextMenu(const QPoint &pos);
    };
     
    #endif // DEFAULTTREEITEM_H
     
     
    #include "defaulttreeitem.h"
    #include <QMenu>
    #include <QAction>
    /*constructor aqui*/
     
    void DefaultTreeItem::contextMenu(const QPoint &pos)
    {
        // creando el menĂº contextual
        QMenu* contextMenu = new QMenu();
        QAction *actCustom = new QAction("Custom Action", contextMenu);
        contextMenu->addAction(actCustom);
        contextMenu->exec&#40; pos &#41;;
        delete contextMenu;
    }
     
     
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
     
    #include <QMainWindow>
    #include <customtreewidget.h>
     
    namespace Ui {
        class MainWindow;
    }
     
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
     
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
     
    private:
        Ui::MainWindow *ui;
        CustomTreeWidget * tree;
     
    };
     
    #endif // MAINWINDOW_H
     
     
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
     
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
     
        this->tree = new CustomTreeWidget(ui->centralWidget);
        this->tree->setContextMenuPolicy(Qt::CustomContextMenu);
     
        connect(this->tree,SIGNAL ( customContextMenuRequested(QPoint) ),
                this->tree,SLOT ( contextMenu(QPoint) ) );
    }
     
     
    /*....codigo aqui....*/
     
    #include <QtGui/QApplication>
    #include "mainwindow.h"
     
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
     
        return a.exec&#40;&#41;;
    }
    

    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      You forgot the Q_OBJECT macro in your header...

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        Also your slot has a different parameter list. Your slot is:
        @
        void contextMenu(const QPoint &pos);
        @
        change to:
        @
        void contextMenu(QPoint pos);
        @
        since your connect uses:
        @
        connect(this->tree,SIGNAL ( customContextMenuRequested(QPoint) ),
        this->tree,SLOT ( contextMenu(QPoint) ) );
        @

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          The const is ignored by signal/slot anyway, so that should not matter.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            thanks, including the macro Q_OBJECT I solved the problem.

            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