从 QTabWidget 拖放选项卡

Drag and drop tab from QTabWidget

我正在尝试从选项卡小部件中拖动一个选项卡并拆分查看区域(- 就像 eclipse 一样,然后您将选项卡标题拖动到编辑区域中 - 类似于选项卡上的 'split vertically' 功能在 PyCharm)。

我尝试使用拖放但无法让选项卡主区域(例如文本区域)注册来自 QTabBar 的拖动。然后我尝试只跟随鼠标移动并更进一步,但是拆分器代码不是很有效,而且它是非常难看的代码。

注意我没有使用可停靠小部件,因为此代码将用于应用程序的中央小部件。我更喜欢使用拖放来执行此操作 - 有人有任何想法吗?

from PyQt4.QtGui import QWidget, QDrag, QTabBar, QTabWidget, QPainter, QPalette,\
QBrush, QColor, QPen, QVBoxLayout, QHBoxLayout, QTextEdit, QCheckBox
from PyQt4.QtCore import QByteArray, QMimeData, QPoint
from PyQt4 import QtCore, QtGui

class CentralTabWidget(QTabWidget):

    def __init__(self, parent=None):
        QTabWidget.__init__(self, parent)
        self.parent = parent

        tabBar = CentralTabBar(self)
        self.setTabBar(tabBar)
        self.addWidgets()

    def addWidgets(self):
        tab1 = QWidget()
        self.addTab(tab1, "Tab1")
        textArea = QTextEdit()
        textArea.setText("Text area 1")
        vBox = QVBoxLayout()
        vBox.addWidget(textArea)
        tab1.setLayout(vBox)
        tab2 = QWidget()
        self.addTab(tab2, "Tab2")
        textArea2 = QTextEdit()
        textArea2.setText("Text area 2")
        vBox2 = QVBoxLayout()
        vBox2.addWidget(textArea2)
        tab2.setLayout(vBox2)
        self.setAcceptDrops(True)
        self.verticalLineOverlay = Overlay(parent = self)
        self.verticalLineOverlay.hide()

    def dragEnterEvent(self, event):
        mimeData = event.mimeData()
        event.accept()

    def dragMoveEvent(self, event):
        print(">>dragMoveEvent()")

    def dropEvent(self, event):
        mimeData = event.mimeData()
        event.setDropAction(QtCore.Qt.MoveAction)
        event.accept()

    def resizeEvent(self, event):    
        self.verticalLineOverlay.resize(event.size())
        event.accept()

class CentralTabBar(QTabBar):   
    def __init__(self, parent=None):
        QTabBar.__init__(self, parent)
        self.parent = parent
        self.__drag_start_pos = QPoint()
        self.setAcceptDrops(True)

    def mousePressEvent(self, event):
        self.__mousePressPos = None
        self.__mouseMovePos = None
        if event.button() == QtCore.Qt.LeftButton:
            self.__mousePressPos = event.globalPos()
            self.__mouseMovePos = event.globalPos()
            if self.parent.parent.dndCheckBox.isChecked():
                self.startDrag()
        super(CentralTabBar, self).mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        self.parent.verticalLineOverlay.setVisible(False)
        currPos = self.mapToGlobal(self.pos())
        ax, ay, aw, ah = self.geometry().getRect()
        if currPos.x() > ax + aw/4:
            self.parent.parent.createSplitter()
        super(CentralTabBar, self).mouseReleaseEvent(event)

    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)
            xp1, yp1, xp2, yp2 = self.geometry().getCoords()
            ax, ay, aw, ah = self.geometry().getRect()
            parentx, parenty, parentw, parenth = self.parent.geometry().getCoords()
            if parenth > 10:
                if (newPos.y() > yp2) and (newPos.y() < (parenth)):
                    self.parent.verticalLineOverlay.setVisible(True)
        super(CentralTabBar, self).mouseMoveEvent(event)

    def startDrag(self):
        data = QByteArray()
        mimeData = QMimeData()
        mimeData.setData("application/x-icon-and-text", data)
        print("Using DnD")
        drag = QDrag(self)
        drag.setMimeData(mimeData)
        drag.exec_()

    def dragEnterEvent(self, event):
        event.accept()

    def dragMoveEvent(self, event):
        print("--dragMoveEvent()")

    def dropEvent(self, event):
        event.setDropAction(QtCore.Qt.MoveAction)
        event.accept()

