Gruwe
31st August 2013, 10:37
Hello,
I want to code a programm with a QTreeWidget. In that QTreeWidget I want to add a item with Right-Click and open a contextmenu with the option "add item", "remove item", aso..
I have used a QMainWindow where I set a new Class of QTreeWidget as the central widget.
That works fine, the TreeWidget appears in the MainWindow.
Now I would like to make a contextmenu with right-click in the Treewidget. Here is my code from my QTreeWidget:
Here the code of the cabletreewidget.h:
#ifndef CABLETREEWIDGET_H
#define CABLETREEWIDGET_H
#include <QTreeWidget>
class CableTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
CableTreeWidget();
~CableTreeWidget();
private:
protected slots:
void ShowContextMenu( QPoint& );
};
#endif // CABLETREEWIDGET_H
And here the code of the cpp-File:
#include "cableTreeWidget.h"
#include <QtCore>
#include <QMenu>
CableTreeWidget::CableTreeWidget()
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(ShowContextMenu(QPoint&)));
}
CableTreeWidget::~CableTreeWidget()
{
}
void CableTreeWidget::ShowContextMenu(QPoint &pos)
{
QPoint globalPos = this->mapToGlobal( pos );
QMenu myMenu;
myMenu.addAction("Menu Item 1");
// ...
QAction* selectedItem = myMenu.exec(globalPos);
}
I found that code at the internet. There is no more code at this time.
Ok, the compilert works fine, no problems, no warnings. But when I start the programm and right-click on the TreeWidget, unfortunately a contextmenu doesnt appears. Can anyone tell me what's wrong?
Thank you! :)
Greetings
I want to code a programm with a QTreeWidget. In that QTreeWidget I want to add a item with Right-Click and open a contextmenu with the option "add item", "remove item", aso..
I have used a QMainWindow where I set a new Class of QTreeWidget as the central widget.
That works fine, the TreeWidget appears in the MainWindow.
Now I would like to make a contextmenu with right-click in the Treewidget. Here is my code from my QTreeWidget:
Here the code of the cabletreewidget.h:
#ifndef CABLETREEWIDGET_H
#define CABLETREEWIDGET_H
#include <QTreeWidget>
class CableTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
CableTreeWidget();
~CableTreeWidget();
private:
protected slots:
void ShowContextMenu( QPoint& );
};
#endif // CABLETREEWIDGET_H
And here the code of the cpp-File:
#include "cableTreeWidget.h"
#include <QtCore>
#include <QMenu>
CableTreeWidget::CableTreeWidget()
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(ShowContextMenu(QPoint&)));
}
CableTreeWidget::~CableTreeWidget()
{
}
void CableTreeWidget::ShowContextMenu(QPoint &pos)
{
QPoint globalPos = this->mapToGlobal( pos );
QMenu myMenu;
myMenu.addAction("Menu Item 1");
// ...
QAction* selectedItem = myMenu.exec(globalPos);
}
I found that code at the internet. There is no more code at this time.
Ok, the compilert works fine, no problems, no warnings. But when I start the programm and right-click on the TreeWidget, unfortunately a contextmenu doesnt appears. Can anyone tell me what's wrong?
Thank you! :)
Greetings