Jūs esate

Qt. How to create a contex menu for QTableWidget?

Add a method definition into your .h file:
private slots: void cool_slot();
Add a line in the constructor of the class:
m_ui->tableWidgetRemoteSite->setContextMenuPolicy(Qt::CustomContextMenu);
Then click on your QTableWidget component and add a method for your customContextMenuRequested(QPoint pos) event (right click and select Go to slot...):
void MDIChildWindow::on_tableWidgetRemoteSite_customContextMenuRequested(QPoint pos)

{

QMenu *contextMenu = new QMenu();

contextMenu->addAction(tr("Cool option 1"), this, SLOT(cool_slot()));

contextMenu->addSeparator();

contextMenu->addAction(tr("Cool option 2"), this, SLOT(cool_slot()));

contextMenu->exec(m_ui->tableWidgetRemoteSite->mapToGlobal(pos));

}

And a method:

void MDIChildWindow::cool_slot() {

QMessageBox::information(this, "Test", "Testing!");

}

As for me - it works :)

Komentuoti