Форум сайта python.su
Есть данная таблица.Требуется напротив каждой строки в конце поставить кнопку,для просмотра/редактирования содержимого.Я так понял,легче это сделать в самой таблице отдельной ячейкой/кнопкой.Вот как реализовать,не могу понять.Если есть ссылочка на пример,плиз,ткните носом.:)Спасибо.
# -*- coding: cp1251 -*-
import sys
import MySQLdb as mdb
from PyQt4 import QtGui, QtCore, QtSql
class MyWindow(QtGui.QWidget):
def __init__(self, *args):
QtGui.QWidget.__init__(self, *args)
# create table
table = FreezeTableWidget(self)
# layout
layout = QtGui.QVBoxLayout()
layout.addWidget(table)
self.setLayout(layout)
class FreezeTableWidget(QtGui.QTableView):
def __init__(self, parent = None, *args):
QtGui.QTableView.__init__(self, parent, *args)
# set the table model
tm = MyTableModel(self)
# set the proxy model
pm = QtGui.QSortFilterProxyModel(self)
pm.setSourceModel(tm)
# назначаем модель данных для TableView
self.setModel(pm)
class MyTableModel(QtCore.QAbstractTableModel):
def __init__(self, parent = None, *args):
QtCore.QAbstractTableModel.__init__(self, parent, *args)
self.colLabels = [ u'Фамилия',u'Имя', u'Отчество', u'Домашний адрес', u'Номер телефона'] # Заголовки столбцов
conn = mdb.connect('localhost', 'root', '', 'test',use_unicode=True, charset='cp1251');
cursor = conn.cursor()
conn.set_character_set('cp1251')
cursor.execute('SET NAMES cp1251')
cursor.execute('SET CHARACTER SET cp1251')
cursor.execute('SET character_set_connection=cp1251')
cursor.execute('select * from users ')
a = []
for row in cursor.fetchall():
a += [row[2], row[1], row[3], row[4], row[5]],
conn.commit()
cursor.close ()
conn.close()
self.dataCached = a
def rowCount(self, parent):
return len(self.dataCached)
def columnCount(self, parent):
return len(self.colLabels)
def get_value(self, index):
i = index.row()
j = index.column()
return self.dataCached[i][j]
def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()
value = self.get_value(index)
if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
return QtCore.QVariant(value)
elif role == QtCore.Qt.TextAlignmentRole:
return QtCore.QVariant(QtCore.Qt.AlignCenter)
return QtCore.QVariant()
def headerData(self, section, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
header = QtCore.QVariant(self.colLabels[section])
return header
if orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
return QtCore.QVariant("%s" % str(section + 1))
return QtCore.QVariant()
Офлайн