Results 1 to 20 of 47

Thread: A few queries about Model View Programming

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: A few queries about Model View Programming

    Quote Originally Posted by montylee View Post
    We need to migrate from text file to CSS but it would most probably be in the next version of the application.
    You can read settings from a text file and apply them using stylesheets.

    Please go thru my queries and if you know answer to any of them, it would be of great help to me.
    From what I see you are doing it the wrong way. You can't modify the rect you paint on. This is the rect that Qt gives you to draw the check on and it supplies the option object to read and apply parameters of the check drawn. If you want something different then provide a full-blown delegate derived from QAbstractItemDelegate. Currently you are strained by the classes you use and instead of getting rid of those constraints you try to work around them, this is not the way.

  2. #2
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    20
    Thanked 6 Times in 5 Posts

    Default Re: A few queries about Model View Programming

    Quote Originally Posted by wysota View Post
    You can read settings from a text file and apply them using stylesheets.
    hmmm maybe. i'll explore about this. So, if i use CSS can i customize everything i have mentioned in my queries?

    Quote Originally Posted by wysota View Post
    From what I see you are doing it the wrong way. You can't modify the rect you paint on. This is the rect that Qt gives you to draw the check on and it supplies the option object to read and apply parameters of the check drawn. If you want something different then provide a full-blown delegate derived from QAbstractItemDelegate. Currently you are strained by the classes you use and instead of getting rid of those constraints you try to work around them, this is not the way.
    okie, so i should not modify the rect argument in the drawCheck() function.
    I suppose, if i subclass QAbstractItemDelegate then i can fully customize the checkbox look.

    One query:
    I have basically no idea about QPainter yet, so does the QPainter code i shared earlier (in void CheckboxDelegate::drawCheck) look ok (except modifying the rect offcourse)? I mean if i subclass QAbstractItemDelegate and rewrite the paint function. In the paint function, i'll just display a checkbox pixmap. In the paint() function, i'll get the information about the current model index too:
    virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0
    so i can display different images when a row is highlighted.

    Now, in this case, can i specify my own rect and draw the checkbox image as i like? You said that in QItemDelegate:drawCheck(), i can't modify the rect but here i am not getting any rect as argument, so is it ok to specify my own desired rect and display the pixmap within that rect?

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

    Default Re: A few queries about Model View Programming

    Quote Originally Posted by montylee View Post
    hmmm maybe. i'll explore about this. So, if i use CSS can i customize everything i have mentioned in my queries?
    Well... so far we were basically tallking about a custom checkbox, so yes.

    okie, so i should not modify the rect argument in the drawCheck() function.
    Correct.

    I suppose, if i subclass QAbstractItemDelegate then i can fully customize the checkbox look.
    Yes, but then you also have to handle clicking on the checkbox yourself (detecting the hit area, modifying the model and handle drawing).

    One query:
    I have basically no idea about QPainter yet, so does the QPainter code i shared earlier (in void CheckboxDelegate::drawCheck) look ok (except modifying the rect offcourse)?
    Yeah, it looks fine although it doesn't do much - you force a fill of a rect with a specified colour and then render a pixmap on top of it. I don't see the point of neither making the fill nor filling with that colour and not some other, but the code itself is ok.

    I mean if i subclass QAbstractItemDelegate and rewrite the paint function. In the paint function, i'll just display a checkbox pixmap. In the paint() function, i'll get the information about the current model index too:

    so i can display different images when a row is highlighted.
    Basically you have to look inside the QStyleOptionViewItem object and fetch all the necessary info from it (like the palette, areas to paint, current state of the item, etc.).

    Now, in this case, can i specify my own rect and draw the checkbox image as i like?
    Yes, you get the area for the whole item and its current state and you can draw as many checks or other things as you like.

    You said that in QItemDelegate:drawCheck(), i can't modify the rect but here i am not getting any rect as argument, so is it ok to specify my own desired rect and display the pixmap within that rect?
    Yes, provided the rect is within the item's rect which you receive inside the option argument. The painter is clipped anyway, so you wouldn't be able to draw outside of the rectangle even if you wanted to.

  4. The following user says thank you to wysota for this useful post:

    montylee (14th January 2009)

  5. #4
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    20
    Thanked 6 Times in 5 Posts

    Default Re: A few queries about Model View Programming

    wow, thanks for the reply
    things seem a bit clear now, i hope i am able to implement it properly.

    BTW, i downloaded ur new article which was in the Qt quarterly newsletter. You really are a Qt guru

    Thanks for your help! I might bug you later on again

  6. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    20
    Thanked 6 Times in 5 Posts

    Default Re: A few queries about Model View Programming

    hey, i tried using style sheets in my application. They seem to be so simple and i am able to set most of the things. But i have a problem.

    I am using QWidget::setStyleSheet to set the style for widgets especially my QTableView. I am able to set the background color, highlight color etc...

    Now the problem is how to set background color or image for the checkbox? Since the checkbox is displayed through the delegate automatically, i am not able to modify it.

    I tried using QWidget::setStyleSheet in the QItemDelegate::drawCheck() method but it gives a compilation error since setStyleSheet method can be used only for QApplication and QWidget classes.

    Now, you said that using Style sheets i would be able to custom display the checkbox, but i am not able to find any info about using style sheets in delegate drawing. Please help me.

  7. #6
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    20
    Thanked 6 Times in 5 Posts

    Default Re: A few queries about Model View Programming

    I was able to display a checkbox icon in the entire table view cell. I subclassed QStyledItemDelegate class and rewrote the paint function. Here's the code for reference:

    Qt Code:
    1. void CheckboxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if (index.column() == 3) {
    4. QVariant state = index.model()->data(index, Qt::CheckStateRole);
    5. if (state == Qt::Checked) {
    6. // Calculate rectangle to draw the checkbox icon
    7. QRect rect = option.rect.adjusted(20, 5, -25, -10);
    8. if (option.state & QStyle::State_Selected) {
    9. // Fill the highlight rectangle
    10. painter->fillRect(option.rect, option.palette.highlight());
    11. // Draw "ALL" icon in the first row and checkbox icons in other rows
    12. if (index.row() == 0)
    13. painter->drawPixmap(option.rect, QPixmap("all-on.png"));
    14. else
    15. painter->drawPixmap(rect, QPixmap("check-icon-hl.png"));
    16. } else {
    17. if (index.row() == 0)
    18. painter->drawPixmap(option.rect, QPixmap("all-on.png"));
    19. else
    20. painter->drawPixmap(rect, QPixmap("check-icon.png"));
    21. }
    22. } else {
    23. if (option.state & QStyle::State_Selected)
    24. painter->fillRect(option.rect, option.palette.highlight());
    25. if (index.row() == 0)
    26. painter->drawPixmap(option.rect, QPixmap("all-off.png"));
    27. }
    28. } else {
    29. QStyledItemDelegate::paint(painter, option, index);
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

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

    georgep (15th February 2009)

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 21:17
  2. model View programming problem
    By mismael85 in forum Qt Programming
    Replies: 3
    Last Post: 2nd April 2008, 22:44
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 09:50
  4. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 14:01
  5. Replies: 6
    Last Post: 20th April 2006, 11:23

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
  •  
Qt is a trademark of The Qt Company.