如何编辑 QTableView
我目前正在尝试向从 QTableView
派生的类添加编辑功能。我添加了一个应该提供编辑功能的自定义委托。但是,如果我执行一个简单的 qDebug() << "FieldDelegate::createEditor()"; 在委托的开头,我可以看到这个函数永远不会被调用。我试图查看 Qt4 文档和《C++ Gui 编程使用 QT4》一书中的示例,但我所能找到的只是带有 QTableWidgets
而不是 QTableViews
的委托示例>。然而,就我而言,我需要一个自定义数据模型,因此我不想复制 QTableWidgetItems
中的任何数据。
到目前为止,我做了以下操作:
- 我使用
QAbstractItemModel::setItemDelegate()
添加了自定义委托 - 我使用
setEditTriggers( QAbstractItemView::DoubleClicked )
激活了编辑功能
当我双击在此设置中,任何项目都不会发生。 FieldDelegate::createEditor()
根本没有被调用。
我还尝试将 doubleClicked()
信号从 QAbstractItemView
连接到同一类的 edit()
插槽,但这只给了我每当我双击单元格时,都会出现消息编辑:编辑失败
。
到目前为止,除了 DatabaseModel::data()
方法中的 Qt::EditRole
之外,我没有返回任何其他角色的内容。这与最后一种情况类似,我想在表底部添加一个空行,用于添加新数据。这会导致问题吗?
下面是构建视图的代码:
DocumentChoiceView::DocumentChoiceView( DatabaseModel * model,
QWidget * parent ) :
QTableView( parent ),
m_model( model )
{
setShowGrid ( false );
setModel( m_model );
setItemDelegate( m_model->delegate().get() );
setEditTriggers( QAbstractItemView::DoubleClicked );
connect( this, SIGNAL(doubleClicked(const QModelIndex&)),
this, SLOT(edit(const QModelIndex&)) );
verticalHeader()->hide();
}
I am currently trying to add editing functionality to a class derived from QTableView
. I have added a custom delegate that should provide editing functionality. However if I do a simple qDebug() << "FieldDelegate::createEditor()";
at the beginning of the delegate, I can see that this function never get's called. I tried to look at the examples in the Qt4 documentation and in the book "C++ Gui Programming with QT4", but all I could find were examples for delegates with QTableWidgets
and not QTableViews
. However in my case I need a custom data model, so I do not want to replicate any data in QTableWidgetItems
.
So far I did the following:
- I added the custom delegate using
QAbstractItemModel::setItemDelegate()
- I activated editing functions using
setEditTriggers( QAbstractItemView::DoubleClicked )
When I double click on an item nothing happens in this setup. The FieldDelegate::createEditor()
is not called at all.
I also tried connecting the doubleClicked()
signal from the QAbstractItemView
to the edit()
slot of the same class, but this only gives me the message edit: editing failed
whenever I double click on a cell.
So far I do not return anything in other roles than Qt::EditRole
from the DatabaseModel::data()
method. This will be similar to the final case, where I want to add an empty row at the bottom of the table that will be used for adding new data. Could this cause the problem?
Here is the code for the construction of the View:
DocumentChoiceView::DocumentChoiceView( DatabaseModel * model,
QWidget * parent ) :
QTableView( parent ),
m_model( model )
{
setShowGrid ( false );
setModel( m_model );
setItemDelegate( m_model->delegate().get() );
setEditTriggers( QAbstractItemView::DoubleClicked );
connect( this, SIGNAL(doubleClicked(const QModelIndex&)),
this, SLOT(edit(const QModelIndex&)) );
verticalHeader()->hide();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使模型可编辑,需要返回编辑标志。
http://doc.qt.io /qt-4.8/model-view-programming.html#making-the-model-editable
For the model to be editable the edit flag need to be returned.
http://doc.qt.io/qt-4.8/model-view-programming.html#making-the-model-editable