PyQt:如何在缩放模式下设置样式表内容?

PyQt: How can i set stylesheet contents on scaled mode?

我有一个应用程序,其中包含许多带有样式表的小部件,但是,我没有向界面添加任何布局,它也没有包含中央小部件,但是该应用程序 运行 没有任何问题.

然而,每当我尝试调整应用程序的大小(缩小它)时,小部件当然不会缩放。

我进行了一些研究(因为我找不到与我的问题相关的任何其他内容),我在 Qt 文档、样式表参考中找到了这个:

"The actual image that is drawn is determined using the same algorithm as QIcon (i.e) the image is never scaled up but always scaled down if necessary."

如何使用 window 缩小样式表? (如果样式表有背景图片)


例如我有带有样式表的按钮:

btn = QtGui.QPushButton(self)
btn.move(0, 0)
btn.setObjectName('btn)
btn.setStyleSheet("#btn {background-image: url(':/images/somepicture.png'); border: none; }")

如何使用 window 缩小此按钮,我可以在没有布局的情况下实现吗?如果不是,我怎么能用布局来做呢? (没有限制太多)

如果您将按钮作为中央小部件添加到 QMainWindow,它应该会自动调整其大小以适应可用的 space。但是,要使按钮图像缩放,您需要将图像设置为 border-image 样式表 属性(有点奇怪)。 PyQt4 的工作示例:

from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        btn = QtGui.QPushButton(self)
        btn.setStyleSheet("border-image: url('somepicture.png');")  # Scaled
        #btn.setStyleSheet("background-image: url('somepicture.png');")  # Not scaled

        self.setCentralWidget(btn)

        self.show()

app = QtGui.QApplication([])
window = MainWindow()

app.exec_()

请注意,您无需设置 id (objectName) 即可将 CSS 分配给特定的小部件,您只需通过 CSS 规则传递 .setStyleSheet().

您不能在 QMainWindow 上设置布局,因为它已经有一个复杂的布局系统来容纳停靠小部件和工具栏。因此,如果你想使用一个布局来向window添加多个widget,你需要使用一个容器widget来容纳它。以下工作示例演示了这一点:

from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        w = QtGui.QWidget() # container widget
        l = QtGui.QVBoxLayout() # your layout
        w.setLayout(l) # set the layout on your container widget

        btn = QtGui.QPushButton(self)
        btn.setStyleSheet("border-image: url('somepicture.png');")      

        label = QtGui.QLabel('Hello!')

        l.addWidget(btn) # add your widget to the layout
        l.addWidget(label) # add the label to the layout

        self.setCentralWidget(w) # add the container widget to the QMainWindow        

        self.show()

app = QtGui.QApplication([])
window = MainWindow()

app.exec_()

如果您希望能够绝对定位小部件,而不是将它们添加到布局(这将控制它们 size/position),您可以传递父元素(相对于采用的 x,y 坐标) 创建时:

from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        w = QtGui.QWidget() # container widget

        btn = QtGui.QPushButton(w)
        btn.move(100,100)
        btn.setStyleSheet("border-image: url('somepicture.png');")      

        self.setCentralWidget(w) # add the container widget to the QMainWindow        
        self.show()

app = QtGui.QApplication([])
window = MainWindow()

app.exec_()

但是完全像这样放置小部件会使您失去自动缩放它以适应父小部件的能力。如果您只想在 window 中的元素周围添加一些 padding/spacing,请查看 QLayouts 中的 .setContentsMargins,例如l.setContentsMargins(50,50,50,50) 将在按钮周围留出 50 像素的边距。