PDA

View Full Version : custom model + PyQt4



wirasto
19th July 2009, 20:40
I want create my custom model. But i confused with the code.

I want set text aligment some column and if in a cell have value =0, cell will color red.



#!/usr/bin/env python

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *

class myModel(QSqlQueryModel):
def __init__(self, parent=None):
QSqlQueryModel.__init__(self)

def data(self, index, role):
?????


Sorry, my englis is bad

Lykurg
19th July 2009, 20:51
What's about using Qt::TextAlignmentRole and Qt::BackgroundRole.

wirasto
19th July 2009, 21:01
I need example code. And, I mean in Python code. Not C++

Lykurg
19th July 2009, 21:19
I need example code.
Oh yes, sorry that I don't have developed your application. My fault. I am terribly sorry, because we are dealing here with high end programming stuff.


And, I mean in Python code. Not C++
Why certainly!

And here a gentile: rtfm. And is it so heavy to transform c++ in python? From Qt::TextAlignmentRole to Qt.TextAlignmentRole? And is it not possible to think about

if role == Qt.TextAlignmentRole and index.row == 1:
return Qt.AlignRight

If so, I strongly recommend the Newbie section of that board...

wirasto
20th July 2009, 03:23
I already have an example here.

http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17197.html

And your sample, the same as that in there. But unfortunately code like that do not display data. Therefore, I ask in here. Sorry...


def data(self, index, role):
if role == Qt.TextAlignmentRole and index.row == 2:
return Qt.AlignRight
return QVariant()

wirasto
20th July 2009, 06:48
Someone give me a sample code


def data(self, index, role):
value=QSqlQueryModel.data(self, index, role)
if role == Qt.TextAlignmentRole:
if (index.column()==2):
return QVariant(Qt.AlignRight)
return value


i hope can be useful for others

Lykurg
20th July 2009, 09:42
And your sample, the same as that in there. But unfortunately code like that do not display data.

That's because it is only a part out of the whole function. You have to respect the other roles by yourself or - as below - call the base class.



def data(self, index, role):
value=QSqlQueryModel.data(self, index, role)
if role == Qt.TextAlignmentRole:
if (index.column()==2):
return QVariant(Qt.AlignRight)
return value
For speed and performance issues you should better use:
def data(self, index, role):
if role == Qt.TextAlignmentRole:
if (index.column()==2):
return QVariant(Qt.AlignRight)
return QSqlQueryModel.data(self, index, role)