PDA

View Full Version : No reaction after right clicking on the QTableWidget's headers



Stanfillirenfro
2nd April 2014, 08:21
Hi!

I have set the headers for my QTableWidget and want to perform some actions by right clicking on them, but I am failing. That means, I am not able to display the menu. Does anyone already have such problem? How to overcome it? However, any right click on a given cell of the corresponding column/row displays the menu.

Many thanks in advance.

Here is my code:


void MyWindow::showContextMenu(const QPoint &pos)
{
QMenu *menu = new QMenu(this);

m_insererRow = menu->addAction("Inser row here");

QAction *selectedItem = menu->exec(QCursor::pos());

// Some actions...
}

anda_skoa
2nd April 2014, 09:25
How does your code relate to a QTableWidget?
Is MyWindow a QTableWidget subclass?
How are you calling showContextMenu()?

Cheers,
_

Stanfillirenfro
2nd April 2014, 10:53
Thanks Ando_skoa for your reply.



Is MyWindow a QTableWidget subclass?

Yes, MyWindow is a QtableWidget subclass.


How are you calling showContextMenu()?
Please have a look at the line 5 of .cpp

MyWindow is a widget having a QWidget as parent.

.h

class AWindow: public QDialog
{
Q_OBJECT
public:
MyWindow(QWidget *parent = 0)

public slots:
void showContextMenu(const QPoint &pos);

private:

QTableWidget *m_MyWindow;
};

.cpp

MyWindow::MyWindow(QWidget *parent): QDialog(parent)
{
m_mywindow = new QTableWidget(this);

connect(m_myWindow, SIGNAL(customContextMenuRequested(const QPoint)), this, SLOT(showContextMenu(const QPoint)));
}

void MyWindow::showContextMenu(const QPoint &pos)
{
QMenu *menu = new QMenu(this);

m_insererRow = menu->addAction("Inser row here");

QAction *selectedItem = menu->exec(QCursor::pos());

// Some actions...
}

Many thanks in advance!

Added after 54 minutes:

Any help? Do you required more information please?

anda_skoa
2nd April 2014, 17:00
You don't seem to have switched the contextMenuPolicy of the table view to CustomContextMenu

Cheers,
_

Stanfillirenfro
2nd April 2014, 21:56
Thanks Anda_Skoa!

Of courses, I did it in the constructor of my class liste this:


m_mywindow->setContextMenuPolicy(Qt::CustomContextMenu);

Is the constructor the wright place for that or should I do it in the method?

Thanks in advance

Stanfillirenfro
3rd April 2014, 07:50
Does anyone have an idea please?

nix
3rd April 2014, 08:20
contextMenuRequest on headers are trigger by the QHeaderView try something adapting this :


ui->tableWidget->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
ui->tableWidget->verticalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->tableWidget->horizontalHeader(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(clicked()));
connect(ui->tableWidget->verticalHeader(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(clicked()));

Stanfillirenfro
3rd April 2014, 08:48
Many thanks Nix!!!

It is working perfectly. The problem is solved.

Many thanks one more time!