python - 在 PyQt5 中对齐弹出窗口小部件

标签 python python-3.x pyqt pyqt5

我在 SO 上看到了很多关于此事的回复,但不是专门针对 QMenu 和 QToolButton 的。希望能提供一些有关将弹出窗口小部件与按钮右侧对齐的指示。这是我正在处理的基本代码..

import sys
from PyQt5.QtWidgets import *


class test(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(200, 100)
        layout = QHBoxLayout(self)
        label = QLabel('Testing QToolButton Popup')
        toolbutton = QToolButton()
        toolbutton.setPopupMode(QToolButton.InstantPopup)

        widget = QWidget()
        widgetLayout = QHBoxLayout(widget)
        widgetLabel = QLabel('Popup Text')
        widgetSpinbox = QSpinBox()
        widgetLayout.addWidget(widgetLabel)
        widgetLayout.addWidget(widgetSpinbox)

        widgetAction = QWidgetAction(toolbutton)
        widgetAction.setDefaultWidget(widget)

        widgetMenu = QMenu(toolbutton)
        widgetMenu.addAction(widgetAction)
        toolbutton.setMenu(widgetMenu)

        layout.addWidget(label)
        layout.addWidget(toolbutton)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = test()
    win.show()
    sys.exit(app.exec_())

结果如下所示:

popup image

最佳答案

Qt 开发人员认为默认位置是正确的,因此如果您想修改对齐方式,则必须移动 QMenu,如下所示:

import sys
from PyQt5.QtCore import QPoint
from PyQt5.QtWidgets import (
    QApplication,
    QHBoxLayout,
    QLabel,
    QMenu,
    QSpinBox,
    QToolButton,
    QWidgetAction,
    QWidget,
)


class Menu(QMenu):
    def showEvent(self, event):
        if self.isVisible():
            button = self.parentWidget()
            if button is not None:
                pos = button.mapToGlobal(button.rect().bottomRight())
                self.move(pos - self.rect().topRight())
        super().showEvent(event)


class Test(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(200, 100)
        layout = QHBoxLayout(self)
        label = QLabel("Testing QToolButton Popup")
        toolbutton = QToolButton(popupMode=QToolButton.InstantPopup)

        widgetLabel = QLabel("Popup Text")
        widgetSpinbox = QSpinBox()

        widget = QWidget()
        widgetLayout = QHBoxLayout(widget)
        widgetLayout.addWidget(widgetLabel)
        widgetLayout.addWidget(widgetSpinbox)

        widgetAction = QWidgetAction(toolbutton)
        widgetAction.setDefaultWidget(widget)

        widgetMenu = Menu(toolbutton)
        widgetMenu.addAction(widgetAction)
        toolbutton.setMenu(widgetMenu)

        layout.addWidget(label)
        layout.addWidget(toolbutton)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Test()
    win.show()
    sys.exit(app.exec_())

关于python - 在 PyQt5 中对齐弹出窗口小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59201657/

相关文章:

python - 如何在现有 csv 文件中写入新行而不将其附加到最后一行?

python - 使用橡皮筋选择 QGraphicsItem

python - Pyqt5 - 网格布局行为不当

python - 在 tf.keras 中正确设置 GAN 实现中的 .trainable 变量

python - 在 Chrome/Firefox 的 Webdriver 中禁用 Cookie

python - 属性错误: 'Rsa' object has no attribute 'n'

python-3.x - 获取异常 : Object type <class 'str' > cannot be passed to C code

python - 如何测试 popplerqt5 中的注释类型?

python - 浅拷贝、深拷贝和普通赋值操作有什么区别?

python - 使用 DataFrame 索引作为 x 轴刻度