在PySide / PyQt中包含QGridLayout的小部件上绘制

时间:2014-09-11 01:41:34

标签: python qt pyqt pyside

我正在制作一个自定义QWidget,其中我有一个QGridLayout,并在网格中的特定元素上绘制一个矩形。我还手动绘制线条来描绘网格元素的位置(使用QPainter.DrawLines)。

绘制线条后,然后在其中一个网格元素中绘制矩形,并使用QGridLayout坐标系指定其位置。

问题是,矩形不会局限于其网格元素。例如,在下面的示例中,蓝色矩形和黑色网格线不对齐,因此我最终在空间中漂浮着一个蓝色框。

我没有通过Google或SO找到对此问题的明确讨论。

修改

请注意,在接受的答案中指出,当我应该使用 point 坐标(即列,排)。也就是说,下面代码中的错误是网格中的元素的x坐标和y坐标相反。


from PySide import QtGui, QtCore

class HighlightSquare(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent=None)
        self.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,
                                       QtGui.QSizePolicy.Expanding))                  
        self.setMinimumSize(self.minimumSizeHint()) 
        layout = QtGui.QGridLayout()
        layout.addItem(QtGui.QSpacerItem(10,10), 0, 0)
        layout.addItem(QtGui.QSpacerItem(10,10), 0, 1)
        layout.addItem(QtGui.QSpacerItem(10,10), 1, 0)
        layout.addItem(QtGui.QSpacerItem(10,10), 1, 1)       
        self.setLayout(layout)
        self.resize(150, 150)
        self.update()

    def paintEvent(self, event = None):
        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        winHeight=self.size().height(); heightStep=winHeight/2
        winWidth=self.size().width(); widthStep=winWidth/2

        #Draw lines
        painter.setPen(QtCore.Qt.black)
        for i in range(4):
            #vertical lines
            painter.drawLine(QtCore.QPoint(i*widthStep,0), QtCore.QPoint(i*widthStep, winHeight))
            #horizontal lines
            painter.drawLine(QtCore.QPoint(0,heightStep*i), QtCore.QPoint(winWidth, heightStep*i))

        #Draw blue outline around box 1,1
        highlightCoordinate=(1,1)
        pen=QtGui.QPen(QtCore.Qt.blue, 3)        
        painter.setPen(pen)
        coordHighlight=[QtCore.QPoint(highlightCoordinate[1]*heightStep, highlightCoordinate[0]*widthStep),\
                        QtCore.QPoint(highlightCoordinate[1]*heightStep, (highlightCoordinate[0]+1)*widthStep),\
                        QtCore.QPoint((highlightCoordinate[1]+1)*heightStep, (highlightCoordinate[0]+1)*widthStep),\
                        QtCore.QPoint((highlightCoordinate[1]+1)*heightStep, highlightCoordinate[0]*widthStep),\
                        QtCore.QPoint(highlightCoordinate[1]*heightStep, highlightCoordinate[0]*widthStep)]
        #print coordHighlight
        painter.drawPolyline(coordHighlight)

    def minimumSizeHint(self):
        return QtCore.QSize(120,120)


if __name__=="__main__":
    import sys
    app=QtGui.QApplication(sys.argv)
    myLight = HighlightSquare()
    myLight.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

您是否已阅读类QtCore.QPoint的构造函数的定义?在方法QPoint.__init__ (self, int xpos, int ypos),您的代码将被撤消(ypos, xpos)。我修好了。

import sys
from PyQt4 import QtGui, QtCore

class QHighlightSquareWidget (QtGui.QWidget):
    def __init__ (self, parent = None):
        QtGui.QWidget.__init__(self, parent = None)
        self.setSizePolicy (
            QtGui.QSizePolicy (
                QtGui.QSizePolicy.Expanding,
                QtGui.QSizePolicy.Expanding))
        self.setMinimumSize(self.minimumSizeHint())
        allQGridLayout = QtGui.QGridLayout()
        allQGridLayout.addItem(QtGui.QSpacerItem(10,10), 0, 0)
        allQGridLayout.addItem(QtGui.QSpacerItem(10,10), 0, 1)
        allQGridLayout.addItem(QtGui.QSpacerItem(10,10), 1, 0)
        allQGridLayout.addItem(QtGui.QSpacerItem(10,10), 1, 1)       
        self.setLayout(allQGridLayout)
        self.resize(150, 150)
        self.update()

    def paintEvent (self, eventQPaintEvent):
        myQPainter = QtGui.QPainter(self)
        myQPainter.setRenderHint(QtGui.QPainter.Antialiasing)
        winHeight = self.size().height()
        heightStep = winHeight / 2
        winWidth  = self.size().width()
        widthStep = winWidth / 2

        myQPainter.setPen(QtCore.Qt.black)
        for i in range(4):
            myQPainter.drawLine(QtCore.QPoint(i * widthStep, 0             ), QtCore.QPoint(i * widthStep, winHeight     ))
            myQPainter.drawLine(QtCore.QPoint(0,             heightStep * i), QtCore.QPoint(winWidth,      heightStep * i))

        highlightCoordinate = (1, 1)
        myQPen = QtGui.QPen(QtCore.Qt.blue, 3)        
        myQPainter.setPen(myQPen)
        coordHighlight = [
            QtCore.QPoint( highlightCoordinate[0]      * widthStep,  highlightCoordinate[1]      * heightStep),
            QtCore.QPoint((highlightCoordinate[0] + 1) * widthStep,  highlightCoordinate[1]      * heightStep),
            QtCore.QPoint((highlightCoordinate[0] + 1) * widthStep, (highlightCoordinate[1] + 1) * heightStep),
            QtCore.QPoint( highlightCoordinate[0]      * widthStep, (highlightCoordinate[1] + 1) * heightStep),
            QtCore.QPoint( highlightCoordinate[0]      * widthStep,  highlightCoordinate[1]      * heightStep)]
        myQPainter.drawPolyline(*coordHighlight)

    def minimumSizeHint (self):
        return QtCore.QSize(120, 120)

if __name__=="__main__":
    myQApplication = QtGui.QApplication(sys.argv)
    myQHighlightSquareWidget = QHighlightSquareWidget()
    myQHighlightSquareWidget.show()
    sys.exit(myQApplication.exec_())