如何创建代理的QTreeWidget? [英] HowTo create delegate for QTreeWidget?

查看:1375
本文介绍了如何创建代理的QTreeWidget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我想做的(所有的父母和孩子必须在右侧有一个关闭按钮,将来只有悬停的项目才能显示**关闭**按钮):

Here is what I'm trying to do (all parents and children must have a close button on the right, in the future, only the hovered item will be able to show the **close ** button):

我的代理代码

class CloseButton : public QItemDelegate
{
     Q_OBJECT

public:
     CloseButton( QObject* parent = 0 )
          : QItemDelegate( parent )
     {};

     QWidget* createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
     {
          if ( index.column() == 1 )
          {
               QToolButton* button = new QToolButton( parent );
               button->setIcon( QIcon( CLOSE_ICON ) );
               //button->setFixedSize( 16, 16 );
               //button->setAutoRaise( true );
               //button->setVisible( true );

               CONNECT( button, SIGNAL( clicked() ), this, SLOT( emitCommitData() ) );

               return button;
          }
          return ( new QWidget );
     }

private slots:
     void emitCommitData()
     {
          emit commitData( qobject_cast< QWidget* >( sender() ) );
     }

private:
     //Q_DISABLE_COPY( CloseButton );
};

使用 QTreeWidget 连接代码:

With QTreeWidget connection code:

recipientsView()->setItemDelegateForColumn( 1, new CloseButton( this ) );

其中 recipientsView() code> QTreeWidget 。

where recipientsView() is a simple QTreeWidget.

问题是未显示 QToolButton (它必须在第二列,即树中的列索引是 1 )。

Problem is that QToolButtons are not shown at all (it must be in the second column, i.e. column index in the tree is 1). What I'm doing wrong?

我已经查看了所有关于代表的Qt演示示例,以及关于 QItemDelegate 和类似的东西。

I have checked already all Qt demo examples about delegates and the first Google result about QItemDelegate's and similar stuff.

推荐答案

您可以使用 QStyledDelegate :: paint 函数绘制关闭图标,而不使用任何窗口小部件, editorEvent 接收项目的鼠标事件,即使您不使用编辑器或使该项目可编辑。

You can use the QStyledDelegate::paint function to draw the close icon, without using any widget, and the editorEvent to receive mouse events for the item, even if you don't use the editor or make the item editable.

class CloseButton : public QStyledItemDelegate {
    Q_OBJECT
public:

    explicit CloseButton(QObject *parent = 0, 
                         const QPixmap &closeIcon = QPixmap())
        : QStyledItemDelegate(parent)
        , m_closeIcon(closeIcon)
    {
        if(m_closeIcon.isNull())
        {
            m_closeIcon = qApp->style()
                ->standardPixmap(QStyle::SP_DialogCloseButton);
        }
    }

    QPoint closeIconPos(const QStyleOptionViewItem &option) const {
        return QPoint(option.rect.right() - m_closeIcon.width() - margin,
                      option.rect.center().y() - m_closeIcon.height()/2);
    }

    void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const {
        QStyledItemDelegate::paint(painter, option, index);
        // Only display the close icon for top level items...
        if(!index.parent().isValid()
                // ...and when the mouse is hovering the item
                // (mouseTracking must be enabled on the view)
                && (option.state & QStyle::State_MouseOver))
        {
            painter->drawPixmap(closeIconPos(option), m_closeIcon);
        }
    }

    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const
    {
        QSize size = QStyledItemDelegate::sizeHint(option, index);

        // Make some room for the close icon
        if(!index.parent().isValid()) {
            size.rwidth() += m_closeIcon.width() + margin * 2;
            size.setHeight(qMax(size.height(),
                                m_closeIcon.height() + margin * 2));
        }
        return size;
    }

    bool editorEvent(QEvent *event, QAbstractItemModel *model,
                     const QStyleOptionViewItem &option,
                     const QModelIndex &index)
    {
        // Emit a signal when the icon is clicked
        if(!index.parent().isValid() &&
                event->type() == QEvent::MouseButtonRelease) {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);

            QRect closeButtonRect = m_closeIcon.rect()
                    .translated(closeIconPos(option));

            if(closeButtonRect.contains(mouseEvent->pos()))
            {
                emit closeIndexClicked(index);
            }
        }
        return false;
    }

signals:
    void closeIndexClicked(const QModelIndex &);
private:
    QPixmap m_closeIcon;
    static const int margin = 2; // pixels to keep arount the icon

    Q_DISABLE_COPY(CloseButton)
};

这篇关于如何创建代理的QTreeWidget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