在 QWidget 窗口中嵌入多个窗口 | 源码

80次阅读
没有评论

直接上干货,如图: 在

源码如下:

import sys,random
from PyQt5.QtWidgets import QApplication,QWidget,QScrollArea,QFrame,QGridLayout,QVBoxLayout
import PyQt5.QtWebEngineWidgets
from PyQt5.QtCore import Qt,QEvent,QMimeData
from PyQt5.QtGui import QDrag
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas


class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("多窗口浏览工具-靓仔专用 2021/11/13版")
        self.setMinimumSize(900,600)
        self.setAcceptDrops(True)

        self.scrollArea = QScrollArea()
        self.scrollArea.setWidgetResizable(True)

        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        # graph windows continer    
        self.graphContainer = QWidget()
        self.gridLayout = QGridLayout(self.graphContainer)

        self.scrollArea.setWidget(self.graphContainer)
        self.layout.addWidget(self.scrollArea)

        self.creatGraphs()

    def eventFilter(self,obj,event):
        if event.type() == QEvent.MouseButtonPress:
            self.mousePressEvent(event)
        elif event.type()==QEvent.MouseMove:
            self.mouseMoveEvent(event)
        return super().eventFilter(obj,event)

    def mousePressEvent(self,event):
        if event.button() == Qt.LeftButton:
            coord = event.windowPos().toPoint() # QPoint objece
            self.targetIndex = self.getWindowIndex(coord)
        else:
            self.targetIndex = None

    def mouseMoveEvent(self,event):
        if event.buttons() == Qt.LeftButton and self.targetIndex is not None:
            windowItem = self.gridLayout.itemAt(self.targetIndex)

            drag = QDrag(windowItem)

            pix = windowItem.itemAt(0).widget().grab() #qpixmap object

            mimeData = QMimeData()
            mimeData.setImageData(pix)

            drag.setMimeData(mimeData)
            drag.setPixmap(pix)
            drag.setHotSpot(event.pos())
            drag.exec_()

    def dragEnterEvent(self, event) :
        if event.mimeData().hasImage():
            event.accept()
        else:
            event.ignore()

    def dropEvent(self,event):
        if not event.source().geometry().contains(event.pos()):
            targetWindowIndex = self.getWindowIndex(event.pos())
            if targetWindowIndex is None:
                return
            i,j = max(self.targetIndex, targetWindowIndex),min(self.targetIndex,targetWindowIndex)
            p1,p2 = self.gridLayout.getItemPosition(i),self.gridLayout.getItemPosition(j) #(row index,column index, width size, col size)

            self.gridLayout.addItem(self.gridLayout.takeAt(i),*p2)
            self.gridLayout.addItem(self.gridLayout.takeAt(j),*p1)



    def getWindowIndex(self,pos):
        for i in range(self.gridLayout.count()):
            if self.gridLayout.itemAt(i).geometry().contains(pos):
                return i

    def creatGraphs(self):
        tracker = 0

        for c in range(3):
            for r in range(3):
                frame = QFrame()
                frame.setStyleSheet('background-color: white;')
                frame.setFrameStyle(QFrame.Panel | QFrame.Raised)

                frameContainer = QVBoxLayout(frame)

                chatDta = [random.random() for i in range(9)]
                tracker +=1

                # create matplatlib  graph
                figure = Figure()
                canvas = FigureCanvas(figure) # pyqt5 widget
                ax = figure.add_subplot() 
                ax.plot(chatDta,'-')
                figure.suptitle('Chrat #{0}'.format(tracker))
                canvas.draw()

                # TODO:Drag and Drop feature
                canvas.installEventFilter(self)
                frameContainer.addWidget(canvas)

                box = QVBoxLayout()
                box.addWidget(frame)

                self.gridLayout.addLayout(box,r,c)
                self.gridLayout.setColumnStretch(c%3,1)
                self.gridLayout.setRowStretch(r,1)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    myApp = MyApp()
    myApp.show()
    try:
        sys.exit(app.exec_())
    except SystemExit:
        print("App is closing")

迷鹿, lostfawn
版权声明:本站原创文章,由迷鹿, lostfawn2021-11-13发表,共计2999字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
载入中...