这是我正在做的事情(所有的父母和孩子必须有一个关闭按钮在未来,只有悬停的项目将能够显示**关闭**按钮):如何为QTreeWidget创建委托?
我的委托代码:
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
连接代码:
recipientsView()->setItemDelegateForColumn(1, new CloseButton(this));
其中recipientsView()
是一个简单的QTreeWidget
。
问题是QToolButton
完全没有显示(它必须在第二列,即树中的列索引是1
)。我做错了什么?
我已经检查过关于代表的所有Qt演示示例,以及有关QItemDelegate
和类似内容的第一个Google结果。
在VS2008调试器构造函数执行,但'createEditor()'方法都没有。首先,Thrx, – mosg