I am trying to disable a default context menu of QTableView in pyqt.
I have re-implemented the contextMenuEvent but it works on 1st time right click. When I click on the same Item 2nd time the default context menu reappears. (Image attached below for referance.)
I tried "QTableView.setContextMenuPolicy(Qt.NoContextMenu)" but it didn't work. Also referred the answers of similar type questions but still the issue is unresolved.
Any idea?
Ex. showing Re-implemented context menu in QTableView.
def contextMenuEvent(self, event):
menu = QMenu(self)
CutAction = QAction(self.view)
CutAction.setText("&Cut")
menu.addAction(CutAction)
CutAction.setIcon(QIcon(":/{0}.png".format("Cut")))
CutAction.setShortcut("Ctrl+X")
self.connect(CutAction, SIGNAL("triggered()"), self.cut)
with the code that shows I can not reproduce your problem, even so the solution is to use Qt::CustomContextMenu by enabling the signal customContextMenuRequested, and in the corresponding slot you have to implement the logic:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class TableView(QTableView):
def __init__(self, *args, **kwargs):
super(TableView, self).__init__(*args, **kwargs)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.onCustomContextMenuRequested)
def onCustomContextMenuRequested(self, pos):
menu = QMenu()
CutAction = menu.addAction("&Cut")
menu.addAction(CutAction)
CutAction.setIcon(QIcon(":/{0}.png".format("Cut")))
CutAction.setShortcut("Ctrl+X")
CutAction.triggered.connect(self.cut)
menu.exec_(self.mapToGlobal(pos))
def cut(self):
pass
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = TableView()
model = QStandardItemModel(10, 10, w)
w.setModel(model)
w.show()
sys.exit(app.exec_())
Related
I am learning Python by creating an MDI application in PyQt5. This application contains a class derived from QMdiSubWindow. These sub-windows need their own menu to be added to the main menu-bar. Where is the 'correct' place to create and show/hide that part of the menu which is only relevant to the sub-window when it's in focus? And where should the menu be destroyed (if it doesn't happen automatically because ownership is taken by the parent)? My attempt at detecting when the sub-window gains/loses focus causes infinite recursion, presumably because the newly visible menu steals the focus back from the sub-window.
This is probably such a common requirement that it's not mentioned in the tutorials, but the only reference in the docs to sub-window menus seems to just refer to the system menu, and not the main menu-bar. Most other Q&A's just refer to activating other sub-windows from the main menu. Several hours of searching haven't quite got what I need, so thank you for your help in either pointing me to the right place in the docs, or by improving my code ... or even both!
A minimal app to illustrate the problem:
#!/usr/bin/python3
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class TestSubWin(QMdiSubWindow):
def __init__(self, parent=None):
super().__init__()
self.setWidget(QLabel("Hello world"))
self.own_menu = QMenu("Sub win menu")
parent.menuBar().addMenu(self.own_menu)
# Add sub-window actions to the menu here
## Causes infinite recursion
# def focusOutEvent(self, event):
# self.own_menu.setVisible(False)
#
# def focusInEvent(self, event):
# self.own_menu.setVisible(True)
class MainWindow(QMainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
file.triggered[QAction].connect(self.windowaction)
self.setWindowTitle("MDI demo")
def windowaction(self, q):
if q.text() == "New":
sub = TestSubWin(self)
self.mdi.addSubWindow(sub)
sub.show()
def main():
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
QMenu and QMenuBar don't take ownership of QActions (and QMenus), unless when created with the functions that accepts icon/title arguments.
This also means that you shall not need to destroy the menus, but only remove them from the menu bar.
The solution is to connect to the subWindowActivated signal, remove the previously added menu, retrieve the menu for the newly active sub window (if any) and add it.
Note that in order to remove a menu from QMenuBar you have to use removeAction() along with the menuAction(), which is the action associated with the menu and shown as menubar title for the menu (or item in a menu for sub menus).
In the following example I'm creating a base subclass for any mdi subwindows that will support menubar menus, and further subclasses for different window types.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MenuSubWin(QMdiSubWindow):
own_menu = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setAttribute(Qt.WA_DeleteOnClose)
def menu(self):
return self.own_menu
class TestSubWin1(MenuSubWin):
def __init__(self):
super().__init__()
self.setWidget(QLabel("Hello world"))
self.own_menu = QMenu("Sub win menu 1")
self.own_menu.addAction('Test 1')
class TestSubWin2(MenuSubWin):
def __init__(self):
super().__init__()
self.setWidget(QLabel("How are you?"))
self.own_menu = QMenu("Sub win menu 2")
self.own_menu.addAction('Test 2')
class MainWindow(QMainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.mdi = QMdiArea()
self.setCentralWidget(self.mdi)
bar = self.menuBar()
fileMenu = bar.addMenu("File")
new1Action = fileMenu.addAction("New 1")
new1Action.setData(TestSubWin1)
new2Action = fileMenu.addAction("New 2")
new2Action.setData(TestSubWin2)
fileMenu.triggered.connect(self.newWindow)
self.setWindowTitle("MDI demo")
self.subWinMenu = None
self.mdi.subWindowActivated.connect(self.subWindowActivated)
def subWindowActivated(self, subWindow):
if self.subWinMenu:
self.menuBar().removeAction(self.subWinMenu.menuAction())
self.subWinMenu = None
if subWindow is None or not hasattr(subWindow, 'menu'):
return
self.subWinMenu = subWindow.menu()
if self.subWinMenu:
self.menuBar().addMenu(self.subWinMenu)
def newWindow(self, action):
cls = action.data()
if not cls:
return
sub = cls()
self.mdi.addSubWindow(sub)
sub.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
Notes:
you shall always set the WA_DeleteOnClose attribute when directly adding a QMdiSubWindow (as opposed to adding a QWidget), otherwise the window will still exist for the MDI area and listed in the subWindowList(), thus preventing proper focus switching (and menu removal) upon closure;
for simplicity, I used the setData() feature of QAction with the class of the window that has to be created;
specifying the signature of signals is only required when signals do have overrides, which is unnecessary for triggered() since it has no overrides; note that Qt is gradually removing signal overrides, preferring explicit and unique signals instead;
I'm trying to add actions to the context menu that pops up when I right click while editing a cell’s content in a QTableWidget. I tried redefining the contextMenuEvent() method from QTableWidget but it is never called in this context. I also tried to call cellWidget() method on the items, but as I expected it returned None. I'm guessing that a temporary QLineEdit widget is created whenever a cell is in edit mode but couldn't find anything to confirm that.
Is there any way to access the context menu of QTableWidgetItems when in edit mode?
I've looked extensively through the documentation but to no avail.
The context menu that you must handle is the editor provided by the delegate. So in this case a QStyledItemDelegate must be implemented:
from PyQt5 import QtCore, QtGui, QtWidgets
class StyledItemDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = super().createEditor(parent, option, index)
if isinstance(editor, QtWidgets.QLineEdit):
editor.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
editor.setProperty("index", QtCore.QPersistentModelIndex(index))
editor.customContextMenuRequested.connect(self.handle_context_menu)
return editor
def handle_context_menu(self, pos):
editor = self.sender()
if isinstance(editor, QtWidgets.QLineEdit):
index = editor.property("index")
menu = editor.createStandardContextMenu()
action = menu.addAction("New Action")
action.setProperty("index", index)
action.triggered.connect(self.handle_add_new_action_triggered)
menu.exec_(editor.mapToGlobal(pos))
def handle_add_new_action_triggered(self):
action = self.sender()
if isinstance(action, QtWidgets.QAction):
index = action.property("index")
print(index.row(), index.column(), index.data())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
view = QtWidgets.QTableWidget(3, 5)
delegate = StyledItemDelegate(view)
view.setItemDelegate(delegate)
view.resize(640, 480)
view.show()
sys.exit(app.exec_())
In PyQt4 I have a main window which when the settings button is clicked opens the settings dialog
from PyQt4 import QtCore, QtGui
import ui_Design, ui_Settings_Design
class MainDialog(QtGui.QMainWindow, ui_Design.Ui_arbCrunchUI):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
self.settingsBtn.clicked.connect(lambda: self.showSettings())
def showSettings(self):
dialog = QtGui.QDialog()
dialog.ui = SettingsDialog()
dialog.ui.setupUi(dialog)
dialog.exec_()
The above works and my SettingsDialog is displayed but I cant get the setPageIndex to work
class SettingsDialog(QtGui.QDialog, ui_Settings_Design.Ui_SettingsDialog):
def __init__(self, parent=None):
super(SettingsDialog, self).__init__(parent)
self.setupUi(self)
self.bookSettingsBtn.clicked.connect(self.setPageIndex)
#QtCore.pyqtSlot()
def setPageIndex(self):
print 'selected'
self.settingsStackedWidget.setCurrentIndex(0)
The bookSettingsBtn is a QToolButton
self.bookSettingsBtn = QtGui.QToolButton(self.navigationFrame)
And the settingsStackedWidget is a QStackedWidget
self.settingsStackedWidget = QtGui.QStackedWidget(SettingsDialog)
At this point I am pretty confused on signals and slots and nothing I have read clears this up - if anyone can point out what I am doing wrong above and also potentially direct me to a good (beginners) guide / tutorial on signals and slots it would be greatly appreciated
I would also like to know how to make setPageIndex work as follows:
def setPageIndex(self, selection):
self.settingsStackedWidget.setCurrentIndex(selection)
I'm not sure why you're doing the following, but that's the issue:
def showSettings(self):
dialog = QtGui.QDialog()
dialog.ui = SettingsDialog()
dialog.ui.setupUi(dialog)
dialog.exec_()
SettingsDialog itself is a proper QDialog. You don't need to instantiate another QDialog.
Right now, you're creating an empty QDialog and then populate it with the same ui as SettingsDialog (i.e. setupUi(dialog)), then you show this dialog. But... The signal connection is for SettingsDialog, and the dialog you're showing doesn't have that.
Basically, you don't need that extra QDialog at all. The following should be enough:
def showSettings(self):
dialog = SettingsDialog()
dialog.exec_()
Ok. So here is an example how you pass an argument to a slot
from functools import partial
# here you have a button bookSettingsBtn:
self.bookSettingsBtn = QtGui.QPushButton("settings")
self.bookSettingsBtn.clicked.connect(partial(self.setPageIndex, self.bookSettingsBtn.text()))
#pyqtSlot(str) # this means the function expects 1 string parameter (str, str) 2 string parameters etc.
def setPageIndex(self, selection):
print "you pressed button " + selection
In your case the argument would be an int. Of course you have to get the value from somewhere
and then put it in the partial part as the argument (here I just used the text of the button),
but you can use int, bool etc. Just watch the slot signature.
Here is a tutorial that helped me:
http://zetcode.com/gui/pyqt4/
I hope this helps.
Hey here I have a fully running example (just copy paste it in a python file and run it):
Maybe this helps you. It's a small example but here you see how it works.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from functools import partial
class MyForm(QMainWindow):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
button1 = QPushButton('Button 1')
button2 = QPushButton('Button 2')
button1.clicked.connect(partial(self.on_button, button1.text()))
button2.clicked.connect(partial(self.on_button, button1.text()))
layout = QHBoxLayout()
layout.addWidget(button1)
layout.addWidget(button2)
main_frame = QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame)
#pyqtSlot(str)
def on_button(self, n):
print "Text of button is: " + str(n)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = MyForm()
form.show()
app.exec_()
So I dont really understand why but changing the way the settingsDialog is called from the MainWindow has fixed my problem. I guess the parent window needed to be specified??:
class MainDialog(QtGui.QMainWindow, ui_Design.Ui_arbCrunchUI):
....
def showSettings(self):
self.settingsDialog = QtGui.QDialog(self)
self.settingsDialog.ui = SettingsDialog(self)
self.settingsDialog.ui.show()
class SettingsDialog(QtGui.QDialog, ui_Settings_Design.Ui_SettingsDialog):
def __init__(self, parent=None):
super(SettingsDialog, self).__init__(parent)
self.setupUi(self)
self.bookSettingsBtn.clicked.connect(partial(self.setPageIndex, 1))
#QtCore.pyqtSlot(int)
def setPageIndex(self, selection):
self.settingsStackedWidget.setCurrentIndex(selection)
I am using Pyqt however c++ code is fine. I am trying to change a menu item in QSystemTrayIcon using the QT framework in Linux (Ubuntu 11.10). Currently I have tried to reset the QMenu that I initially set:
self.tray = QSystemTrayIcon()
m = QMenu()
m.addAction('First')
m.addAction('Second')
tray.setContextMenu(m)
I place this in my class and make tray a class variable. I was thinking that if I just change the tray to set a new menu it would update:
new_m = QMenu()
new_m.addAction('First')
new_m.addAction('Third')
self.tray.setContextMenu(new_m)
However that doesn't work and the tray menu is still the same as it was initially made. How could I be able to rebuild the menu to change it?
I tested with the following code and it seems to work fine :
from PyQt4.QtGui import *
import sys
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.tray = QSystemTrayIcon(QApplication.style().standardIcon(QStyle.SP_DriveDVDIcon), self)
m = QMenu()
m.addAction('First')
m.addAction('Second')
self.tray.setContextMenu(m)
self.tray.show()
p = QPushButton("test", self)
self.setCentralWidget(p)
p.clicked.connect(self.onClick)
def onClick(self):
new_m = QMenu()
new_m.addAction('First')
new_m.addAction('Third')
self.tray.setContextMenu(new_m)
app = QApplication(sys.argv)
w = MainWindow()
w.show();
sys.exit(app.exec_())
Are you sure there is only one QSystemTrayIcon object ? (In your snippets, there is both self.tray and tray).
For my app I have created a GUI in Qt Designer and converted it into python(2.6) code.
On some of the QPushButton (created with the designer) I want to add a right click context menu. The menu options depend on the application status.
How to implement such a context menu ?
Check if an example below would work for you. The key thing is to set context menu policy for your widget to CustomContextMenu and connect to the widget's customContextMenuRequested signal:
import sys
from PyQt4 import QtGui, QtCore
class MainForm(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
# create button
self.button = QtGui.QPushButton("test button", self)
self.button.resize(100, 30)
# set button context menu policy
self.button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.button.customContextMenuRequested.connect(self.on_context_menu)
# create context menu
self.popMenu = QtGui.QMenu(self)
self.popMenu.addAction(QtGui.QAction('test0', self))
self.popMenu.addAction(QtGui.QAction('test1', self))
self.popMenu.addSeparator()
self.popMenu.addAction(QtGui.QAction('test2', self))
def on_context_menu(self, point):
# show context menu
self.popMenu.exec_(self.button.mapToGlobal(point))
def main():
app = QtGui.QApplication(sys.argv)
form = MainForm()
form.show()
app.exec_()
if __name__ == '__main__':
main()