RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 818586
Accepted
Виктор
Виктор
Asked:2020-04-24 17:23:24 +0000 UTC2020-04-24 17:23:24 +0000 UTC 2020-04-24 17:23:24 +0000 UTC

如何在 QDoubleSpinBoxDelegate 中动态更改增量步长?

  • 772

在 C++ 应用程序中,有一个 QTableWidget 表,其中一些列与 QDoubleSpinBoxDelegate 相关联,但增量值通过 QComboBoxDelegate 设置在另一列的同一行上。问题是 - 在 QDoubleSpinBoxDelegate 中编辑单元格时如何设置步长值。代表本人:

#ifndef DOUBLESPINBOXDELEGATE_H
#define DOUBLESPINBOXDELEGATE_H

#include <QObject>
#include <QItemDelegate>
#include <QDoubleSpinBox>

class DoubleSpinBoxDelegate : public QItemDelegate
{
    Q_OBJECT
    double minVal;
    double maxVal;
    double oneStep;
    QString suffix;
public:
    DoubleSpinBoxDelegate(QObject *parent = nullptr);
    DoubleSpinBoxDelegate(QObject *parent, double min, double max, double step = 0.0001, QString suffix = "");

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void setStepSize(double step);

};

#endif // DOUBLESPINBOXDELEGATE_H

执行:

#include "doublespinboxdelegate.h"

DoubleSpinBoxDelegate::DoubleSpinBoxDelegate(QObject *parent)
{
    minVal = 1.5;
    maxVal = 29.9999;
}

DoubleSpinBoxDelegate::DoubleSpinBoxDelegate(QObject *parent, double min, double max, double step, QString suffix)
{
    if (parent)
    {
        minVal = min;
        maxVal = max;
        oneStep = step;
        this->suffix = suffix;
    }
}

QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{    
    QDoubleSpinBox *editor = new QDoubleSpinBox (parent);
    editor->setSuffix(tr(" MHz"));
    editor->setDecimals (6);
    editor->setMinimum(minVal);
    editor->setMaximum(maxVal);
    editor->setSingleStep(oneStep);

    return (QWidget *) editor;
}

void DoubleSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    double value = index.model()->data(index, Qt::EditRole).toDouble();

    QDoubleSpinBox *dSpinBox = static_cast<QDoubleSpinBox*>(editor);
    dSpinBox->setValue(value);
}

void DoubleSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QDoubleSpinBox *dSpinBox = static_cast<QDoubleSpinBox*>(editor);
    dSpinBox->interpretText();
    double value = dSpinBox->value();

    model->setData(index, value, Qt::EditRole);
}

void DoubleSpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

很明显,在createEditor方法中,需要设置oneStep的值,但是如何从同一行的表的另一列中读取呢?

c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Cerbo
    2020-04-24T19:53:18Z2020-04-24T19:53:18Z

    如果您的编辑器设置取决于数据,那么最好在setEditorData. 所以对我来说更有意义。如何最正确地做到这一点取决于项目,我将列出以下选项:

    void DoubleSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
       double value = index.data(Qt::EditRole).toDouble();
    
       QDoubleSpinBox *spin = dynamic_cast<QDoubleSpinBox*>(editor);
       spin->setValue(value);
    
       // можно обратиться прямо к модели, самый простой и быстрый вариант
       // но делегат будет работать только с одной моделью
       MyModel * model = dynamic_cast<MyModel*>(index.model());
       auto step1 = model->getMySpinBoxStep();
    
       // можно напрямую запросить нужную ячейку, чуть лучший вариант
       // так как делегат будет зависеть от структуры модели
       double step2 = index.model()->data(step_index, Qt::EditRole).toDouble();
    
       // можно задать для этого отдельную роль, это наиболее идеоматичный
       // способ так как типы делегата и модели связаны только через код роли
       double step3 = index.data(MyStepRole).toDouble();
    
       spin->setSingleStep(step2);
    }
    

    让我提醒您,您自己的数据角色的代码必须大于 的值Qt::UserRole。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5