使用 pyQt5 自定义 QPrintPreviewDialog
Customizing QPrintPreviewDialog with pyQt5
根据 Question with C++ 的回答,我正在尝试对 PyQt5 做类似的事情。我想删除 QPrintPreviewDialog
的一些按钮并添加新按钮。我可以通过 toolbar = dialog.findChildren(QToolBar)
获取工具栏,通过 buttons = dialog.findChildren(QToolButton)
获取按钮,其中 dialog = QPrintPreviewDialog(printer)
,然后我对按钮进行循环以检查其名称。
代码被截取:
dialog = QPrintPreviewDialog(printer)
buttons = dialog.findChildren(QToolButton)
actions = dialog.findChildren(QAction)
toolbar = dialog.findChildren(QToolBar)
toolbar[0].addAction("PDF", saveaspdf) # This works fine to add a button
# and connection to saveaspdf()
for button in buttons:
print("entry:", button.objectName(), button.text())
if button.text() == 'Portrait' or button.text() == 'Landscape':
button.setDisabled(True) # Button will be disabled
button.setVisible(False) # Button still visible, but disabled
dialog.paintRequested.connect
我尝试使用 toolbar.removeAction()
进行一些操作,但我发现无法识别所要求的操作。我也不认为这是解决方案。此外,任何 .removeWidget(button)
都不能解决问题。
所以,请你解释一下提到的 c++ 页面中的这一行,并给我一个 PyQt 的提示:
//toolbarlist.first()->removeAction(toolbarlist.first()->actions().at(3));
假设您要删除 Portrait
和 Landscape
按钮,那么您必须搜索 QAction 文本并将其从 QToolBar 中删除。
toolbar = dialog.findChild(QToolBar)
ACTIONS_TEXT = [
QCoreApplication.translate("QPrintPreviewDialog", text)
for text in ("Portrait", "Landscape")
]
for action in toolbar.actions():
if action.text() in ACTIONS_TEXT:
toolbar.removeAction(action)
根据 Question with C++ 的回答,我正在尝试对 PyQt5 做类似的事情。我想删除 QPrintPreviewDialog
的一些按钮并添加新按钮。我可以通过 toolbar = dialog.findChildren(QToolBar)
获取工具栏,通过 buttons = dialog.findChildren(QToolButton)
获取按钮,其中 dialog = QPrintPreviewDialog(printer)
,然后我对按钮进行循环以检查其名称。
代码被截取:
dialog = QPrintPreviewDialog(printer)
buttons = dialog.findChildren(QToolButton)
actions = dialog.findChildren(QAction)
toolbar = dialog.findChildren(QToolBar)
toolbar[0].addAction("PDF", saveaspdf) # This works fine to add a button
# and connection to saveaspdf()
for button in buttons:
print("entry:", button.objectName(), button.text())
if button.text() == 'Portrait' or button.text() == 'Landscape':
button.setDisabled(True) # Button will be disabled
button.setVisible(False) # Button still visible, but disabled
dialog.paintRequested.connect
我尝试使用 toolbar.removeAction()
进行一些操作,但我发现无法识别所要求的操作。我也不认为这是解决方案。此外,任何 .removeWidget(button)
都不能解决问题。
所以,请你解释一下提到的 c++ 页面中的这一行,并给我一个 PyQt 的提示:
//toolbarlist.first()->removeAction(toolbarlist.first()->actions().at(3));
假设您要删除 Portrait
和 Landscape
按钮,那么您必须搜索 QAction 文本并将其从 QToolBar 中删除。
toolbar = dialog.findChild(QToolBar)
ACTIONS_TEXT = [
QCoreApplication.translate("QPrintPreviewDialog", text)
for text in ("Portrait", "Landscape")
]
for action in toolbar.actions():
if action.text() in ACTIONS_TEXT:
toolbar.removeAction(action)