qt - 不处于编辑模式时如何在QTableView中显示QSpinBox
问题描述
我能够使用 QStyledItemDelegate 在 QTreeView 中添加 QSpinBox 小部件。
QWidget *NoteDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 2)
{
QSpinBox *spinBox = new QSpinBox(parent);
spinBox->setRange(0, 9999);
return spinBox;
}
return QStyledItemDelegate::createEditor(parent, option, index);
}
void NoteDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (index.column() == 2)
{
int value = index.model()->data(index, Qt::EditRole).toInt();
auto spinBox = static_cast<QSpinBox *>(editor);
spinBox->setValue(value);
return;
}
QStyledItemDelegate::setEditorData(editor, index);
}
void NoteDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (index.column() == 2)
{
auto spinBox = static_cast<QSpinBox *>(editor);
int value = spinBox->value();
model->setData(index, value, Qt::EditRole);
return;
}
QStyledItemDelegate::setModelData(editor, model, index);
}
但是,QSpinBox 只有在编辑模式下才会出现。即使处于显示模式,如何始终显示此 QSpinBox?
解决方案
I suspect that QAbstractItemView::openPersistentEditor may be what you are looking for.
推荐阅读
- java - SSL WebSocket 连接错误证书_未知
- python - 如何从电子和节点 js 中使用 python venv
- azure - Azure 数据工厂管道永久显示 RequestingConsent
- python - VS Code 在每次运行前清除先前的输出
- javascript - e.preventDefault 在 javascript 中不起作用
- python - Python 语音识别在基于窗口的 GUI 应用程序中不起作用(它是一个带有隐藏控制台的 .exe 应用程序)
- ios - 如何在一个 ViewController 和另一个控制器之间共享核心数据(小部件)
- rest - 如何使用 apollo-datasource-rest 编写 Multipart-form POST REQUEST
- flutter - Flutter ontap 在模拟器中工作,但在移动设备中不一致
- c# - 从父组件传递的 Blazor Page Bootstrap 模型不显示