class Overlay(QWidget):    
    def __init__(self, parent=None):        
        super(Overlay, self).__init__(parent)
        print("--__init__() parent type:{0}".format(type(parent))) 
        self.parent = parent
        palette = QPalette(self.palette())
        palette.setColor(palette.Background, QtCore.Qt.transparent)
        self.setPalette(palette)

    def paintEvent(self, event):  
        self.painter = QPainter()
        self.painter.setPen(QPen(QtCore.Qt.NoPen))
        self.painter.begin(self)
        self.painter.setRenderHint(QPainter.Antialiasing)
        self.painter.fillRect(self.parent.currentWidget().geometry(), QBrush(QColor(255, 255, 255, 10)))
        parentx, parenty, parentw, parenth = self.parent.geometry().getCoords()
        self.painter.drawRect( parentx, parenty, (parentw/2)-10, parenth)   
        self.painter.drawRect( (parentw/2)+10, parenty, parentw, parenth)        
        self.painter.setPen(QPen(QtCore.Qt.NoPen)) 

    def setUpPainter(self):
        self.painter = QPainter()
        self.painter.setPen(QPen(QtCore.Qt.NoPen))
        self.painter.begin(self)
        self.painter.setRenderHint(QPainter.Antialiasing)
        self.painter.fillRect(self.parent.currentWidget().geometry(), QBrush(QColor(255, 255, 255, 10)))
        parentx, parenty, parentw, parenth = self.parent.geometry().getCoords()

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.layout = QHBoxLayout()
        self.dndCheckBox = QCheckBox("Use DnD")
        self.dndCheckBox.setChecked(True)
        self.ctw = CentralTabWidget(self)
        self.layout.addWidget(self.ctw)
        self.layout.addWidget(self.dndCheckBox)
        self.setLayout(self.layout)

    def createSplitter(self):
        #not sure why widgets are not redrawn inside the splitter
        splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
        self.removeWidgets(self.layout)
        splitter1.addWidget(self.ctw)
        self.layout.addWidget(splitter1)
        self.layout.addWidget(self.dndCheckBox)

    def removeWidgets(self, layout):
        for cnt in reversed(range(layout.count())):
            widget = layout.takeAt(cnt).widget()
            if widget is not None: 
                widget.deleteLater()

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(350, 300)
    window.show()
    sys.exit(app.exec_())

在 QMainMaindow 中使用 QMainWindow 我试过:

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QMainWindow, QTextEdit, QDockWidget

_DOCK_OPTS = QtGui.QMainWindow.AllowNestedDocks
_DOCK_OPTS |= QtGui.QMainWindow.AllowTabbedDocks

class Window(QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        secondQMainWindow = QMainWindow()
        self.central = secondQMainWindow
        self.setDockOptions(_DOCK_OPTS)
        dw1 = QDockWidget("One")
        textArea = QTextEdit()
        textArea.setText("Text area 1")
        dw1.setWidget(textArea)

        dw2 = QDockWidget("Two")
        textArea2 = QTextEdit()
        textArea2.setText("Text area 2")
        dw2.setWidget(textArea2)
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, dw1)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, dw2)
        self.tabifyDockWidget(dw1, dw2)
        dw3 = QDockWidget("Three")
        textArea3 = QTextEdit()
        textArea3.setText("Text area 3")
        dw3.setWidget(textArea3)
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, dw3)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    app.exec_()

但我不认为我在 parent 和 child 中央小部件中添加了正确的小部件。

这是我过去用过的一个技巧,可以满足您的需求。如果您想对参数进行大量控制,它可能会非常有限,但它可以完成工作。

  1. 将第 2 个 QMainWindow 作为 QMainWindow 中的 centralWidget
  2. 不要为第 2 个 QMainWindow 设置任何 centralWidget
  3. 设置标志:QMainWindow.AllowNestedDocksQMainWindow.AllowTabbedDocks

  4. 通过添加 QDockWidgets 添加标签。这些将被自动威胁为 QTabWidgets.

  5. 使用QMainWindow.tabifyDockWidget(firstWidget,secondWidget)以编程方式堆叠小部件(它可以多次用于超过 2 个小部件)

