QT中如何让QTreeWidget某一列可以编辑,如何实现某一列控件的自定义化

原文链接:http://www.verydemo.com/demo_c278_i2022.html

1.如何让树控件的第二栏变成QSpinBox?

首先我们要了解类QItemDelegate,这是个委托类。委托类可用于很多地方,比如QTreeWidget,QTableWidget等等。要想实现定义的委托类,我们可以继承QItemDelegate,然后实现4个方法,分别是createEditor(),setEditorData(),updateEditorGeometry() ,setModelData() 。4个方法的用途,详请参考QT帮助文档。


好了,有了我们自定的委托类。这件事就好办多了。QTreeWidget的方法setItemDelegateForColumn( int column, QAbstractItemDelegate * delegate ),可用来将指定的列设置为某个委托。这样它,就会如我们所愿的显示和工作了。

关键代码如下:

QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    QSpinBox *editor = new QSpinBox(parent);
    editor->setMinimum(0);
    editor->setMaximum(100);

    return editor;
}
void SpinBoxDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    int value = index.model()->data(index, Qt::EditRole).toInt();

    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(value);
}
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText();
    int value = spinBox->value();

    model->setData(index, value, Qt::EditRole);
}
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}

小提示:如果你仅把第二栏设置了委托,而第一栏没有设置的话,图形化界面效果不是很好,所以,尽量给所有的列都设置委托,是个QItemDelegate也行。


2.如果让我们刚才设置特殊代理的那一栏可以编辑,而其它的栏不可编辑


大家都知道,在Qt中, QTreeWidget都是QTreeWidgetItem的形式来组织的,也就是说如果单独设置属性的话,可以使得某一行可以编辑或者不可编辑。这里给大家介绍个方法来使某一列仅可编辑

 QTreeWidget::openPersistentEditor ( QTreeWidgetItem * item, int column = 0 )可用来打开某一行的某一列的编辑状态

QTreeWidget::closePersistentEditor ( QTreeWidgetItem * item, int column = 0 )则可以用来关闭某一样某一列的始终编辑状态

在鼠标双击树控件的某个部分的时候,树控件会发出一个消息itemDoubleClicked ( QTreeWidgetItem * item, int column ),如果我们捕捉这个消息 ,并判断Column的值,就可以使得某一列可以编辑,而其它列不可编辑。但是由于openPersistentEditor 会使得这个单元格始终可编辑 ,及时鼠标已经没有选中该单元格了,所以,我们还需要在用户编辑完当前单元格时,调用closePersistentEditor来关闭单元格始终可以编辑状态。

关键代码如下:

QObject::connect(treeWidget,SIGNAL(itemDoubleClicked ( QTreeWidgetItem*, int)),
		this,SLOT(openPersistentEditor(QTreeWidgetItem*,int)));

QObject::connect(treeWidget,SIGNAL(itemSelectionChanged ()),
		this,SLOT(slotSelectionChanged()));

void CMainWindow::openPersistentEditor(QTreeWidgetItem* item,int column)
{
	if( 1 == column  )
	{
		treeWidget->openPersistentEditor(item,column);
		lastOpen = item;
	}
}
void CMainWindow::slotSelectionChanged()
{
	if( NULL!= lastOpen )
	{
		treeWidget->closePersistentEditor(lastOpen,1);
		lastOpen = NULL;
	}
}

效果图:

小提示:可以参见Qt Examples中的spinboxdelegate工程(Qt\4.8.0\examples\itemviews\spinboxdelegate)


  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值