SHOW:
|
|
- or go back to the newest paste.
| 1 | import Ui_ImageSort # designed using Qt-Designer | |
| 2 | from PyQt5 import QtCore | |
| 3 | from PyQt5.QtWidgets import QMainWindow, QListWidgetItem | |
| 4 | ||
| 5 | MaskRole = QtCore.Qt.UserRole + 1000 | |
| 6 | ||
| 7 | class ListDelegate(QtWidgets.QStyledItemDelegate): | |
| 8 | def createEditor(self, parent, option, index): | |
| 9 | mask = index.data(MaskRole) | |
| 10 | if mask is not None: | |
| 11 | editor = QtWidgets.QLineEdit(parent) | |
| 12 | editor.setInputMask(mask) | |
| 13 | return editor | |
| 14 | ||
| 15 | def setModelData(self, editor, model, index): | |
| 16 | if editor.hasAcceptableInput(): | |
| 17 | text = editor.text() | |
| 18 | model.setData(index, text, QtCore.Qt.DisplayRole) | |
| 19 | ||
| 20 | re = QtCore.QRegularExpression(r"(\d+)\) (\d+) microns") | |
| 21 | match = re.match(index.data()) | |
| 22 | color = QtGui.QColor("red")
| |
| 23 | if match.hasMatch(): | |
| 24 | val = match.captured(match.lastCapturedIndex()) | |
| 25 | if val != "0": | |
| 26 | color = QtGui.QColor("black")
| |
| 27 | model.setData(index, color, QtCore.Qt.ForegroundRole) | |
| 28 | ||
| 29 | class ImageSort(Ui_ImageSort.Ui_MainWindow, QMainWindow): | |
| 30 | def __init__(self): | |
| 31 | super(ImageSort, self).__init__() | |
| 32 | self.setupUi(self) | |
| 33 | ||
| 34 | self.pushButton_1.clicked.connect(list_view) | |
| 35 | self.pushButton_2.clicked.connect(print_scales) | |
| 36 | ||
| 37 | def list_view(self): | |
| 38 | self.listWidget.clear() | |
| 39 | for i in range(len(self.scale_txt)): | |
| 40 | item = QListWidgetItem(str(self.scale_txt[i])) | |
| 41 | delegate = ListDelegate(self.listWidget) | |
| 42 | item.setData(MaskRole, "1)") | |
| 43 | item.setText("1)" + str(scale) + "microns")
| |
| 44 | item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable) | |
| 45 | item.setForeground(QtCore.Qt.red) | |
| 46 | self.listWidget.addItem(item) | |
| 47 | ||
| 48 | def print_scales(self): | |
| 49 | self.listWidget.clear() | |
| 50 | error = 0 | |
| 51 | for i in range(len(self.scale_txt)): | |
| 52 | if (self.scale_txt[i]==0): | |
| 53 | error = 1 | |
| 54 | ||
| 55 | if (error ==0): | |
| 56 | self.listWidget.addItem("\nThe scale values are confirmed ")
| |
| 57 | for i in range(len(self.scale_txt)): | |
| 58 | print(self.scale_txt[i]) | |
| 59 | print("\n")
| |
| 60 | else: | |
| 61 | self.listWidget.addItem("\nThe scale values have an error")
| |
| 62 | self.listWidget.addItem("Press button 1 again to edit") |