crystal0075 2015-05-19 03:07 采纳率: 0%
浏览 3393

qt combox 表格;表格中数据可输入

表格中含有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);
    

    }

    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多