Results 1 to 2 of 2

Thread: checkbox and radiobutton in treeview

  1. #1
    Join Date
    May 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default checkbox and radiobutton in treeview

    Hi all
    I have to show radio button and check box in a treeview.

    to do this I have derived a QAbstractItemModel class that with a custom role that get/set the node check type (radio buttons or check box)
    I also derived a QItemDelegate, on this new class in the virtual function:
    drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
    is possible to draw directly the check box or the radio button with QApplication::style()->drawPrimitive using as parameter PE_IndicatorRadioButton or PE_IndicatorViewItemCheck.

    my problem is: how can I access the item to draw? I need a ModelIndex to know if draw a radio button or a check box.

    Thank all

  2. #2
    Join Date
    May 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: checkbox and radiobutton in treeview

    I have find a patch:
    since drawCheck is called by paint, I've reimplemented the paint function in QMyItemDelegate:

    class QMyItemDelegate : public QItemDelegate
    {
    ...
    protected:
    virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    void drawCheck ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, Qt::CheckState state ) const;
    private:
    mutable int selType;
    ...
    }

    where selType is mutable becouse of the const attribute of the functions

    void QMyItemDelegate :aint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
    selType=index.model()->data(index,SelTypeRole).toInt();
    QItemDelegate:aint(painter,option,index );
    }

    void QMyItemDelegate ::drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
    {
    ...
    switch(selType)
    {
    case stp_check:
    style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck , &opt, painter);
    break;
    case stp_radio:
    style->drawPrimitive(QStyle::PE_IndicatorRadioButton, &opt, painter);
    }

    where SelTypeRole is a custom role that return stp_check or stp_radio.

    That work, but is a little "dirt" for me...

    any other idea?

    thanks

Similar Threads

  1. CheckBox in treeview
    By ansar in forum Qt Programming
    Replies: 2
    Last Post: 16th December 2009, 05:41
  2. Replies: 2
    Last Post: 27th September 2009, 21:36
  3. How can I center a checkbox in a treeview?
    By 3str in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 06:47
  4. radiobutton behavior on menubar
    By sp4rk3r in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2008, 04:37
  5. radiobutton output file
    By nitriles in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2007, 09:04

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.