更新时间:2023-12-06 08:34:46
我不会说 Python,但它在 C++ 中相当容易.
I can't speak for python, but it's fairly easy in C++.
首先在创建小部件后设置策略:
first after creating the widget you set the policy:
w->setContextMenuPolicy(Qt::CustomContextMenu);
然后将上下文菜单事件连接到插槽:
then you connect the context menu event to a slot:
connect(w, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ctxMenu(const QPoint &)));
最后,您实现了插槽:
void A::ctxMenu(const QPoint &pos) {
QMenu *menu = new QMenu;
menu->addAction(tr("Test Item"), this, SLOT(test_slot()));
menu->exec(w->mapToGlobal(pos));
}
这就是你在 c++ 中的做法,在 python API 中应该不会有太大的不同.
that's how you do it in c++ , shouldn't be too different in the python API.
在谷歌上环顾四周后,这是我在python中的示例的设置部分:
after looking around on google, here's the setup portion of my example in python:
self.w = QWhatever();
self.w.setContextMenuPolicy(Qt.CustomContextMenu)
self.connect(self.w,SIGNAL('customContextMenuRequested(QPoint)'), self.ctxMenu)