我的问题是self.spdBar.setGeometry不适用于导入的小部件,请有人解释我做错了什么?
这是main.py代码:
from PyQt4 import QtGui, QtCore
import sys
from src.cprg import cPrg
class mainWindow(QtGui.QWidget):
def __init__(self):
self.spdBar = cPrg()
super(mainWindow, self).__init__()
self.initUi()
def initUi(self):
self.spdBar.setGeometry(10,10,10,10)
self.spdBar.setValue(.5)
# self.showFullScreen()
self.setGeometry(100, 100, 800, 480)
self.setWindowTitle('Window Title')
self.show()
def close_app(self):
sys.exit()
def main():
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)
app = QtGui.QApplication(sys.argv)
gui = mainWindow()
gui.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这是自定义小部件py文件:
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import QPen
class cPrg(QtGui.QWidget):
def __init__(self):
super(cPrg, self).__init__()
self.lineWidth = 0
self.setValue(0.0)
def setValue(self, val):
val = float(min(max(val, 0), 1))
self._value = -270 * val
self.update()
def setLineWidth(self, lineWidth):
self.lineWidth = lineWidth
def paintEvent(self, e):
painter = QtGui.QPainter(self)
painter.setRenderHint(painter.Antialiasing)
#outerRadius = min(self.width()/2, self.height()/2)
r = QtCore.QRect(15,15,300,300) #<-- create rectangle
startAngle = 270 * 16 # <-- set start angle to draw arc
endAngle = -270 * 16 # <-- set end arc angle
painter.setPen(QPen(QtGui.QColor('#000000'), 3)) # self.lineWidth)) # <-- arc color
# painter.setBrush(QtCore.Qt.HorPattern)
painter.drawArc(r, startAngle, endAngle) # <-- draw arc
# arc prg
painter.save()
painter.setPen(QPen(QtGui.QColor('#ffffff'), 30))
painter.drawArc(r, startAngle, self._value * 16)
painter.restore()
painter.end()
super(cPrg, self).paintEvent(e)
我猜测paintEvent函数有问题。
更新:
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import QPen
class cPrg(QtGui.QWidget):
def __init__(self, parent=None):
super(cPrg, self).__init__(parent)
self.lineWidth = 0
self.setValue(0.0)
def setValue(self, val):
val = float(min(max(val, 0), 1))
self._value = -270 * val
self.update()
def setLineWidth(self, lineWidth):
self.lineWidth = lineWidth
def paintEvent(self, e):
painter = QtGui.QPainter(self)
painter.setRenderHint(painter.Antialiasing)
# outerRadius = min(self.width()/2, self.height()/2)
r = QtCore.QRect(1, 1, 100, 100) # <-- create rectangle
startAngle = 270 * 16 # <-- set start angle to draw arc
endAngle = -270 * 16 # <-- set end arc angle
painter.setPen(QPen(QtGui.QColor('#000000'), 3)) # self.lineWidth)) # <-- arc color
# painter.setBrush(QtCore.Qt.HorPattern)
painter.drawArc(r, startAngle, endAngle) # <-- draw arc
# arc prg
painter.save()
painter.setPen(QPen(QtGui.QColor('#ffffff'), 30))
painter.drawArc(r, startAngle, self._value * 16)
painter.restore()
painter.end()
super(cPrg, self).paintEvent(e)
class mainWindow(QtGui.QWidget):
def __init__(self):
super(mainWindow, self).__init__()
self.spdBar = cPrg(self)
self.initUi()
def initUi(self):
self.spdBar.setGeometry(10, 10, 500, 500)
self.spdBar.setValue(.5)
# self.hbox = QtGui.QHBoxLayout(self)
# self.hbox.addWidget(self.spdBar)
# self.showFullScreen()
self.setGeometry(100, 100, 800, 480)
self.setWindowTitle('Window Title')
# self.show()
# def close_app(self):
# sys.exit()
编辑: 使用此代码:
rect = e.rect()
r = QtCore.QRect(rect)
size = r.size()
pos = r.center()
r.moveCenter( QtCore.QPoint(pos.x(), pos.y()) )
r.setSize(size*.9)
r.moveCenter(pos)
答案 0 :(得分:1)
您必须更改以下内容:
parent
添加到cPrg
r = QtCore.QRect(15, 15, 300, 300)
则setGeometry(rect)
必须更大。我的解决方案是:
import sys
from PyQt4 import QtCore, QtGui
class cPrg(QtGui.QWidget):
def __init__(self, parent=None):
super(cPrg, self).__init__(parent)
self.lineWidth = 0
self.setValue(0.0)
def setValue(self, val):
val = float(min(max(val, 0), 1))
self._value = -270 * val
self.update()
def setLineWidth(self, lineWidth):
self.lineWidth = lineWidth
def paintEvent(self, e):
painter = QtGui.QPainter(self)
painter.setRenderHint(painter.Antialiasing)
# outerRadius = min(self.width()/2, self.height()/2)
r = QtCore.QRect(15, 15, 300, 300) # <-- create rectangle
startAngle = 270 * 16 # <-- set start angle to draw arc
endAngle = -270 * 16 # <-- set end arc angle
painter.setPen(QtGui.QPen(QtGui.QColor('#000000'), 3)) # self.lineWidth)) # <-- arc color
# painter.setBrush(QtCore.Qt.HorPattern)
painter.drawArc(r, startAngle, endAngle) # <-- draw arc
# arc prg
painter.save()
painter.setPen(QtGui.QPen(QtGui.QColor('#ffffff'), 30))
painter.drawArc(r, startAngle, self._value * 16)
painter.restore()
painter.end()
super(cPrg, self).paintEvent(e)
class mainWindow(QtGui.QWidget):
def __init__(self):
super(mainWindow, self).__init__()
self.spdBar = cPrg(self)
self.initUi()
def initUi(self):
self.spdBar.setGeometry(10, 10, 300, 300)
self.spdBar.setValue(.5)
# self.showFullScreen()
self.setGeometry(100, 100, 800, 480)
self.setWindowTitle('Window Title')
def main():
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)
app = QtGui.QApplication(sys.argv)
gui = mainWindow()
gui.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()