gpt4 book ai didi

drag-and-drop - 从 QTabWidget 拖放选项卡

转载 作者:行者123 更新时间:2023-12-05 02:21:53 26 4
gpt4 key购买 nike

我正在尝试从选项卡小部件中拖动一个选项卡并拆分查看区域(- 就像 eclipse 然后将选项卡标题拖动到编辑区域中一样 - 类似于 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_()

但我认为我没有正确地将小部件添加到父与子中央小部件。

最佳答案

这是我过去用过的一个技巧来完成您正在寻找的事情。如果您想对参数进行大量控制,它可能会非常有限,但它可以完成工作。

  1. 将第二个 QMainWindow 作为 QMainWindowcentralWidget
  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)

关于drag-and-drop - 从 QTabWidget 拖放选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com