[英]PyQT - Positioning and Displaying a Custom Widget
我試圖使用PyQT來定位和顯示自定義小部件。 到目前為止,我有一個小部件和我的窗口。 我已成功通過布局顯示小部件,但是,我有興趣使用.move(x,y,)在show()之前定位我的小部件。 到目前為止,我的代碼如下:
import sys, random
from PyQt4 import QtGui, QtCore
# Robot Widget
class RobotLink(QtGui.QWidget):
def __init__(self, parent, x, y, width, height, fill):
super(RobotLink, self).__init__(parent)
self._x = x
self._y = y
self._width = width
self._height = height
self._fill = fill
self._rotation = 0
def paintEvent(self, e):
painter = QtGui.QPainter()
painter.begin(self)
self.drawLink(painter)
painter.end()
def drawLink(self, painter):
painter.setPen(QtGui.QColor(0, 0, 0))
painter.setBrush(self._fill)
painter.drawEllipse(self._x, self._y, self._width, self._height)
# Window
class Window(QtGui.QWidget):
# Default Constructor, sets up the window GUI
def __init__(self):
super(Window, self).__init__()
self.initUI()
def initUI(self):
self._link1 = RobotLink(self, 225, 400, 30, 150, QtCore.Qt.DiagCrossPattern)
self._link2 = RobotLink(self, 0, 320, 30, 100, QtCore.Qt.Dense5Pattern)
self._link3 = RobotLink(self, 225, 260, 30, 75, QtCore.Qt.Dense2Pattern)
self._link1.move(0, 0)
self._link1.show()
self.setGeometry(300, 300, 800, 600)
self.setWindowTitle("CSCE 452 - PaintBot")
def paintEvent(self, e):
super(Window, self).paintEvent(e)
painter = QtGui.QPainter()
painter.begin(self)
self.drawBoundingBoxes(painter)
painter.end()
# Draws the boxes that define the robots workspace and
# the control panel
def drawBoundingBoxes(self, painter):
color = QtGui.QColor(0, 0, 0)
color.setNamedColor("#cccccc")
painter.setPen(color)
# Draw the robot workspace
painter.setBrush(QtGui.QColor(255, 255, 255))
painter.drawRect(10, 10, 500, 578)
# Draw the control panel workspace
painter.setBrush(QtGui.QColor(150, 150, 150))
painter.drawRect(520, 10, 270, 578)
# Draws the slider 'base'
painter.setPen(QtGui.QColor(0, 0, 0))
painter.drawLine(100, 570, 400, 570)
def changeValue(self, value):
self.wid.emit(QtCore.SIGNAL("updateRobot(int)"), value)
self.wid.repaint()
# Setup the Window, and the Robot
app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
app.exec_()
任何想法如何附加我的小部件,沒有布局,定位它並讓它顯示在我的窗口內?
我在你的代碼中注意到的一些事情:
您實際上不需要為自定義小部件定義商店x,y,寬度和高度。 只需使用傳遞給構造函數的坐標調用setGeometry即可 。 Widget已經提供了getGeometry,getHeight,getWidth等方法,您可以使用這些方法來操作和繪制組件。
當您在子窗口小部件drawLink方法中調用drawEllipse時,您將x和y坐標作為矩形開頭傳遞給函數。 根據我的理解,你應該把0,0作為那些坐標應該是widget相對而不是窗口相對。
我對您的代碼進行了一些更改,看看它是否適合您
import sys, random
from PyQt4 import QtGui, QtCore
# Robot Widget
class RobotLink(QtGui.QWidget):
def __init__(self, parent, x, y, width, height, fill):
super(RobotLink, self).__init__(parent)
self._fill = fill
self._rotation = 0
self.setGeometry(x, y, width, height)
def paintEvent(self, e):
painter = QtGui.QPainter()
painter.begin(self)
self.drawLink(painter)
painter.end()
def drawLink(self, painter):
painter.setPen(QtGui.QColor(0, 0, 0))
painter.setBrush(self._fill)
painter.drawEllipse(0, 0, self.width(), self.height())
# Window
class Window(QtGui.QWidget):
# Default Constructor, sets up the window GUI
def __init__(self):
super(Window, self).__init__()
self.initUI()
def initUI(self):
self._link1 = RobotLink(self, 10, 10, 100, 50, QtCore.Qt.DiagCrossPattern)
self._link2 = RobotLink(self, 100, 100, 50, 100, QtCore.Qt.Dense5Pattern)
self._link3 = RobotLink(self, 150, 150, 50, 50, QtCore.Qt.Dense2Pattern)
self.setGeometry(300, 300, 800, 600)
self.setWindowTitle("CSCE 452 - PaintBot")
def paintEvent(self, e):
super(Window, self).paintEvent(e)
painter = QtGui.QPainter()
painter.begin(self)
self.drawBoundingBoxes(painter)
painter.end()
# Draws the boxes that define the robots workspace and
# the control panel
def drawBoundingBoxes(self, painter):
color = QtGui.QColor(0, 0, 0)
color.setNamedColor("#cccccc")
painter.setPen(color)
# Draw the robot workspace
painter.setBrush(QtGui.QColor(255, 255, 255))
painter.drawRect(10, 10, 500, 578)
# Draw the control panel workspace
painter.setBrush(QtGui.QColor(150, 150, 150))
painter.drawRect(520, 10, 270, 578)
# Draws the slider 'base'
painter.setPen(QtGui.QColor(0, 0, 0))
painter.drawLine(100, 570, 400, 570)
def changeValue(self, value):
self.wid.emit(QtCore.SIGNAL("updateRobot(int)"), value)
self.wid.repaint()
# Setup the Window, and the Robot
app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
app.exec_()
希望這有幫助,問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.