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_())