2014-09-11 112 views
0

我正在製作一個定製的QWidget,其中包含一個QGridLayout,並在網格中的特定元素上繪製一個矩形。我還手動繪製線條來描繪網格元素的位置(使用QPainter.DrawLines)。在包含PySide/PyQt中的QGridLayout的小部件上繪製

繪製線條後,我畫了一個網格元素中的矩形,其位置使用QGridLayout座標系指定。

問題是,該矩形不會限制在其網格元素。例如,在下面的例子中,藍色的矩形和黑色的網格線不對齊,所以我最終得到一個藍色的框在空間中漂浮。

我還沒有找到通過Google或SO對此問題的明確討論。

編輯:

注意作爲公認的答案中指出,錯誤使用電網座標繪製在網格上,當我應該使用座標(即,列了,行)。也就是說,下面代碼中的錯誤是網格中的元素的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

你讀過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_()) 
相關問題