本文整理汇总了Python中PyQt4.QtSql.QSqlTableModel类的典型用法代码示例。如果您正苦于以下问题:Python QSqlTableModel类的具体用法?Python QSqlTableModel怎么用?Python QSqlTableModel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QSqlTableModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: data
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
column = index.column()
if role == Qt.DisplayRole:
if column == DATAINS:
return QVariant(QSqlTableModel.data(self,
index).toDate().toString(DATEFORMAT))
if column == IMP:
return QVariant("€ %s" %
QSqlTableModel.data(self, index).toString())
if role == Qt.TextAlignmentRole:
if column == DATAINS:
return QVariant(int(Qt.AlignLeft|Qt.AlignVCenter))
else:
return QVariant(int(Qt.AlignRight|Qt.AlignVCenter))
if role == Qt.BackgroundRole:
if column == QT:
if QSqlTableModel.data(self, index).toInt()[0] == 0:
return QVariant(QColor(Qt.red))
# default, no specific condition found
return QSqlTableModel.data(self, index, role)
示例2: __init__
def __init__(self, domainDict, parent=None, db=QSqlDatabase):
"""
Constructor
"""
QSqlTableModel.__init__(self, parent=parent, db=db)
self.dict = domainDict
self.db = db
示例3: __init__
def __init__(self, parent):
# type: (object) -> object
QSqlTableModel.__init__(self, parent)
self.setTable("Register")
self.select()
self.setEditStrategy(QSqlTableModel.OnManualSubmit)
示例4: data
def data(self, index, role):
"""
Custom reimplementation of the method data.
It is necessary to work with value map and value relation.
index: column index
role: role used to get the data
"""
dbdata = QSqlTableModel.data(self, index, role)
column = self.headerData(index.column(), Qt.Horizontal)
if self.dict.has_key(column):
if isinstance(self.dict[column], dict):
valueMap = self.dict[column]
if str(dbdata) in valueMap.values():
id = valueMap.values().index(str(dbdata))
return valueMap.keys()[id]
elif isinstance(self.dict[column], tuple):
tupla = self.dict[column]
valueMap = self.makeValueRelationDict(tupla[0], tupla[1])
codes = str(dbdata)[1:-1].split(',')
code_names = list()
for c in codes:
if str(c) in valueMap.values():
id = valueMap.values().index(str(c))
code_name = valueMap.keys()[id]
code_names.append(code_name)
if len(code_names) > 0:
return '{%s}' % ','.join(code_names)
return dbdata
示例5: get_color_by_option
def get_color_by_option(self, index):
option = QSqlTableModel.data(self, self.index(index.row(), 3)).toString()
if option == 'left':
return QColor(Qt.darkGreen)
if option == 'right':
return QColor(Qt.darkCyan)
return QVariant()
示例6: setData
def setData(self, index, value, role=Qt.EditRole):
"""
Custom reimplementation of the method setData.
It is necessary to work with value map and value relation.
index: column index to be set
value: value to be set
role: role used
"""
column = self.headerData(index.column(), Qt.Horizontal)
newValue = value
if self.dict.has_key(column):
if isinstance(self.dict[column], dict):
valueMap = self.dict[column]
newValue = int(valueMap[value])
elif isinstance(self.dict[column], tuple):
tupla = self.dict[column]
valueMap = self.makeValueRelationDict(tupla[0], tupla[1])
code_names = value[1:-1].split(',')
codes = []
for code_name in code_names:
code = valueMap[code_name]
codes.append(code)
if len(codes) > 0:
newValue = '{%s}' % ','.join(map(str, codes))
return QSqlTableModel.setData(self, index, newValue, role)
示例7: __init__
def __init__(self, parent):
QDialog.__init__(self)
self.parent = parent
self.model = QSqlTableModel(self, DBHandle.default)
self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
self.model.setTable("player")
self.model.setSort(1, 0)
self.model.setHeaderData(1, Qt.Horizontal, QVariant(m18nc("Player", "Name")))
self.model.setFilter('name not like "ROBOT %" and name not like "Robot %"')
self.view = MJTableView(self)
self.view.verticalHeader().show()
self.view.setModel(self.model)
self.view.hideColumn(0)
self.buttonBox = QDialogButtonBox()
self.buttonBox.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.newButton = self.buttonBox.addButton(m18nc('define a new player', "&New"), QDialogButtonBox.ActionRole)
self.newButton.setIcon(KIcon("document-new"))
self.newButton.clicked.connect(self.slotInsert)
self.deleteButton = self.buttonBox.addButton(m18n("&Delete"), QDialogButtonBox.ActionRole)
self.deleteButton.setIcon(KIcon("edit-delete"))
self.deleteButton.clicked.connect(self.delete)
cmdLayout = QHBoxLayout()
cmdLayout.addWidget(self.buttonBox)
layout = QVBoxLayout()
layout.addWidget(self.view)
layout.addLayout(cmdLayout)
self.setLayout(layout)
self.setWindowTitle(m18n("Players") + ' - Kajongg')
self.setObjectName('Players')
示例8: setData
def setData(self, index, value, role=Qt.EditRole):
column = self.headerData(index.column(), Qt.Horizontal)
newValue = value
if self.dict.has_key(column):
dict = self.dict[column]
newValue = int(dict[value])
return QSqlTableModel.setData(self, index, newValue, role)
示例9: __init__
def __init__(self):
super(MainForm, self).__init__()
self.model = QSqlTableModel(self)
self.model.setTable('tbl_symbols')
self.model.select()
self.view = QTableView()
self.view.setModel(self.model)
self.view.horizontalHeader().setResizeMode(QHeaderView.Stretch)
addButton = QPushButton("&Add")
deleteButton = QPushButton("&Delete")
buttonLayout = QVBoxLayout()
buttonLayout.addWidget(addButton)
buttonLayout.addWidget(deleteButton)
buttonLayout.addStretch()
lay = QHBoxLayout()
lay.addWidget(self.view)
lay.addLayout(buttonLayout)
self.setLayout(lay)
self.connect(addButton, SIGNAL("clicked()"), self.addRecord)
self.connect(deleteButton, SIGNAL("clicked()"), self.deleteRecord)
示例10: __init__
def __init__(self, header=None, db=None, parent=None):
super(FilterDialog, self).__init__(parent)
self.setupUi(self)
self.dbi = ""
if not db:
filename = ":memory:"
self.dbi = QSqlDatabase.addDatabase("QSQLITE")
self.dbi.setDatabaseName(filename)
self.dbi.open()
self.creaStrutturaDB(header)
self.sModel = QSqlTableModel(self)
self.sModel.setTable(QString("filtertable"))
self.sModel.setEditStrategy(QSqlTableModel.OnManualSubmit)
self.sModel.select()
self.filterTableView.setModel(self.sModel)
self.filterTableView.setColumnHidden(0, True)
self.filterTableView.setAlternatingRowColors(True)
self.connect(self.sModel, SIGNAL("primeInsert (int,QSqlRecord&)"),
self.updRows)
self.connect(self.addFilterPushButton, SIGNAL("clicked()"),
self.accept)
self.connect(self.addLinePushButton, SIGNAL("clicked()"),
self.addLine)
self.sModel.insertRows(0, 10)
self.sModel.submitAll()
self.restoreSettings()
self.resultFilter = None
示例11: data
def data(self, index, role):
code = QSqlTableModel.data(self, index, role)
column = self.headerData(index.column(), Qt.Horizontal)
if self.dict.has_key(column):
dict = self.dict[column]
if str(code) in dict.values():
id = dict.values().index(str(code))
return dict.keys()[id]
return code
示例12: data
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return QVariant()
column = index.column()
if role == Qt.DisplayRole:
if column == SIMP:
return QVariant("€ %.2f" %
QSqlTableModel.data(self, index).toDouble()[0])
elif column == SIVA:
return QVariant("%.2f %%" %
QSqlTableModel.data(self, index).toDouble()[0])
if role == Qt.TextAlignmentRole:
return QVariant(int(Qt.AlignRight|Qt.AlignVCenter))
# default, no specific condition found
return QSqlTableModel.data(self, index, role)
示例13: data
def data(self, index, role):
if role == Qt.FontRole:
return self.font
if role == Qt.TextAlignmentRole:
return Qt.AlignCenter
if role == Qt.BackgroundColorRole:
return self.get_color_by_option(index)
if index.column() == 2:
value = QSqlTableModel.data(self, index, Qt.EditRole).toInt()[0] == 1
if role == Qt.CheckStateRole:
return Qt.Checked if value else Qt.Unchecked
if role == Qt.EditRole or role == Qt.DisplayRole:
return QVariant(value)
return QVariant()
return QSqlTableModel.data(self, index, role)
示例14: create_widgets
def create_widgets(self):
self.model = QSqlTableModel(self)
self.model.setTable("employee")
self.model.setSort(ID, Qt.AscendingOrder)
self.model.setHeaderData(ID, Qt.Horizontal, QVariant("ID"))
self.model.setHeaderData(CITY, Qt.Horizontal,
QVariant("City"))
self.model.setHeaderData(DEPARTMENT, Qt.Horizontal,
QVariant("Department"))
self.model.setHeaderData(FIRSTNAME, Qt.Horizontal,
QVariant("First Name"))
self.model.setHeaderData(LASTNAME, Qt.Horizontal,
QVariant("Last Name"))
self.model.setHeaderData(SEX, Qt.Horizontal,
QVariant("sex"))
self.model.setHeaderData(ADDRESS, Qt.Horizontal,
QVariant("Address"))
self.model.setHeaderData(PID, Qt.Horizontal,
QVariant("PID"))
self.model.setHeaderData(TITLE, Qt.Horizontal,
QVariant("Title"))
self.model.setHeaderData(SOLARY, Qt.Horizontal,
QVariant("SOLARY"))
self.model.setHeaderData(SHIFT, Qt.Horizontal,
QVariant("SHIFT"))
self.model.setHeaderData(HOURS, Qt.Horizontal,
QVariant("HOURS"))
self.model.select()
self.view = QTableView()
self.view.setModel(self.model)
self.view.setSelectionMode(QTableView.SingleSelection)
self.view.setSelectionBehavior(QTableView.SelectRows)
self.view.setColumnHidden(ID, True)
self.view.resizeColumnsToContents()
self.buttonBox = QDialogButtonBox()
self.addButton = self.buttonBox.addButton(u"&Добавить",
QDialogButtonBox.ActionRole)
self.deleteButton = self.buttonBox.addButton(u"&Удалить",
QDialogButtonBox.ActionRole)
self.sortButton = self.buttonBox.addButton(u"&Сортировать",
QDialogButtonBox.ActionRole)
menu = QMenu(self)
self.sortByTitleAction = menu.addAction(u"Сортировка по &Title")
self.sortBySolaryAction = menu.addAction(
u"Сортировка по &SOLARY")
self.sortByIDAction = menu.addAction(u"Сортировка по &ID")
self.sortButton.setMenu(menu)
self.closeButton = self.buttonBox.addButton(QDialogButtonBox.Close)
示例15: __init__
def __init__(self, name, table, parent=None):
super(DataDialog, self).__init__(parent)
self.name = name
self.resize(600, 300)
self.model = QSqlTableModel(self)
self.model.setTable(table)
self.model.setSort(ID, Qt.AscendingOrder)
self.model.setHeaderData(ID, Qt.Horizontal, QVariant(_("Id")))
self.model.setHeaderData(EMAIL, Qt.Horizontal, QVariant(_("Email")))
self.model.setHeaderData(WWW, Qt.Horizontal, QVariant(_("WWW")))
self.model.setHeaderData(PACKAGE, Qt.Horizontal, QVariant(_("Package")))
self.model.select()
self.view = QTableView()
self.view.setModel(self.model)
self.view.setSelectionMode(QTableView.SingleSelection)
self.view.setSelectionBehavior(QTableView.SelectRows)
# self.view.setColumnHidden(ID, True)
# self.view.resizeColumnsToContents()
addButton = QPushButton(_("&Add"))
addButton.setIcon(QIcon(":/add.png"))
deleteButton = QPushButton(_("&Delete"))
deleteButton.setIcon(QIcon(":/delete.png"))
okButton = QPushButton(_("&OK"))
okButton.setIcon(QIcon(":/quit.png"))
addButton.setFocusPolicy(Qt.NoFocus)
deleteButton.setFocusPolicy(Qt.NoFocus)
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(addButton)
buttonLayout.addWidget(deleteButton)
buttonLayout.addStretch()
buttonLayout.addWidget(okButton)
layout = QVBoxLayout()
layout.addWidget(self.view)
layout.addLayout(buttonLayout)
self.setLayout(layout)
self.connect(addButton, SIGNAL("clicked()"), self.addRecord)
self.connect(deleteButton, SIGNAL("clicked()"), self.deleteRecord)
self.connect(okButton, SIGNAL("clicked()"), self.accept)
self.setWindowTitle(name)