PDA

View Full Version : Problems with contextmenu in a QTreeWidget



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

Santosh Reddy
31st August 2013, 11:23
While making connection the oder and type of the parameters in the signature of the slot and signal should be same. Note that QPoint and const QPoint are considered as different types. This you could have figured out by looking at the warning messages in application output.


//cabletreewidget.h
#ifndef CABLETREEWIDGET_H
#define CABLETREEWIDGET_H

#include <QTreeWidget>

class CableTreeWidget : public QTreeWidget
{
Q_OBJECT

public:
CableTreeWidget();
~CableTreeWidget();

private:

protected slots:
void ShowContextMenu(const QPoint & ); //<<<<<<<<<<< added const

};

#endif // CABLETREEWIDGET_H




//cabletreewidget.cpp
#include "cableTreeWidget.h"
#include <QtCore>
#include <QMenu>


CableTreeWidget::CableTreeWidget()
{
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(ShowContextMenu(const QPoint&))); //<<<<<<<<<<< added const
}

CableTreeWidget::~CableTreeWidget()
{

}


void CableTreeWidget::ShowContextMenu(const QPoint &pos) //<<<<<<<<<<< added const
{

QPoint globalPos = this->mapToGlobal( pos );

QMenu myMenu;
myMenu.addAction("Menu Item 1");
// ...

QAction* selectedItem = myMenu.exec(globalPos);
}

Gruwe
31st August 2013, 19:03
Hello,

thanks for your reply!

I tried your solution, but the contextmenu doenst work :(

Anyone knows the problem? :(

Santosh Reddy
31st August 2013, 20:14
I tried your solution, but the contextmenu doenst work
Then you did not understand my reply, please read it again.


Anyone knows the problem?
The problem is already solved in the earlier post, you just have to read the reply carefuly.