Change the method of Qtreeview (change the height of the delegate line, or: When the data is:d ATA, judge by Qt::sizehintrole)

Source: Internet
Author: User

A long time ago wrote an article about Qtreeview fast display more than thousands data items, if that article is about Qtreeview's internal strength, today this is about qtreeview drilling, sometimes we want to change the view of the row height of the data item, how to do? Here are two ways to change the line height here today:

1. By changing the default delegate method for Qtreeview

[CPP]View PlainCopy
  1. Class Cdelegate: Public qstyleditemdelegate
  2. {
  3. Q_object
  4. Public
  5. Cdelegate (Qobject *parent): qstyleditemdelegate (parent)
  6. {
  7. }
  8. Protected
  9. Qsize sizehint (const Qstyleoptionviewitem &option,
  10. Const Qmodelindex &index) Const
  11. {
  12. Qsize size = qstyleditemdelegate::sizehint (option, index);
  13. return Qsize (Size.width (), 28);
  14. }
  15. };

By default, Qtreeview uses Qstyleditemdelegate as its proxy, by overloading the Qstyleditemdelegate Sizehint method, I set the row height here to 28, you can modify the corresponding row height as needed, and then, By replacing the default proxy with Qtreeview with cdelegate, I do the following:

[CPP]View PlainCopy
    1. Ctreeview::ctreeview (Qwidget *parent): Qtreeview (parent)
    2. {
    3. Cdelegate *pdelegate = new Cdelegate (this);
    4. Setitemdelegate (pdelegate);
    5. Setuniformrowheights (true);
    6. }

Is it easy for everyone to see?

2. Use the data method of the overloaded Qabstractitemmodel as follows:

[CPP]View PlainCopy
  1. Qvariant Cmchmodel::d ata (const qmodelindex &index, int role) const
  2. {
  3. if (!index.isvalid ())
  4. return qvariant ();
  5. if (qt::sizehintrole = = role)
  6. {
  7. return Qsize (Ptreeview->visualrect (index). Width (), 28);
  8. }
  9. return qvariant ();
  10. }

Where Ptreeview is the CTreeView above, the method does not add in the CTreeView constructor:

[CPP]View PlainCopy
    1. Cdelegate *pdelegate = new Cdelegate (this);
    2. Setitemdelegate (pdelegate);

is not also very simple.

http://blog.csdn.net/rabinsong/article/details/13161799

Change the method of Qtreeview (change the height of the delegate line, or: When the data is:d ATA, judge by Qt::sizehintrole)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.