Results 1 to 5 of 5

Thread: Set textAlignment on all the items [QSqlTableModel]

  1. #1
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Question Set textAlignment on all the items [QSqlTableModel]

    I know such question like this already [asked,answered]!, But the main topic was on the specific item
    So let just explain it:
    I already subclass the QSqlTableModel and reimplement the data() , but how can i set all of the items aligment to AlignCenter ?
    Also i found some links about it, but agin they argued abot specific item
    http://www.mentby.com/nesbitt/qtable...n-problem.html
    http://lists.trolltech.com/qt-intere...ad00349-0.html

    Thanks ;)
    ...یه مرد هیچوقت زمین نمیخوره

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

    Default Re: Set textAlignment on all the items [QSqlTableModel]

    Where is the problem? If you have a solution for a specific case, then applying it for all isn't normally a problem. The solution is on the trolltech list, last entry. Reimp the data method and return AlignCenter for all items.

  3. #3
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Set textAlignment on all the items [QSqlTableModel]

    Now i get confused!
    i change the code to:
    Qt Code:
    1. QVariant BillyModel::data(const QModelIndex &idx, int role) const
    2. {
    3. QVariant value = QSqlTableModel::data(idx, role);
    4. if(role == Qt::TextAlignmentRole)
    5. return Qt::AlignCenter;
    6. if(value.isValid() && role == Qt::DisplayRole){
    7. if(idx.column() == id)
    8. return value.toString().prepend("#");
    9. }
    10. return value;
    11. }
    To copy to clipboard, switch view to plain text mode 
    And now works!

    How data() works?, i mean is it possible to return multivalue at the time or what?
    Because when soething lik ethis happen
    Qt Code:
    1. return BillyWilly;
    To copy to clipboard, switch view to plain text mode 
    The function shoulda return BillyWilly and stop , in this situation data() return
    first:

    Qt Code:
    1. if(role == Qt::TextAlignmentRole)
    2. return Qt::AlignCenter;
    To copy to clipboard, switch view to plain text mode 
    Second:
    Qt Code:
    1. if(idx.column() == id)
    2. return value.toString().prepend("#");
    To copy to clipboard, switch view to plain text mode 

    But i'm sure something like this couldn't possible, and i misunderstood how the data() works!
    ...یه مرد هیچوقت زمین نمیخوره

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

    Default Re: Set textAlignment on all the items [QSqlTableModel]

    At return the function quits and no further code is executed. For showing the content on a view, the data function is called multiple times. For each roll once!

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

    Alir3z4 (18th February 2012)

  6. #5
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Set textAlignment on all the items [QSqlTableModel]

    Quote Originally Posted by Lykurg View Post
    For showing the content on a view, the data function is called multiple times. For each roll once!
    Hmmm! That's the answer, nice!
    Thanks Lykurg!
    ...یه مرد هیچوقت زمین نمیخوره

Similar Threads

  1. Replies: 0
    Last Post: 15th April 2011, 19:04
  2. Replies: 8
    Last Post: 30th March 2011, 20:06
  3. Replies: 2
    Last Post: 20th August 2010, 05:18
  4. Replies: 7
    Last Post: 18th November 2008, 17:17
  5. QSqlTableModel and LIKE
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 08:45

Tags for this Thread

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.