Last active
October 11, 2018 04:31
-
-
Save boredstiff/f6f2581f0bba2e8e2dd0f0274ce77929 to your computer and use it in GitHub Desktop.
UI Movable data in MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PySide2 import QtCore, QtWidgets, QtGui | |
class Node(QtGui.QStandardItem): | |
def __init__(self): | |
super(Node, self).__init__() | |
self.name = 'My name' | |
class ItemModel(QtGui.QStandardItemModel): | |
def __init__(self, rows, columns, parent=None): | |
super(ItemModel, self).__init__(rows, columns, parent) | |
for row in range(0, 4): | |
node = Node() | |
node.name += str(row) | |
self.setItem(row, node) | |
def data(self, index, role=QtCore.Qt.DisplayRole): | |
if not index.isValid(): | |
return | |
if role == QtCore.Qt.DisplayRole: | |
item = self.itemFromIndex(index) | |
return item.name | |
if role == QtCore.Qt.ForegroundRole: | |
return QtGui.QColor(255, 12, 12) | |
if role == QtCore.Qt.ToolTipRole: | |
item = self.itemFromIndex(index) | |
return 'Tooltip + ' + item.name | |
class ListView(QtWidgets.QListView): | |
def __init__(self, parent=None): | |
super(ListView, self).__init__(parent) | |
model = ItemModel(4, 1, self) | |
self.setModel(model) | |
class Dialog(QtWidgets.QDialog): | |
def __init__(self, parent=None): | |
super(Dialog, self).__init__(parent) | |
layout = QtWidgets.QVBoxLayout() | |
self.setLayout(layout) | |
self.up_button = QtWidgets.QPushButton('Up') | |
self.down_button = QtWidgets.QPushButton('Down') | |
self.up_button.clicked.connect(self.move_up) | |
self.down_button.clicked.connect(self.move_down) | |
layout.addWidget(self.up_button) | |
layout.addWidget(self.down_button) | |
self.view = ListView(self) | |
layout.addWidget(self.view) | |
def move_up(self): | |
row = self.view.currentIndex().row() | |
if row <= 0: | |
return | |
data = self.view.model().takeRow(row) | |
self.view.model().insertRow(row - 1, data) | |
def move_down(self): | |
row = self.view.currentIndex().row() | |
if row == self.view.model().rowCount() - 1: | |
return | |
data = self.view.model().takeRow(row) | |
self.view.model().insertRow(row + 1, data) | |
if __name__ == '__main__': | |
app = QtWidgets.QApplication([]) | |
dialog = Dialog() | |
dialog.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment