表格中含有COMBOX,如何使QT中的combox选项中的值被选中后该行上其他位置处的数值自动被填进表格内??而且要求选中后该行上的数据是可改变输入的。求详细代码
3条回答 默认 最新
- 沉西乐恩 2015-05-19 05:52关注
可以使用model/view框架,修改数据用delegate
都有现成的类,没有合适的可以继承他现有的再自己实现。
模型类QAbstractItemModel: 链表模型QAbstractListModel 表格模型QAbstractTableModel 标准模型QAbstandModel
视图类QAbstractItemView: QListView QTableView QTreeView ...
委托类QItemDelegate
这是我代码中的一段,继承QItemDelegate的一个类的实现,仅供参考 希望对你有帮助。
ValDelegate::ValDelegate(int Column,QObject *parent) :
QItemDelegate(parent)
{
this->Column = Column;
}QWidget* ValDelegate::createEditor(QWidget parent,
const QStyleOptionViewItem &option,const QModelIndex &index) const
{
if(index.column()%2)
{
int secs = index.model()->data(index, Qt::DisplayRole).toInt();
QSpinBox box = new QSpinBox(parent);
box->setMaximum(99999);
box->setMinimum(0);
box->setAlignment(Qt::AlignHCenter);
box->setValue((short)secs);
connect(box, SIGNAL(editingFinished()),this, SLOT(CommitAndCloseEditor()));
return box;
}
else
return QItemDelegate::createEditor(parent, option, index);
}void ValDelegate::setEditorData(QWidget editor, const QModelIndex &index) const
{
if (index.column()%2)
{
int secs = index.model()->data(index, Qt::DisplayRole).toInt();
QSpinBox box = qobject_cast(editor);
box->resize(100,30);
QPoint pos = box->pos();
//pos.setX(pos.rx()+10);
pos.setY(pos.ry()+5);
box->move(pos);
box->setValue((short)secs);
}
else
QItemDelegate::setEditorData(editor, index);
}void ValDelegate::setModelData(QWidget editor, QAbstractItemModel model,
const QModelIndex &index) const
{
if (index.column()%2)
{
QSpinBox* box = qobject_cast(editor);
short secs = (short)box->value();((YcModel*)model)->setData(index, secs); } else QItemDelegate::setModelData(editor, model, index);
}
void ValDelegate::CommitAndCloseEditor()
{
QSpinBox* box = qobject_cast(sender());emit commitData(box); emit closeEditor(box);
}
解决 无用评论 打赏 举报