Problem with Context Menu
-
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( pos ); 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(); }
@
-
You forgot the Q_OBJECT macro in your header...
-
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) ) );
@ -
The const is ignored by signal/slot anyway, so that should not matter.
-
thanks, including the macro Q_OBJECT I solved the problem.