#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QAction *action;
QMenu *menu;
void actions();
public slots:
void contextMenuEvent(QContextMenuEvent *event);
void actionClick();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qDebug>
#include <QContextMenuEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->setColumnCount(2);
ui->tableWidget->setRowCount(2);
action = new QAction(this);
actions();
connect(action, SIGNAL(triggered(bool)), this, SLOT(actionClick()));
}
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
qDebug() << "contextMenuEvent here";
QPoint point = event->pos();
QTableWidgetItem *item = ui->tableWidget->itemAt(point);
if(item != NULL)
{
qDebug() << "row" << item->row();
menu->clear();
menu->addAction(action);
menu->addSeparator();
menu->exec(QCursor::pos());
event->accept();
}
}
void MainWindow::actions()
{
menu = new QMenu();
action->setText(QString("action"));
}
void MainWindow::actionClick()
{
qDebug() << "actionClick here";
}
MainWindow::~MainWindow()
{
delete ui;
}