示例:

window=QtGui.QMainWindow()
    window.centralContent=QtGui.QMainWindow()

window.setCentralWidget(window.centralContent)

window.centralContent.firstTabWidget=QtGui.QWidget()
window.centralContent.firstTabDock=QtGui.QDockWidget("first")
window.centralContent.firstTabDock.setWidget(window.centralContent.firstTabWidget)
window.centralContent.addDockWidget(window.centralContent.firstTabDock)
 window.centralContent.secondTabWidget=QtGui.QWidget()
window.centralContent.secondTabDock=QtGui.QDockWidget("second")
window.centralContent.secondTabDock.setWidget(window.centralContent.secondTabWidget)
window.centralContent.addDockWidget(window.centralContent.secondTabDock)

window.centralContent.tabifyDockWidget( window.centralContent.firstTabDock, window.centralContent.secondTabDock)

同时支持选项卡到选项卡和选项卡到其他选项卡-window的选项卡

from PyQt5.QtWidgets import QTabWidget
from PyQt5.QtCore import Qt, QPoint, QMimeData
from PyQt5.QtGui import QPixmap, QRegion, QDrag, QCursor

class TabWidget(QTabWidget):
   def __init__(self, parent=None, new=None):
      super().__init__(parent)
      self.setAcceptDrops(True)
      self.tabBar().setMouseTracking(True)
      self.setMovable(True)
      if new:
         TabWidget.setup(self)

   def __setstate__(self, data):
      self.__init__(new=False)
      self.setParent(data['parent'])
      for widget, tabname in data['tabs']:
         self.addTab(widget, tabname)
      TabWidget.setup(self)

   def __getstate__(self):
      data = {
         'parent' : self.parent(),
         'tabs' : [],
      }
      tab_list = data['tabs']
      for k in range(self.count()):
         tab_name = self.tabText(k)
         widget = self.widget(k)
         tab_list.append((widget, tab_name))
      return data

   def setup(self):
      pass

   def mouseMoveEvent(self, e):
      if e.buttons() != Qt.RightButton:
         return

      globalPos = self.mapToGlobal(e.pos())
      tabBar = self.tabBar()
      posInTab = tabBar.mapFromGlobal(globalPos)
      index = tabBar.tabAt(e.pos())
      tabBar.dragged_content = self.widget(index)
      tabBar.dragged_tabname = self.tabText(index)
      tabRect = tabBar.tabRect(index)

      pixmap = QPixmap(tabRect.size())
      tabBar.render(pixmap,QPoint(),QRegion(tabRect))
      mimeData = QMimeData()

      drag = QDrag(tabBar)
      drag.setMimeData(mimeData)
      drag.setPixmap(pixmap)

      cursor = QCursor(Qt.OpenHandCursor)

      drag.setHotSpot(e.pos() - posInTab)
      drag.setDragCursor(cursor.pixmap(),Qt.MoveAction)
      drag.exec_(Qt.MoveAction)

   def dragEnterEvent(self, e):
      e.accept()
      #self.parent().dragged_index = self.indexOf(self.widget(self.dragged_index))

   def dragLeaveEvent(self,e):
      e.accept()

   def dropEvent(self, e):
      if e.source().parentWidget() == self:
         return

      e.setDropAction(Qt.MoveAction)
      e.accept()
      tabBar = e.source()
      self.addTab(tabBar.dragged_content, tabBar.dragged_tabname)


if __name__ == '__main__':
   from PyQt5.QtWidgets import QWidget, QApplication, QHBoxLayout
   import sys

   class Window(QWidget):
      def __init__(self):

         super().__init__()

         self.dragged_index = None
         tabWidgetOne = TabWidget(self)
         tabWidgetTwo = TabWidget(self)
         tabWidgetOne.addTab(QWidget(), "tab1")
         tabWidgetTwo.addTab(QWidget(), "tab2")

         layout = QHBoxLayout()

         self.moveWidget = None

         layout.addWidget(tabWidgetOne)
         layout.addWidget(tabWidgetTwo)

         self.setLayout(layout)

   app = QApplication(sys.argv)
   window = Window()
   window1 = Window()
   window.show()
   window1.show()
   sys.exit(app.exec_())