本文整理汇总了Python中PyQt5.QtWidgets.QTreeView.setDragDropMode方法的典型用法代码示例。如果您正苦于以下问题:Python QTreeView.setDragDropMode方法的具体用法?Python QTreeView.setDragDropMode怎么用?Python QTreeView.setDragDropMode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QTreeView
的用法示例。
在下文中一共展示了QTreeView.setDragDropMode方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DirectoriesDialog
# 需要导入模块: from PyQt5.QtWidgets import QTreeView [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeView import setDragDropMode [as 别名]
#.........这里部分代码省略.........
label = QLabel(tr("Application Mode:"), self)
label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
hl.addWidget(label)
self.appModeRadioBox = RadioBox(
self,
items=[tr("Standard"), tr("Music"), tr("Picture")],
spread=False
)
hl.addWidget(self.appModeRadioBox)
self.verticalLayout.addLayout(hl)
hl = QHBoxLayout()
hl.setAlignment(Qt.AlignLeft)
label = QLabel(tr("Scan Type:"), self)
label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
hl.addWidget(label)
self.scanTypeComboBox = QComboBox(self)
self.scanTypeComboBox.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed))
self.scanTypeComboBox.setMaximumWidth(400)
hl.addWidget(self.scanTypeComboBox)
self.showPreferencesButton = QPushButton(tr("More Options"), self.centralwidget)
self.showPreferencesButton.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
hl.addWidget(self.showPreferencesButton)
self.verticalLayout.addLayout(hl)
self.promptLabel = QLabel(tr("Select folders to scan and press \"Scan\"."), self.centralwidget)
self.verticalLayout.addWidget(self.promptLabel)
self.treeView = QTreeView(self.centralwidget)
self.treeView.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.treeView.setSelectionBehavior(QAbstractItemView.SelectRows)
self.treeView.setAcceptDrops(True)
triggers = QAbstractItemView.DoubleClicked | QAbstractItemView.EditKeyPressed\
| QAbstractItemView.SelectedClicked
self.treeView.setEditTriggers(triggers)
self.treeView.setDragDropOverwriteMode(True)
self.treeView.setDragDropMode(QAbstractItemView.DropOnly)
self.treeView.setUniformRowHeights(True)
self.verticalLayout.addWidget(self.treeView)
self.horizontalLayout = QHBoxLayout()
self.removeFolderButton = QPushButton(self.centralwidget)
self.removeFolderButton.setIcon(QIcon(QPixmap(":/minus")))
self.removeFolderButton.setShortcut("Del")
self.horizontalLayout.addWidget(self.removeFolderButton)
self.addFolderButton = QPushButton(self.centralwidget)
self.addFolderButton.setIcon(QIcon(QPixmap(":/plus")))
self.horizontalLayout.addWidget(self.addFolderButton)
spacerItem1 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.loadResultsButton = QPushButton(self.centralwidget)
self.loadResultsButton.setText(tr("Load Results"))
self.horizontalLayout.addWidget(self.loadResultsButton)
self.scanButton = QPushButton(self.centralwidget)
self.scanButton.setText(tr("Scan"))
self.scanButton.setDefault(True)
self.horizontalLayout.addWidget(self.scanButton)
self.verticalLayout.addLayout(self.horizontalLayout)
self.setCentralWidget(self.centralwidget)
self._setupActions()
self._setupMenu()
if self.app.prefs.directoriesWindowRect is not None:
self.setGeometry(self.app.prefs.directoriesWindowRect)
else:
moveToScreenCenter(self)
def _setupColumns(self):
header = self.treeView.header()
示例2: OpenedFileExplorer
# 需要导入模块: from PyQt5.QtWidgets import QTreeView [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeView import setDragDropMode [as 别名]
class OpenedFileExplorer(DockWidget):
"""Opened File Explorer is list widget with list of opened files.
It implements switching current file, files sorting. Uses _OpenedFileModel internally.
Class instance created by Workspace.
"""
def __init__(self, workspace):
DockWidget.__init__(self, workspace, "&Opened Files", QIcon(":/enkiicons/filtered.png"), "Alt+O")
self._workspace = workspace
self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.tvFiles = QTreeView(self)
self.tvFiles.setHeaderHidden(True)
self.tvFiles.setEditTriggers(QAbstractItemView.SelectedClicked)
self.tvFiles.setContextMenuPolicy(Qt.CustomContextMenu)
self.tvFiles.setDragEnabled(True)
self.tvFiles.setDragDropMode(QAbstractItemView.InternalMove)
self.tvFiles.setRootIsDecorated(False)
self.tvFiles.setTextElideMode(Qt.ElideMiddle)
self.tvFiles.setUniformRowHeights(True)
self.tvFiles.customContextMenuRequested.connect(self._onTvFilesCustomContextMenuRequested)
self.setWidget(self.tvFiles)
self.setFocusProxy(self.tvFiles)
self.model = _OpenedFileModel(self) # Not protected, because used by Configurator
self.tvFiles.setModel(self.model)
self.tvFiles.setAttribute(Qt.WA_MacShowFocusRect, False)
self.tvFiles.setAttribute(Qt.WA_MacSmallSize)
self._workspace.currentDocumentChanged.connect(self._onCurrentDocumentChanged)
# disconnected by startModifyModel()
self.tvFiles.selectionModel().selectionChanged.connect(self._onSelectionModelSelectionChanged)
self.tvFiles.activated.connect(self._workspace.focusCurrentDocument)
core.actionManager().addAction("mView/aOpenedFiles", self.showAction())
# Add auto-hide capability.
self._waitForCtrlRelease = False
core.actionManager().action("mNavigation/aNext").triggered.connect(
self._setWaitForCtrlRelease)
core.actionManager().action("mNavigation/aPrevious").triggered.connect(
self._setWaitForCtrlRelease)
QApplication.instance().installEventFilter(self)
def terminate(self):
"""Explicitly called destructor
"""
core.actionManager().removeAction("mView/aOpenedFiles")
QApplication.instance().removeEventFilter(self)
def startModifyModel(self):
"""Blocks signals from model while it is modified by code
"""
self.tvFiles.selectionModel().selectionChanged.disconnect(self._onSelectionModelSelectionChanged)
def finishModifyModel(self):
"""Unblocks signals from model
"""
self.tvFiles.selectionModel().selectionChanged.connect(self._onSelectionModelSelectionChanged)
@pyqtSlot(Document, Document)
def _onCurrentDocumentChanged(self, oldDocument, currentDocument): # pylint: disable=W0613
""" Current document has been changed on workspace
"""
if currentDocument is not None:
index = self.model.documentIndex(currentDocument)
self.startModifyModel()
self.tvFiles.setCurrentIndex(index)
# scroll the view
self.tvFiles.scrollTo(index)
self.finishModifyModel()
@pyqtSlot(QItemSelection, QItemSelection)
def _onSelectionModelSelectionChanged(self, selected, deselected): # pylint: disable=W0613
""" Item selected in the list. Switch current document
"""
if not selected.indexes(): # empty list, last file closed
return
index = selected.indexes()[0]
# backup/restore current focused widget as setting active mdi window will steal it
focusWidget = self.window().focusWidget()
# set current document
document = self._workspace.sortedDocuments[index.row()]
self._workspace.setCurrentDocument(document)
# restore focus widget
if focusWidget:
focusWidget.setFocus()
@pyqtSlot(QPoint)
def _onTvFilesCustomContextMenuRequested(self, pos):
#.........这里部分代码省略.........
示例3: OpenedFileExplorer
# 需要导入模块: from PyQt5.QtWidgets import QTreeView [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeView import setDragDropMode [as 别名]
class OpenedFileExplorer(DockWidget):
"""Opened File Explorer is list widget with list of opened files.
It implements switching current file, files sorting. Uses _OpenedFileModel internally.
Class instance created by Workspace.
"""
def __init__(self, workspace):
DockWidget.__init__(self, workspace, "&Opened Files", QIcon(":/enkiicons/filtered.png"), "Alt+O")
self._workspace = workspace
self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.tvFiles = QTreeView(self)
self.tvFiles.setHeaderHidden(True)
self.tvFiles.setEditTriggers(QAbstractItemView.SelectedClicked)
self.tvFiles.setContextMenuPolicy(Qt.CustomContextMenu)
self.tvFiles.setDragEnabled(True)
self.tvFiles.setDragDropMode(QAbstractItemView.InternalMove)
self.tvFiles.setRootIsDecorated(False)
self.tvFiles.setTextElideMode(Qt.ElideMiddle)
self.tvFiles.setUniformRowHeights(True)
self.tvFiles.customContextMenuRequested.connect(self._onTvFilesCustomContextMenuRequested)
self.setWidget(self.tvFiles)
self.setFocusProxy(self.tvFiles)
self.model = _OpenedFileModel(self) # Not protected, because used by Configurator
self.tvFiles.setModel(self.model)
self.tvFiles.setAttribute(Qt.WA_MacShowFocusRect, False)
self.tvFiles.setAttribute(Qt.WA_MacSmallSize)
self._workspace.currentDocumentChanged.connect(self._onCurrentDocumentChanged)
# disconnected by startModifyModel()
self.tvFiles.selectionModel().selectionChanged.connect(self._onSelectionModelSelectionChanged)
self.tvFiles.activated.connect(self._workspace.focusCurrentDocument)
core.actionManager().addAction("mView/aOpenedFiles", self.showAction())
def terminate(self):
"""Explicitly called destructor
"""
core.actionManager().removeAction("mView/aOpenedFiles")
def startModifyModel(self):
"""Blocks signals from model while it is modified by code
"""
self.tvFiles.selectionModel().selectionChanged.disconnect(self._onSelectionModelSelectionChanged)
def finishModifyModel(self):
"""Unblocks signals from model
"""
self.tvFiles.selectionModel().selectionChanged.connect(self._onSelectionModelSelectionChanged)
@pyqtSlot(Document, Document)
def _onCurrentDocumentChanged(self, oldDocument, currentDocument): # pylint: disable=W0613
""" Current document has been changed on workspace
"""
if currentDocument is not None:
index = self.model.documentIndex(currentDocument)
self.startModifyModel()
self.tvFiles.setCurrentIndex(index)
# scroll the view
self.tvFiles.scrollTo(index)
self.finishModifyModel()
@pyqtSlot(QItemSelection, QItemSelection)
def _onSelectionModelSelectionChanged(self, selected, deselected): # pylint: disable=W0613
""" Item selected in the list. Switch current document
"""
if not selected.indexes(): # empty list, last file closed
return
index = selected.indexes()[0]
# backup/restore current focused widget as setting active mdi window will steal it
focusWidget = self.window().focusWidget()
# set current document
document = self._workspace.sortedDocuments[index.row()]
self._workspace.setCurrentDocument(document)
# restore focus widget
if focusWidget:
focusWidget.setFocus()
@pyqtSlot(QPoint)
def _onTvFilesCustomContextMenuRequested(self, pos):
"""Connected automatically by uic
"""
menu = QMenu()
menu.addAction(core.actionManager().action("mFile/mClose/aCurrent"))
menu.addAction(core.actionManager().action("mFile/mSave/aCurrent"))
menu.addAction(core.actionManager().action("mFile/mReload/aCurrent"))
menu.addSeparator()
menu.addAction(core.actionManager().action("mFile/mFileSystem/aRename"))
#.........这里部分代码省略.........
示例4: DirectoriesDialog
# 需要导入模块: from PyQt5.QtWidgets import QTreeView [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeView import setDragDropMode [as 别名]
class DirectoriesDialog(QMainWindow):
def __init__(self, app, **kwargs):
super().__init__(None, **kwargs)
self.app = app
self.lastAddedFolder = platform.INITIAL_FOLDER_IN_DIALOGS
self.recentFolders = Recent(self.app, "recentFolders")
self._setupUi()
self.directoriesModel = DirectoriesModel(self.app.model.directory_tree, view=self.treeView)
self.directoriesDelegate = DirectoriesDelegate()
self.treeView.setItemDelegate(self.directoriesDelegate)
self._setupColumns()
self.app.recentResults.addMenu(self.menuLoadRecent)
self.app.recentResults.addMenu(self.menuRecentResults)
self.recentFolders.addMenu(self.menuRecentFolders)
self._updateAddButton()
self._updateRemoveButton()
self._updateLoadResultsButton()
self._setupBindings()
def _setupBindings(self):
self.scanButton.clicked.connect(self.scanButtonClicked)
self.loadResultsButton.clicked.connect(self.actionLoadResults.trigger)
self.addFolderButton.clicked.connect(self.actionAddFolder.trigger)
self.removeFolderButton.clicked.connect(self.removeFolderButtonClicked)
self.treeView.selectionModel().selectionChanged.connect(self.selectionChanged)
self.app.recentResults.itemsChanged.connect(self._updateLoadResultsButton)
self.recentFolders.itemsChanged.connect(self._updateAddButton)
self.recentFolders.mustOpenItem.connect(self.app.model.add_directory)
self.directoriesModel.foldersAdded.connect(self.directoriesModelAddedFolders)
self.app.willSavePrefs.connect(self.appWillSavePrefs)
def _setupActions(self):
# (name, shortcut, icon, desc, func)
ACTIONS = [
("actionLoadResults", "Ctrl+L", "", tr("Load Results..."), self.loadResultsTriggered),
("actionShowResultsWindow", "", "", tr("Results Window"), self.app.showResultsWindow),
("actionAddFolder", "", "", tr("Add Folder..."), self.addFolderTriggered),
]
createActions(ACTIONS, self)
def _setupMenu(self):
self.menubar = QMenuBar(self)
self.menubar.setGeometry(QRect(0, 0, 42, 22))
self.menuFile = QMenu(self.menubar)
self.menuFile.setTitle(tr("File"))
self.menuView = QMenu(self.menubar)
self.menuView.setTitle(tr("View"))
self.menuHelp = QMenu(self.menubar)
self.menuHelp.setTitle(tr("Help"))
self.menuLoadRecent = QMenu(self.menuFile)
self.menuLoadRecent.setTitle(tr("Load Recent Results"))
self.setMenuBar(self.menubar)
self.menuFile.addAction(self.actionLoadResults)
self.menuFile.addAction(self.menuLoadRecent.menuAction())
self.menuFile.addSeparator()
self.menuFile.addAction(self.app.actionQuit)
self.menuView.addAction(self.app.actionPreferences)
self.menuView.addAction(self.actionShowResultsWindow)
self.menuView.addAction(self.app.actionIgnoreList)
self.menuHelp.addAction(self.app.actionShowHelp)
self.menuHelp.addAction(self.app.actionOpenDebugLog)
self.menuHelp.addAction(self.app.actionAbout)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuView.menuAction())
self.menubar.addAction(self.menuHelp.menuAction())
# Recent folders menu
self.menuRecentFolders = QMenu()
self.menuRecentFolders.addAction(self.actionAddFolder)
self.menuRecentFolders.addSeparator()
# Recent results menu
self.menuRecentResults = QMenu()
self.menuRecentResults.addAction(self.actionLoadResults)
self.menuRecentResults.addSeparator()
def _setupUi(self):
self.setWindowTitle(self.app.NAME)
self.resize(420, 338)
self.centralwidget = QWidget(self)
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.promptLabel = QLabel(tr('Select folders to scan and press "Scan".'), self.centralwidget)
self.verticalLayout.addWidget(self.promptLabel)
self.treeView = QTreeView(self.centralwidget)
self.treeView.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.treeView.setSelectionBehavior(QAbstractItemView.SelectRows)
self.treeView.setAcceptDrops(True)
triggers = (
QAbstractItemView.DoubleClicked | QAbstractItemView.EditKeyPressed | QAbstractItemView.SelectedClicked
)
self.treeView.setEditTriggers(triggers)
self.treeView.setDragDropOverwriteMode(True)
self.treeView.setDragDropMode(QAbstractItemView.DropOnly)
self.treeView.setUniformRowHeights(True)
self.verticalLayout.addWidget(self.treeView)
self.horizontalLayout = QHBoxLayout()
self.removeFolderButton = QPushButton(self.centralwidget)
self.removeFolderButton.setIcon(QIcon(QPixmap(":/minus")))
#.........这里部分代码省略.........