我的目的是在两个QTreeView
之间进行拖放。例如:将项目从本地树视图拖到远程树视图,远程树视图将接受信号并触发一个函数(例如:打印一些东西,所以我知道它成功了)。在
我已经搜索了一些帖子,他们只做了一个QTreeView
,我仍然不清楚如何使用拖放。这是我的完整代码,包括UI。在
在主.py在
# coding=utf-8
__author__ = 'Administrator'
import os, re, sys, time, math
import ConfigParser
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from MainWindow_ui import Ui_MainWindow
import AccountDialog_ui
######## Config ini File Part#######################
def create_conf_ini(ipaddress='', username='', password='', secret='',mode="a+"):
conf = ConfigParser.ConfigParser()
conf.add_section("logininfor")
conf.set("logininfor", "ipaddress", ipaddress)
conf.set("logininfor", "username", username)
conf.set("logininfor", "password", password)
conf.set("logininfor", "secret", secret)
f = open('config.ini', mode)
conf.write(f)
f.close()
### get conf file
def get_infor_from_config():
conf = ConfigParser.ConfigParser()
if os.path.isfile('config.ini'):
conf.read("config.ini")
ipaddress = conf.get("logininfor", "ipaddress")
username = conf.get("logininfor", "username")
password = conf.get("logininfor", "password")
secret = conf.get("logininfor", "secret")
return ipaddress, username, password, secret
else:
create_conf_ini()
return '', '', '', ''
###############################
class S3MiniToolsView(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.actionAccount_Setting.triggered.connect(self.accountsetting_dlg)
self.init_localtreeview()
self.init_remotetreeview()
QtGui.QTreeView.connect(self.remote_Treeview, QtCore.SIGNAL('dropEvent()'), self.additem)
(ipaddress, username, password, secret) = get_infor_from_config()
self.servername_mainw.setText(ipaddress)
self.uid_mainw.setText(username)
self.accesskey_mainw.setText(password)
self.secretkey_mainw.setText(secret)
def init_localtreeview(self):
self.fileSystemModel = QFileSystemModel(self.local_Treeview)
self.fileSystemModel.setReadOnly(False)
#self.fileSystemModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot | QDir.Drives)
root = self.fileSystemModel.setRootPath("")
self.local_Treeview.setModel(self.fileSystemModel)
self.local_Treeview.setRootIndex(root)
self.local_Treeview.setDragDropMode(QtGui.QAbstractItemView.DragOnly)
#QtCore.QObject.connect(self.local_Treeview.selectionModel(), QtCore.SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.test)
#self.local_Treeview.clicked[QtCore.QModelIndex].connect(self.test)
self.local_Treeview.clicked.connect(self.test)
self.local_Treeview.setDragEnabled(True)
def init_remotetreeview(self):
self.remote_Treeview.setDragDropMode(QtGui.QAbstractItemView.DropOnly)
# self.fileSystemModel = QFileSystemModel(self.local_Treeview)
# self.fileSystemModel.setReadOnly(False)
# self.remote_Treeview.setModel(self.fileSystemModel)
self.model = QtGui.QStandardItemModel(self.remote_Treeview)
self.remote_Treeview.setModel(self.model)
self.remote_Treeview.setAcceptDrops(True)
def dragEnterEvent(self, e):
if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()
def dropEvent(self, e):
print 'In dropEvent'
# item = QtGui.QStandardItem("did ok")
# self.model.appendRow(item)
#self.remote_Treeview.(e.mimeData().text())
def additem(self):
item = QtGui.QStandardItem("did ok")
self.model.appendRow(item)
#@QtCore.pyqtSlot("QItemSelection, QItemSelection")
@QtCore.pyqtSlot(QtCore.QModelIndex)
def test(self, index):
indexItem = self.fileSystemModel.index(index.row(), 0, index.parent())
# path or filename selected
fileName = self.fileSystemModel.fileName(indexItem)
# full path/filename selected
filePath = self.fileSystemModel.filePath(indexItem)
print("hello!")
print(fileName)
print(filePath)
def accountsetting_dlg(self):
print "AccountSetting"
accountdlg = QuickConnectDlg(self)
accountdlg.lineEdit_servername.setText( 'ceph-radosgw.lab.com')
accountdlg.lineEdit_accountname.setText( 'johndoe')
accountdlg.lineEdit_accesskey.setText( 'CIXN1L1B42JAYGV6KSIT')
accountdlg.lineEdit_secretkey.setText('17YTAqVBL60StWQniDNWoAH04bScFbjxAxpxNFCg')
accountdlg.lineEdit_httpport.setText( '8080')
accountdlg.lineEdit_httpsport.setText( '443')
if accountdlg.exec_():
servername = accountdlg.lineEdit_servername.text()
accountname = accountdlg.lineEdit_accountname.text()
accesskey = accountdlg.lineEdit_accesskey.text()
secretkey = accountdlg.lineEdit_secretkey.text()
create_conf_ini(servername, accountname, accesskey, secretkey, 'r+')
return
class QuickConnectDlg(QDialog, AccountDialog_ui.Ui_accountDialog):
def __init__(self, parent=None):
super(QuickConnectDlg, self).__init__(parent)
self.setupUi(self)
def main():
app = QApplication(sys.argv)
window = S3MiniToolsView()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
这是主窗口_用户界面在
^{pr2}$帐户对话框_用户界面在
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'AccountDialog.ui'
#
# Created: Mon Aug 25 23:41:07 2014
# by: PyQt4 UI code generator 4.11.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_accountDialog(object):
def setupUi(self, accountDialog):
accountDialog.setObjectName(_fromUtf8("accountDialog"))
accountDialog.resize(400, 248)
self.layoutWidget = QtGui.QWidget(accountDialog)
self.layoutWidget.setGeometry(QtCore.QRect(10, 10, 380, 225))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.gridLayout_3 = QtGui.QGridLayout(self.layoutWidget)
self.gridLayout_3.setMargin(0)
self.gridLayout_3.setObjectName(_fromUtf8("gridLayout_3"))
self.tabWidget_2 = QtGui.QTabWidget(self.layoutWidget)
self.tabWidget_2.setObjectName(_fromUtf8("tabWidget_2"))
self.tabWidgetPage1_2 = QtGui.QWidget()
self.tabWidgetPage1_2.setObjectName(_fromUtf8("tabWidgetPage1_2"))
self.gridLayout_4 = QtGui.QGridLayout(self.tabWidgetPage1_2)
self.gridLayout_4.setObjectName(_fromUtf8("gridLayout_4"))
self.label_7 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_7.setObjectName(_fromUtf8("label_7"))
self.gridLayout_4.addWidget(self.label_7, 0, 0, 1, 1)
self.lineEdit_servername = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_servername.setObjectName(_fromUtf8("lineEdit_servername"))
self.gridLayout_4.addWidget(self.lineEdit_servername, 0, 1, 1, 2)
self.label_8 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_8.setObjectName(_fromUtf8("label_8"))
self.gridLayout_4.addWidget(self.label_8, 1, 0, 1, 1)
self.lineEdit_accountname = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_accountname.setObjectName(_fromUtf8("lineEdit_accountname"))
self.gridLayout_4.addWidget(self.lineEdit_accountname, 1, 1, 1, 2)
self.label_9 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_9.setObjectName(_fromUtf8("label_9"))
self.gridLayout_4.addWidget(self.label_9, 2, 0, 1, 1)
self.lineEdit_accesskey = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_accesskey.setObjectName(_fromUtf8("lineEdit_accesskey"))
self.gridLayout_4.addWidget(self.lineEdit_accesskey, 2, 1, 1, 2)
self.label_10 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_10.setObjectName(_fromUtf8("label_10"))
self.gridLayout_4.addWidget(self.label_10, 3, 0, 1, 1)
self.lineEdit_secretkey = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_secretkey.setObjectName(_fromUtf8("lineEdit_secretkey"))
self.gridLayout_4.addWidget(self.lineEdit_secretkey, 3, 1, 1, 2)
self.label_11 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_11.setObjectName(_fromUtf8("label_11"))
self.gridLayout_4.addWidget(self.label_11, 4, 0, 1, 1)
self.lineEdit_httpport = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_httpport.setObjectName(_fromUtf8("lineEdit_httpport"))
self.gridLayout_4.addWidget(self.lineEdit_httpport, 4, 1, 1, 1)
self.label_12 = QtGui.QLabel(self.tabWidgetPage1_2)
self.label_12.setObjectName(_fromUtf8("label_12"))
self.gridLayout_4.addWidget(self.label_12, 5, 0, 1, 1)
self.lineEdit_httpsport = QtGui.QLineEdit(self.tabWidgetPage1_2)
self.lineEdit_httpsport.setObjectName(_fromUtf8("lineEdit_httpsport"))
self.gridLayout_4.addWidget(self.lineEdit_httpsport, 5, 1, 1, 1)
self.checkBox_https = QtGui.QCheckBox(self.tabWidgetPage1_2)
self.checkBox_https.setObjectName(_fromUtf8("checkBox_https"))
self.gridLayout_4.addWidget(self.checkBox_https, 5, 2, 1, 1)
self.tabWidget_2.addTab(self.tabWidgetPage1_2, _fromUtf8(""))
self.gridLayout_3.addWidget(self.tabWidget_2, 0, 0, 1, 3)
spacerItem = QtGui.QSpacerItem(218, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
self.gridLayout_3.addItem(spacerItem, 1, 0, 1, 1)
self.buttonBox = QtGui.QDialogButtonBox(self.layoutWidget)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
self.gridLayout_3.addWidget(self.buttonBox, 1, 1, 1, 1)
self.retranslateUi(accountDialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), accountDialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), accountDialog.reject)
QtCore.QMetaObject.connectSlotsByName(accountDialog)
def retranslateUi(self, accountDialog):
accountDialog.setWindowTitle(_translate("accountDialog", "Account", None))
self.label_7.setText(_translate("accountDialog", "Server name", None))
self.label_8.setText(_translate("accountDialog", "Account name", None))
self.label_9.setText(_translate("accountDialog", "Access key", None))
self.label_10.setText(_translate("accountDialog", "Secret key", None))
self.label_11.setText(_translate("accountDialog", "HTTP Port", None))
self.lineEdit_httpport.setText(_translate("accountDialog", "8080", None))
self.label_12.setText(_translate("accountDialog", "HTTPS Port", None))
self.lineEdit_httpsport.setText(_translate("accountDialog", "443", None))
self.checkBox_https.setText(_translate("accountDialog", "Connect usring SSL/HTTPS", None))
self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.tabWidgetPage1_2), _translate("accountDialog", "Account", None))
如果您在2个小部件之间交换数据,我建议使用
QTreeWidget
而不是QTreeView
,因为QTreeWidget
中的数据可以编辑动态数据、行和值。在2QTreeWidget
之间的拖放中,我们可以将它们等效为“复制和删除”。在在2
QTreeWidget
之间很容易实现,您只需在QTreeWidget
中拖动enter事件并复制到新的QTreeWidget
时处理。(如果您的内部移动,我将自行删除旧数据)接下来,如果在QTreeWidget
之间有移动数据,则创建您自己的信号来处理它们。最后,将它集成到您的类或小部件中。在示例(不是您的代码,但您可以自己实现),如下图所示
希望是帮助
相关问题 更多 >
编程相关推荐