问题代码
- 使用设计器随便拖动几个控件,转
.ui
文件成.py
后文件代码
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QFrame, QLineEdit, QPushButton,
QSizePolicy, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(698, 567)
self.frame = QFrame(Form)
self.frame.setObjectName(u"frame")
self.frame.setGeometry(QRect(10, 10, 671, 551))
self.frame.setFrameShape(QFrame.StyledPanel)
self.frame.setFrameShadow(QFrame.Raised)
self.widget = QWidget(self.frame)
self.widget.setObjectName(u"widget")
self.widget.setGeometry(QRect(0, 0, 671, 231))
self.pushButton = QPushButton(self.frame)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(90, 440, 75, 24))
self.pushButton_2 = QPushButton(self.frame)
self.pushButton_2.setObjectName(u"pushButton_2")
self.pushButton_2.setGeometry(QRect(390, 440, 75, 24))
self.lineEdit = QLineEdit(self.frame)
self.lineEdit.setObjectName(u"lineEdit")
self.lineEdit.setGeometry(QRect(270, 300, 113, 21))
self.lineEdit_2 = QLineEdit(self.frame)
self.lineEdit_2.setObjectName(u"lineEdit_2")
self.lineEdit_2.setGeometry(QRect(270, 340, 113, 21))
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
self.pushButton.setText(QCoreApplication.translate("Form", u"PushButton", None))
self.pushButton_2.setText(QCoreApplication.translate("Form", u"PushButton", None))
from PySide6.QtWidgets import *
import tex
import res
import sys
class Mywidget(QWidget,tex.Ui_Form):
def __init__(self) -> None:
super(Mywidget,self).__init__()
self.setupUi(self)
self.setStyleSheet(rf'background-image:url(:R-C.jpg)')
if __name__ == '__main__':
app = QApplication([])
win = Mywidget()
win.show()
sys.exit(app.exec())
- 使用qss设置窗体背景,不显示。
- 终端提示:
Could not create pixmap from :R-C.jpg
- 问题原因未知,如有大佬知晓,请在评论区或者私信告诉我。谢谢
解决办法
- 自定义窗体类的时候继承
QFrame
即可。 - 重写paintEvent方法。
def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
opt = QtWidgets.QStyleOption()
opt.initFrom(self)
painter = QtGui.QPainter(self)
self.style().drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, painter, self)