Skip to content

Instantly share code, notes, and snippets.

@fereria
Created February 4, 2015 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fereria/c7d23941e23b394cb31c to your computer and use it in GitHub Desktop.
Save fereria/c7d23941e23b394cb31c to your computer and use it in GitHub Desktop.
Clipboard Add ListView
## -*- coding: utf-8 -*-
import sys
from PySide import QtCore, QtGui
from PySide.QtUiTools import QUiLoader
class clipbordCopy(QtGui.QDialog):
def __init__(self):
super(clipbordCopy,self).__init__(None)
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
self.view = QtGui.QListView()
self.view.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
layout.addWidget(self.view)
self.model = QtGui.QStandardItemModel()
self.view.setModel(self.model)
#右クリックでMenu
self.view.setContextMenuPolicy( QtCore.Qt.CustomContextMenu )
self.view.customContextMenuRequested.connect(self.copyMenu)
def copyMenu(self,pos):
"""
View上で右クリックした時に表示されるMenu
"""
Menu = QtGui.QMenu(self)
Paste = QtGui.QAction("Paste",self,triggered=self.copyClipboard)
Menu.addAction(Paste)
Delete = QtGui.QAction("Delete",self,triggered=self.deleteMenu)
Menu.addAction(Delete)
Menu.exec_(self.view.mapToGlobal(pos))
def deleteMenu(self):
"""
選択しているItemを削除
"""
indexList = self.model.persistentIndexList()
for i in indexList:
self.model.removeRow(i.row())
def copyClipboard(self):
"""
クリップボードの内容をListに追加
"""
cb = QtGui.QClipboard()
buff = cb.text().split("\n")
for i in buff:
item = QtGui.QStandardItem(i)
self.model.appendRow(item)
app = QtGui.QApplication(sys.argv)
a = clipbordCopy()
a.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment