Results 1 to 7 of 7

Thread: custom model + PyQt4

  1. #1
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question custom model + PyQt4

    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.

    Qt Code:
    1. #!/usr/bin/env python
    2.  
    3. from PyQt4.QtCore import *
    4. from PyQt4.QtGui import *
    5. from PyQt4.QtSql import *
    6.  
    7. class myModel(QSqlQueryModel):
    8. def __init__(self, parent=None):
    9. QSqlQueryModel.__init__(self)
    10.  
    11. def data(self, index, role):
    12. ?????
    To copy to clipboard, switch view to plain text mode 

    Sorry, my englis is bad

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: custom model + PyQt4

    What's about using Qt::TextAlignmentRole and Qt::BackgroundRole.

  3. #3
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: custom model + PyQt4

    I need example code. And, I mean in Python code. Not C++

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: custom model + PyQt4

    Quote Originally Posted by wirasto View Post
    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
    Qt Code:
    1. if role == Qt.TextAlignmentRole and index.row == 1:
    2. return Qt.AlignRight
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: custom model + PyQt4

    I already have an example here.

    http://www.mail-archive.com/pyqt@riv.../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...

    Qt Code:
    1. def data(self, index, role):
    2. if role == Qt.TextAlignmentRole and index.row == 2:
    3. return Qt.AlignRight
    4. return QVariant()
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Wink Re: custom model + PyQt4

    Someone give me a sample code

    Qt Code:
    1. def data(self, index, role):
    2. value=QSqlQueryModel.data(self, index, role)
    3. if role == Qt.TextAlignmentRole:
    4. if (index.column()==2):
    5. return QVariant(Qt.AlignRight)
    6. return value
    To copy to clipboard, switch view to plain text mode 


    i hope can be useful for others

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: custom model + PyQt4

    Quote Originally Posted by wirasto View Post
    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.

    Quote Originally Posted by wirasto View Post
    Qt Code:
    1. def data(self, index, role):
    2. value=QSqlQueryModel.data(self, index, role)
    3. if role == Qt.TextAlignmentRole:
    4. if (index.column()==2):
    5. return QVariant(Qt.AlignRight)
    6. return value
    To copy to clipboard, switch view to plain text mode 
    For speed and performance issues you should better use:
    python Code:
    1. def data(self, index, role):
    2. if role == Qt.TextAlignmentRole:
    3. if (index.column()==2):
    4. return QVariant(Qt.AlignRight)
    5. return QSqlQueryModel.data(self, index, role)
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to Lykurg for this useful post:

    wirasto (20th July 2009)

Similar Threads

  1. Replies: 12
    Last Post: 5th July 2009, 16:03
  2. custom widget and model
    By asieriko in forum Qt Programming
    Replies: 2
    Last Post: 6th October 2008, 14:11
  3. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  4. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 12:41
  5. Treeview and custom model
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 15th May 2007, 13:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.