有人知道一种简单的方法,而无需添加其他偶数句柄来调整大小,提供所选图标的比例列表或将尺寸限制为最大尺寸吗? PS:我也研究了QPixmap-仍然存在相同的问题.我忘了提到一种发现自己想要做的事情的方法(但是结果并不像我想要的那样漂亮)-使用样式表中的 image 属性QPushbutton.但是,这不会创建图标!如果真的不需要真正的图标,而只需要使用绘画按钮,则使用此属性可以在调整大小方面提供极大的灵活性,尤其是在使用SVG时.@div的注释中,解决方案子类QPushButton似乎是解决您的问题的合理选择.下面,我提供一个简单的示例,说明如何在PySide中完成此操作.import sysfrom PySide import QtGui, QtCoreclass myContainter(QtGui.QWidget): def __init__(self, parent=None): super(myContainter, self).__init__(parent) icon = QtGui.QIcon('process-stop.png') grid = QtGui.QGridLayout() for i in range(3): button = myPushButton() button.setIcon(icon) grid.addWidget(button, i, 0) grid.setRowStretch(i, i) self.setLayout(grid)class myPushButton(QtGui.QPushButton): def __init__(self, label=None, parent=None): super(myPushButton, self).__init__(label, parent) self.pad = 4 # padding between the icon and the button frame self.minSize = 8 # minimum size of the icon sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) self.setSizePolicy(sizePolicy) def paintEvent(self, event): qp = QtGui.QPainter() qp.begin(self) #---- get default style ---- opt = QtGui.QStyleOptionButton() self.initStyleOption(opt) #---- scale icon to button size ---- Rect = opt.rect h = Rect.height() w = Rect.width() iconSize = max(min(h, w) - 2 * self.pad, self.minSize) opt.iconSize = QtCore.QSize(iconSize, iconSize) #---- draw button ---- self.style().drawControl(QtGui.QStyle.CE_PushButton, opt, qp, self) qp.end()if __name__ == '__main__': app = QtGui.QApplication(sys.argv) instance = myContainter() instance.show() sys.exit(app.exec_())这将导致:图标的最大尺寸受QIcon中用作输入的png的尺寸限制.如果将svg用作QIcon的输入,则图标的缩放比例将不受大小限制.但是,Windows 7中似乎不支持svg图标,而Ubuntu中则支持.如果在按钮上添加标签,则需要扩展上面的代码.此外,如果需要,还可以将标签的字体大小缩放到按钮大小.I'm currently struggling with what should be an easy issue to solve. Many widgets support some sort of QSizePolicy. This includes the QPushbutton. In my case I have multiple buttons in a grid layout all of which have their QSizePolicy for both vertical and horizontal resizing set to expanding. This leads to the nead result of the buttons being resized according to the size of the widget which the grid layout is part of.The problem comes from the way icons seem to be handled in Qt. QIcon does not have a QSizePolicy property (or at least I was unable to find one in the official documentation of Qt4 about QIcon and QAbstractButton). The only way seems to be using setIconSize() where you can give the maximum size of the icon. In addition one has to manually implement a routine on how the size is to be updated. In this case it would be (abstract writing here) icon.size == button.size-CONSTANT, where CONSTANT is some kind of a predefined factor (>= 0). Predefining various sizes (a list of QIcons) for the selected icon is also possible but still not a good option (read below why).This seems to be a little bit of an overkill especially since QPushbutton supports QSizePolicy and the developer doesn't have to tinker in this departement at all unless he/she wants some special resizing going on. Also this sort of contradicts the support for SVG files that can be used in QIcon since, as we know, SVG = vector graphics = you can stretch those as much as you like without loss of quality.Does anyone know an easy way to do that without the need to add additional even handles for resizing, providing a list of scales of the chosen icon or restricting the size to a maximum size?PS: I have also looked into QPixmap - still the same issues there.EDIT:I forgot to mention one way I found out how to do what I wanted (the results are however not as pretty as I want them to be) - using the image property in the stylesheet for QPushbutton. This however does not create an icon! If one doesn't really require a real icon and can just use a painted button using this property allows a huge flexibility in terms of resizing especially when using SVG. 解决方案 Sub-classing QPushButton, as suggested by @Pavel in a comment, seems like a reasonable option to solve your issue. Below I provide a simple example that shows how this can be done in PySide.import sysfrom PySide import QtGui, QtCoreclass myContainter(QtGui.QWidget): def __init__(self, parent=None): super(myContainter, self).__init__(parent) icon = QtGui.QIcon('process-stop.png') grid = QtGui.QGridLayout() for i in range(3): button = myPushButton() button.setIcon(icon) grid.addWidget(button, i, 0) grid.setRowStretch(i, i) self.setLayout(grid)class myPushButton(QtGui.QPushButton): def __init__(self, label=None, parent=None): super(myPushButton, self).__init__(label, parent) self.pad = 4 # padding between the icon and the button frame self.minSize = 8 # minimum size of the icon sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) self.setSizePolicy(sizePolicy) def paintEvent(self, event): qp = QtGui.QPainter() qp.begin(self) #---- get default style ---- opt = QtGui.QStyleOptionButton() self.initStyleOption(opt) #---- scale icon to button size ---- Rect = opt.rect h = Rect.height() w = Rect.width() iconSize = max(min(h, w) - 2 * self.pad, self.minSize) opt.iconSize = QtCore.QSize(iconSize, iconSize) #---- draw button ---- self.style().drawControl(QtGui.QStyle.CE_PushButton, opt, qp, self) qp.end()if __name__ == '__main__': app = QtGui.QApplication(sys.argv) instance = myContainter() instance.show() sys.exit(app.exec_())Which results in:The maximum size of the icon is limited by the size of the png used as input in QIcon. If a svg is used as input for the QIcon, the scaling of the icon won't be limited in size. However, svg icons seems not to be supported in Windows7, but they are in Ubuntu.The code above would need to be expanded if a label is added to the button. Moreover, it would be possible to also scale the font size of the label to the button size if desired. 这篇关于动态调整QIcon的大小,而无需调用setSizeIcon()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 13:40