void MyWidget::contextMenuEvent(QContextMenuEvent */*event*/)
{
QMenu *menu = new QMenu()
menu->addAction("退出")
menu->exec(QCursor::pos())
}
不解释,自己看。// myWidget is any QWidget-derived class
myWidget->setContextMenuPolicy(Qt::CustomContextMenu)
connect(myWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ShowContextMenu(const QPoint&)))
void MyClass::ShowContextMenu(const QPoint& pos) // this is a slot
{
// for most widgets
QPoint globalPos = myWidget->mapToGlobal(pos)
// for QAbstractScrollArea and derived classes you would use:
// QPoint globalPos = myWidget->viewport()->mapToGlobal(pos)
QMenu myMenu
myMenu.addAction("Menu Item 1")
// ...
QAction* selectedItem = myMenu.exec(globalPos)
if (selectedItem)
{
// something was chosen, do stuff
}
else
{
// nothing was chosen
}
}
'''
【简介】
'''
import sys
from PyQt5.QtWidgets import (QMenu, QPushButton, QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem,
QHeaderView)
from PyQt5.QtCore import QObject, Qt
class Table(QWidget):
if name == ' main ':
app = QApplication(sys.argv)
example = Table()
example.show()
sys.exit(app.exec_())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)