import sys from PyQt5 import QtCore from PyQt5 QtCore import Qt QRegEx

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import sys
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, QRegExp, pyqtSignal, QEvent
from PyQt5.QtGui import QPalette, QColor, QRegExpValidator, QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import (QWidget,
QVBoxLayout, QApplication, QTableView, QHeaderView, QAbstractItemView, QSizePolicy,
QHBoxLayout, QGridLayout, QCheckBox, QComboBox, QItemDelegate, QPushButton, QLineEdit,
QLabel)
class ComboBox(QComboBox):
# clicked = pyqtSignal()
def __init__(self, parent, view, model, delegate, form):
super(ComboBox, self).__init__(parent=parent)
self.parent = parent
self._view = view
self.model = model
self.delegate = delegate
self.form = form
def hideEvent(self, event):
self.delegate.editor.setCurrentIndex(self.delegate.editor.currentIndex())
self.delegate.commitData.emit(self.sender())
self._view.closePersistentEditor(self.model.index(self._view.currentIndex().row(), 3))
self.form.search.setFocus()
class ComboDelegate(QItemDelegate):
def __init__(self, view, model, parent):
super(QItemDelegate, self).__init__()
self._view = view
self.model = model
self.editor = None
self.parent = parent
# Создаем QComboBox
def createEditor(self, parent, option, ind):
data = ind.sibling(ind.row(), 0).data()
self.editor = ComboBox(parent, self._view, self.model, self, self.parent)
self.editor.addItem("2")
self.editor.addItem("1")
self.editor.setFocusPolicy(Qt.NoFocus)
self.editor.installEventFilter(self)
self.editor.activated.connect(self.current_index_changed)
return self.editor
def setEditorData(self, editor, index):
self.editor.showPopup()
def editorEvent(self, event, model, options, index):
if event.type() == QEvent.MouseButtonRelease:
if event.button() == Qt.LeftButton:
view = options.widget
view.emit(setCurrentIndex(index))
view.emit(edit(index))
return super(ComboDelegate, self).editorEvent(event, model, options, index)
def current_index_changed(self, index):
self.commitData.emit(self.sender())
self._view.closePersistentEditor(self.model.index(self._view.currentIndex().row(), 3))
class MyTableView(QTableView):
clicked = pyqtSignal()
def __init__(self, form):
self.form = form
super(MyTableView, self).__init__()
def mouseReleaseEvent(self, event):
for row in range(self.form.model.rowCount()):
self.closePersistentEditor(self.form.model.index(row, 3))
self.closePersistentEditor(self.form.model.index(row, 2))
if self.currentIndex().column() == 2:
self.openPersistentEditor(
self.form.model.index(self.currentIndex().row(), 2)
)
elif self.currentIndex().column() == 3:
self.openPersistentEditor(
self.form.model.index(self.currentIndex().row(), 3)
)
else:
QTableView.mouseReleaseEvent(self, event)
def mouseDoubleClickEvent(self, e):
if self.currentIndex().column() != 3 or self.currentIndex().column() != 2:
e.ignore()
else:
QTableView.mouseDoubleClickEvent(self, e)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.selectAll = QCheckBox(self)
self.selectAll.setFocusPolicy(Qt.NoFocus)
self.table = MyTableView(self)
self.CloseButton = QPushButton(self)
self.CloseButton.setFocusPolicy(Qt.NoFocus)
self.print = QPushButton(self)
self.print.setFocusPolicy(Qt.NoFocus)
self.type_stickers = QComboBox(self)
self.type_stickers.setFocusPolicy(Qt.NoFocus)
self.search = QLineEdit(self)
self.search_label = QLabel()
self.search_label.setFocusPolicy(Qt.NoFocus)
self.search.setFocusPolicy(Qt.ClickFocus)
self.setup_stickers_form()
self.setWindowFlags(QtCore.Qt.Dialog)
self.search_dict = dict()
QtCore.QMetaObject.connectSlotsByName(self)
Checkable = False
self.model = QStandardItemModel(100, 4)
for i in range(100):
item = QStandardItem("item {}".format(i))
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setCheckable(True)
if Checkable:
item.setCheckState(Qt.Checked)
else:
item.setCheckState(Qt.Unchecked)
self.model.setItem(i, 0, item)
item = QStandardItem("name {}".format(i))
self.model.setItem(i, 1, item)
item = QStandardItem("product count {}".format(i))
self.model.setItem(i, 2, item)
item = QStandardItem("product_barcode {}".format(i))
self.model.setItem(i, 3, item)
# self.search_dict[e["product_code"]] = i
# self.model.itemChanged.connect(self.item_changed)
self.model.setHeaderData(0, Qt.Horizontal, "Артикул")
self.model.setHeaderData(1, Qt.Horizontal, "Номенклатура")
self.model.setHeaderData(2, Qt.Horizontal, "Количество\nстикеров")
self.model.setHeaderData(3, Qt.Horizontal, "Штрихкод")
self.table.setModel(self.model)
self.table.setItemDelegateForColumn(3, ComboDelegate(self.table, self.model, self))
self.table.setColumnWidth(0, 120)
self.table.setColumnWidth(2, 80)
self.table.setColumnWidth(3, 120)
self.table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
self.show()
def events(self):
self.selectAll.clicked.connect(self.check_check_box_select_all)
self.CloseButton.clicked.connect(self.closed)
self.print.clicked.connect(self.generate_query)
self.search.textEdited.connect(self.method_search)
self.search.returnPressed.connect(self.clear_search)
self.initial_data(True)
def setup_stickers_form(self):
self.resize(1078, 768)
self.setMinimumSize(1078, 768)
self.setMaximumSize(1078, 768)
self.selectAll.setChecked(True)
col = QPalette(QColor(237, 225, 139))
self.setPalette(col)
self.setFocusPolicy(Qt.NoFocus)
self.setWindowModality(QtCore.Qt.WindowModal)
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)
self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
self.table.setSelectionMode(QAbstractItemView.SingleSelection)
self.table.setAlternatingRowColors(True)
self.table.setFocusPolicy(Qt.NoFocus)
valid = QRegExpValidator(QRegExp("^[1-9][0-9]{0,7}$"), self)
self.search.setValidator(valid)
self.search_label.setText("Поиск артикула:")
self.search_label.setMinimumSize(110, 26)
self.search_label.setMaximumSize(110, 26)
self.CloseButton.setText("Закрыть")
size_policy = QSizePolicy(
QSizePolicy.Fixed, QSizePolicy.Fixed
)
self.CloseButton.setSizePolicy(size_policy)
self.CloseButton.setFocusPolicy(Qt.NoFocus)
self.print.setSizePolicy(size_policy)
self.print.setText("Печать")
self.print.setFocusPolicy(Qt.NoFocus)
self.selectAll.setText("Выбрать все/ снять все")
self.type_stickers.setFocusPolicy(Qt.NoFocus)
self.type_stickers.addItem("")
self.type_stickers.addItem("Обычный стикер")
self.type_stickers.addItem("СЕТИ (со сроком годности)")
self.type_stickers.addItem("СЕТИ (Стикер на коробку)")
self.type_stickers.addItem("ТС ВЕРНЫЙ (печать стикера)")
self.type_stickers.addItem("ТС ВЕРНЫЙ (печать стикера (фасовка))")
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.selectAll)
horizontal_layout = QHBoxLayout()
horizontal_layout.addLayout(vertical_layout)
horizontal_layout.addWidget(self.search_label)
horizontal_layout.addWidget(self.search)
vertical_layout_2 = QVBoxLayout()
vertical_layout_2.addLayout(horizontal_layout)
vertical_layout_2.addWidget(self.table)
horizontal_layout_2 = QHBoxLayout()
horizontal_layout_2.addWidget(self.type_stickers)
horizontal_layout_2.addWidget(self.print)
horizontal_layout_2.addWidget(self.CloseButton)
grid_layout = QGridLayout()
grid_layout.addLayout(vertical_layout_2, 0, 0, 1, 1)
grid_layout.addLayout(horizontal_layout_2, 1, 0, 1, 1)
grid_layout_2 = QGridLayout(self)
grid_layout_2.addLayout(grid_layout, 0, 0, 1, 1)
return self
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())