Найти - Пользователи
Полная версия: Делегат и qtreeview
Начало » GUI » Делегат и qtreeview
1
Alexey13
Здравствуйте.
Подскажите в чем может быть дело: использую делегата в qtreeview, но он почему то запускается в отдельном окне
Код полностью
[code python]# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPalette, QStandardItemModel, QStandardItem
from PyQt5.QtCore import QModelIndex, Qt

class SpinBoxDDelegate(QStyledItemDelegate):
def createEditor(self, parent, opt, ind):
if ind.row() == 0 and ind.column() == 1:
editor = QSpinBox()
editor.setFrame(False)
editor.setMinimum(1)
editor.setMaximum(3)
editor.setSingleStep(1)
return editor
elif ind.row() == 1 and ind.column() == 1:
editor = QSpinBox()
editor.setFrame(False)
editor.setMinimum(1)
editor.setMaximum(1000)
editor.setSingleStep(5)
return editor
else:
return
def setEditorData(self, editor, ind):
val = int(ind.model().data(ind,Qt.EditRole))
editor.setValue(val)
def updateEditorGeometry(self, editor, opt, ind):
editor.setGeometry(opt.rect)
def setModelData(self, editor, model, ind):
val = str(editor.value())
model.setData(ind,val,Qt.EditRole)

class NM_MainWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(1200, 800)
self.sys_dev = QTreeView(self)
self.sys_dev.resize(600,500)
item11 = QStandardItem('Система')
sti = QStandardItemModel()
item12 = QStandardItem('1')
item21 = QStandardItem('Плотность')
item22 = QStandardItem('36')
item31 = QStandardItem('a0/b0')
item32 = QStandardItem('2')
sti.appendRow([item11, item12])
sti.appendRow([item21, item22])
sti.appendRow([item31, item32])
self.sys_dev.setModel(sti)
self.sys_dev.setItemDelegate(SpinBoxDDelegate())
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mui = NM_MainWindow()
mui.show()
sys.exit(app.exec_())[/code]
Rodegast
 editor = QSpinBox(parent)
Alexey13
Спасибо, все работает!!
Alexey13
Еще возник вопрос
Создаю делегата для отрисовки grid в дереве
 class fraimDelegate(QStyledItemDelegate):
    def paint(self, painter, opt, ind):
        painter.save()
        painter.setPen(Qt.gray)
        x = opt.rect.x()
        y = opt.rect.y()
        h = opt.rect.height()
        w = opt.rect.width()
        if ind.column() == 0:
            w = w + x
            x = 0
        painter.drawRect(x,y,w,h)
        painter.restore()
Рисует рамки нормально, но при этом не отображает содержимое ячеек, как отобразить содержимое?

Rodegast
Добавь в начало
 QStyledItemDelegate.paint(self, painter, opt, ind)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB