iT邦幫忙

0

PYQT 想在VIEW 上畫一條線

  • 分享至 

  • xImage



    def handleClearView(self):
        self.view.scene().clear()

class DragButton(QPushButton):

    def mousePressEvent(self, event):
        self.__mousePressPos = None
        self.__mouseMovePos = None
        if event.button() == QtCore.Qt.LeftButton:
            self.__mousePressPos = event.globalPos()
            self.__mouseMovePos = event.globalPos()


    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:

            currPos = self.mapToGlobal(self.pos())
            globalPos = event.globalPos()
            diff = globalPos - self.__mouseMovePos
            newPos = self.mapFromGlobal(currPos + diff)
            self.move(newPos)
            self.__mouseMovePos = globalPos


    def mouseReleaseEvent(self, event):
        if self.__mousePressPos is not None:
            moved = event.globalPos() - self.__mousePressPos 
            if moved.manhattanLength() > 3:
                event.ignore()
                return



class View(QGraphicsView):
    def __init__(self, parent):
        QGraphicsView.__init__(self, parent)
        self.setScene(QGraphicsScene(self))
        self.setSceneRect(QtCore.QRectF(self.viewport().rect()))
        btn1=DragButton('Test1', self)
        btn2=DragButton('Test2', self)

    def mousePressEvent(self, event):
        self._start = event.pos()

    def mouseReleaseEvent(self, event):
        start = QtCore.QPointF(self.mapToScene(self._start))
        end = QtCore.QPointF(self.mapToScene(event.pos()))
        self.scene().addItem(
            QGraphicsLineItem(QtCore.QLineF(start, end)))
        for point in (start, end):
            text = self.scene().addSimpleText(
                '(%d, %d)' % (point.x(), point.y()))
            text.setBrush(QtCore.Qt.red)
            text.setPos(point)

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())
看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2019-04-21 21:52:05 檢舉
把那部分的原始碼拿出來看看。
froce iT邦大師 1 級 ‧ 2019-04-22 08:23:22 檢舉
你view又不是AppForm的其中一員,self.view存取的到你的class View那邊嗎?
froce iT邦大師 1 級 ‧ 2019-04-22 08:26:12 檢舉
self.theview也沒看到你定義啊。
抱歉 已改好了
我讀入的是view
theview 是我之前測試用的 忘記刪掉
這樣寫view讀得進來可以開可是
self.view.scene.addItem(QGraphicsLineItem(QLineF(start,end)))就會錯
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答