Results 1 to 10 of 10

Thread: Problem with QItemDelegate

  1. #1
    Join Date
    Mar 2010
    Posts
    69
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with QItemDelegate

    Hi everyone,
    How can I change the size of the font of the item in a list using QItemDelegate's paint method? I have tried to change the painter font in the paint method but it is not reflecting the changes in the list view.
    Please help me.
    Thanks in advance.

  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: Problem with QItemDelegate

    Can we see your code? Without it is hard to tell. Use QStyleOptionViewItem and set the font there.

  3. #3
    Join Date
    Mar 2010
    Posts
    69
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QItemDelegate

    Thanks for your reply.
    Here is my code and I want to change the font using painter of the paint method. Is it possible please?
    void CCameraUICustomDelegate:aint(QPainter *painter,
    const QStyleOptionViewItem &option,
    const QModelIndex &index) const{
    QVariant variant = index.data();
    /*QPainter *qPainter = new QPainter;*/
    //QString stringData = variant.toString();
    painter->save();
    painter->setFont(QFont("Times", 30, QFont::Bold));
    painter->restore();

    QStyledItemDelegate:aint(painter,option,index);
    }

  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: Problem with QItemDelegate

    With save and restore everything in between is lost! So your code wont work. Try
    Qt Code:
    1. painter->setFont(QFont("Times", 30, QFont::Bold));
    2. QStyledItemDelegate::paint(painter,option,index);
    To copy to clipboard, switch view to plain text mode 
    If that does not work, alter as told option.

  5. #5
    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: Problem with QItemDelegate

    Ehm, and did you try QWidget::setFont() on your QListView/QListWidget?

  6. #6
    Join Date
    Mar 2010
    Posts
    69
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QItemDelegate

    Still it is not working. Is there any other way to do that please?

  7. #7
    Join Date
    Mar 2010
    Posts
    69
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QItemDelegate

    May be in the future I have to make some more changes in my list. That's why I have to subclass QStyledDelegate. So I have to change the font in the paint method.

  8. #8
    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: Problem with QItemDelegate

    So did you alter option? And you can alternatively draw all yourself without calling the base function. Then you can change the painter like you want.

  9. #9
    Join Date
    Mar 2010
    Posts
    69
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QItemDelegate

    I did alter option but didn't work. I will try to implement the whole function myself. Anyway thanks for your help.

  10. #10
    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: Problem with QItemDelegate

    Well, to be honest, I don't think you have really tried it, because it works fine. It is more likely you use wrong code. But without seeing anything.... The simple setFont solution with a custom delegate works perfect:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class delegate : public QStyledItemDelegate
    4. {
    5. public:
    6. delegate(QObject* parent = 0) : QStyledItemDelegate(parent)
    7. {}
    8.  
    9. void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    10. {
    11. QStyledItemDelegate::paint(painter, option, index);
    12. }
    13. };
    14.  
    15. int main(int argc, char* argv[])
    16. {
    17. QApplication a(argc, argv);
    18.  
    19. list << "a" << "b" << "c";
    20.  
    21. w.setFont(QFont("Courier", 30));
    22. delegate d;
    23. w.setItemDelegate(&d);
    24. w.addItems(list);
    25. w.show();
    26.  
    27. lv.setFont(QFont("Courier", 30));
    28. lv.setItemDelegate(&d);
    29. m.setStringList(list);
    30. lv.setModel(&m);
    31. lv.show();
    32.  
    33.  
    34. return a.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 

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

    chandan (15th April 2010)

Similar Threads

  1. SQL and QItemDelegate problem
    By vieraci in forum Qt Programming
    Replies: 4
    Last Post: 31st May 2009, 01:07
  2. pyqt custom QItemDelegate problem
    By spohle in forum Qt Programming
    Replies: 0
    Last Post: 31st January 2009, 01:48
  3. QComboBox+QItemDelegate+QDateTimeEdit Problem
    By bangqianchen in forum Qt Programming
    Replies: 2
    Last Post: 15th September 2008, 10:14
  4. QItemDelegate problem
    By WinchellChung in forum Newbie
    Replies: 2
    Last Post: 5th December 2007, 16:16
  5. Replies: 2
    Last Post: 12th June 2007, 17:23

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.