假设我有这样的布局:
这样我就可以在左上角添加一个不会太大或太小的小图像,但总是与窗口大小有很好的关系。我怎样才能做到这一点,无论是在设计器中还是在代码中?
我已经发现您似乎可以通过选择布局并将 LayoutStretch
更改为 1,3
之类的内容来执行此操作 - 这在设计器中有效,但是,当我在代码中插入我的图像时,它不尊重它并再次使布局脱离了proption。
我添加了一个担架并使用 QLabel 来显示图像,然后通过 self.LogoLabel.setPixmap(QtGui.QPixmap('res/image.png'))
添加了文件,所以我不明白我需要更改什么才能使图像始终美观且小巧在左上角。
一个测试示例,以防问题不够清楚 - 我需要的图像是 1000x710px 大。
from PyQt5 import QtCore,QtWidgets,QtGui
import sys
class Ui_zebra(object):
def setupUi(self,zebra):
zebra.setobjectname("zebra")
zebra.resize(315,134)
self.centralwidget = QtWidgets.QWidget(zebra)
self.centralwidget.setobjectname("centralwidget")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout_4.setobjectname("verticalLayout_4")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setContentsMargins(-1,-1,0)
self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setobjectname("horizontalLayout")
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setobjectname("verticalLayout_3")
self.LogoLabel = QtWidgets.QLabel(self.centralwidget)
self.LogoLabel.setText("")
self.LogoLabel.setScaledContents(True)
self.LogoLabel.setobjectname("LogoLabel")
self.verticalLayout_3.addWidget(self.LogoLabel)
spacerItem = QtWidgets.QSpacerItem(20,40,QtWidgets.QSizePolicy.Minimum,QtWidgets.QSizePolicy.Expanding)
self.verticalLayout_3.addItem(spacerItem)
self.ComboBox_InputType = QtWidgets.QComboBox(self.centralwidget)
self.ComboBox_InputType.setobjectname("ComboBox_InputType")
self.ComboBox_InputType.addItem("")
self.ComboBox_InputType.addItem("")
self.ComboBox_InputType.addItem("")
self.ComboBox_InputType.addItem("")
self.verticalLayout_3.addWidget(self.ComboBox_InputType)
self.horizontalLayout.addLayout(self.verticalLayout_3)
self.TextInput_Devices = QtWidgets.QPlainTextEdit(self.centralwidget)
self.TextInput_Devices.setobjectname("TextInput_Devices")
self.horizontalLayout.addWidget(self.TextInput_Devices)
self.horizontalLayout.setStretch(0,1)
self.horizontalLayout.setStretch(1,3)
self.verticalLayout_4.addLayout(self.horizontalLayout)
zebra.setCentralWidget(self.centralwidget)
self.menuBar_EnterToken = QtWidgets.Qaction(zebra)
self.menuBar_EnterToken.setobjectname("menuBar_EnterToken")
self.menuBar_TestToken = QtWidgets.Qaction(zebra)
self.menuBar_TestToken.setobjectname("menuBar_TestToken")
self.menuBar_About = QtWidgets.Qaction(zebra)
self.menuBar_About.setobjectname("menuBar_About")
self.retranslateUi(zebra)
QtCore.QMetaObject.connectSlotsByName(zebra)
def retranslateUi(self,zebra):
_translate = QtCore.QCoreApplication.translate
zebra.setWindowTitle(_translate("zebra","zebra"))
self.ComboBox_InputType.setItemText(0,_translate("zebra","ip"))
self.ComboBox_InputType.setItemText(1,"Use all devices"))
self.ComboBox_InputType.setItemText(2,"displayName"))
self.ComboBox_InputType.setItemText(3,"id"))
self.menuBar_EnterToken.setText(_translate("zebra","Enter accesstoken"))
self.menuBar_TestToken.setText(_translate("zebra","Test accesstoken"))
self.menuBar_About.setText(_translate("zebra","About..."))
class Test(QtWidgets.QMainWindow,Ui_zebra):
def __init__(self,parent=None):
super(Test,self).__init__(parent)
self.setupUi(self)
self.LogoLabel.setPixmap(QtGui.QPixmap('res/image.png'))
def main():
app = QtWidgets.QApplication(sys.argv)
form = Test()
form.show()
app.exec_()
if __name__ == '__main__':
main()
编辑:奇怪的是,我找不到关于如何在 QLabel 中使用图像并在更改窗口大小的同时缩放其大小,同时保持纵横比的单个工作示例。这么基础的东西,哪里都找不到?