Results 1 to 4 of 4

Thread: Models, delegates or views and how?

  1. #1
    Join Date
    Nov 2007
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Models, delegates or views and how?

    This my first post I have no intro. so let's get to the point

    In my current and my first QT4 project which is a simple ToDo list

    manager I have two more things to do and I have no idea how to do

    them, Here they are:

    1) Showing strike out titles in case of a done tasks based on done value (0 or 1) retrieved by the model.
    2) Showing icons of type (personal, Professional) instead of numbers 0 for professional and 1 for personal.

    --------------------------------------------------------
    Model is QSqlQueryModel & view is QTableView
    --------------------------------------------------------

    Remember I'm a fresh newbie

    Thanks in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Models, delegates or views and how?

    This can be done via model's data(). You would subclass QSqlQueryModel and reimplement its data(). Get the data from the base class and add a few checks. When the index is valid and the role is Qt::FontRole, make the font striked out if needed. Notice that there's QFont::setStrikedOut(bool) for that. Also, when the index is valid and the role is Qt::DecorationRole, return a corresponding icon (personal/professional). Otherwise return the value retrieved from the base class. Here's something for you to start with:
    Qt Code:
    1. QVariant MySqlQueryModel::data(const QModelIndex& index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. QVariant value = QSqlQueryModel::data(index, role);
    7. switch (role)
    8. {
    9. case Qt::FontRole:
    10. {
    11. QFont font;
    12. if (value.isValid())
    13. font = qvariant_cast<QFont>(value);
    14. if (taskIsDone)
    15. font.setStrikedOut(true);
    16. return QVariant::fromValue(font);
    17. }
    18. case Qt::DecorationRole:
    19. {
    20. ...
    21. if (professional)
    22. return QVariant::fromValue(professionalIcon);
    23. else
    24. return QVariant::fromValue(personalIcon);
    25. }
    26. default:
    27. return value;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Models, delegates or views and how?

    You can do the same from within a delegate (or a proxy model), if you don't want to touch the original model. The proxy approach is similar to what J-P already suggested (additional advantage is that you can filter out columns which you need for calculation but don't want to show to the user), while in the delegate you'd have to reimplement paint() and modify the style option view item to reflect the state of the task.

  4. #4
    Join Date
    Nov 2007
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: Models, delegates or views and how?

    Quote Originally Posted by jpn View Post
    This can be done via model's data(). You would subclass QSqlQueryModel and reimplement its data(). Get the data from the base class and add a few checks. When the index is valid and the role is Qt::FontRole, make the font striked out if needed. Notice that there's QFont::setStrikedOut(bool) for that. Also, when the index is valid and the role is Qt:ecorationRole, return a corresponding icon (personal/professional). Otherwise return the value retrieved from the base class. Here's something for you to start with:
    Qt Code:
    1. QVariant MySqlQueryModel::data(const QModelIndex& index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. QVariant value = QSqlQueryModel::data(index, role);
    7. switch (role)
    8. {
    9. case Qt::FontRole:
    10. {
    11. QFont font;
    12. if (value.isValid())
    13. font = qvariant_cast<QFont>(value);
    14. if (taskIsDone)
    15. font.setStrikedOut(true);
    16. return QVariant::fromValue(font);
    17. }
    18. case Qt::DecorationRole:
    19. {
    20. ...
    21. if (professional)
    22. return QVariant::fromValue(professionalIcon);
    23. else
    24. return QVariant::fromValue(personalIcon);
    25. }
    26. default:
    27. return value;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    Sounds easy I think I can do that, Just give me a couple minutes

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.