gpt4 book ai didi

c++ - Qt double spin box 在处理值改变时自动填充

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:02:41 25 4
gpt4 key购买 nike

我有一个依赖 double 值的耗时计算。为此,我创建了一个 GUI,我可以在其中使用 QDoubleSpinBox 设置计算值。 .双旋转框QDoubleSpinBox::valueChanged(double)信号连接到一个插槽,该插槽使用 QtConcurrent::run 在新线程中启动繁重的计算。 .问题是,当我添加进度条时,双旋转框会在进度条出现时自动填充其内容(零填充直到小数位数)。我的感觉是,这是因为双旋转框失去了焦点(即进度条是选定的小部件)。

我的问题是:

  • 如何显示进度条并制作双旋转框不用零填充其余的小数位?

This video展示了当进度条未显示时,我可以继续编辑双旋转框,而当显示进度条时,双旋转框用零填充其精度。这是当前的行为,而不是期望的行为。期望的是在计算完成后,双旋转框不会自动用零填充其空的小数位。这是用于视频的代码(在 GitHub 中完全可用):

标题

class DoubleSpinboxHeavyComputation : public QWidget
{
Q_OBJECT

public:
explicit DoubleSpinboxHeavyComputation(QWidget *parent = nullptr);
~DoubleSpinboxHeavyComputation();

signals:

void computationDone();

void progressSignal(int progress_state);

private slots:
void startHeavyComputationInThread();

void heavyComputation();

private:
Ui::DoubleSpinboxHeavyComputation *ui;
int n_ = 0;
};

实现

DoubleSpinboxHeavyComputation::DoubleSpinboxHeavyComputation(QWidget *parent)
: QWidget(parent), ui(new Ui::DoubleSpinboxHeavyComputation)
{
ui->setupUi(this);
connect(ui->doubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
&DoubleSpinboxHeavyComputation::startHeavyComputationInThread);
}

DoubleSpinboxHeavyComputation::~DoubleSpinboxHeavyComputation()
{
delete ui;
}

void DoubleSpinboxHeavyComputation::startHeavyComputationInThread()
{
if (ui->checkBox->isChecked())
{
QProgressDialog *progress = new QProgressDialog("Computing", "", 0, 0, this);
progress->setWindowTitle(windowTitle());
progress->setWindowFlags((progress->windowFlags() | Qt::CustomizeWindowHint) &
~Qt::WindowCloseButtonHint); // Hide close button
progress->setWindowModality(Qt::WindowModal);
progress->setCancelButton(nullptr);
progress->setMaximum(1000);
progress->show();
connect(this, &DoubleSpinboxHeavyComputation::progressSignal, progress, &QProgressDialog::setValue);
connect(this, &DoubleSpinboxHeavyComputation::computationDone, progress, &QProgressDialog::close);
connect(this, &DoubleSpinboxHeavyComputation::computationDone, progress, &QProgressDialog::deleteLater);
}
QtConcurrent::run(this, &DoubleSpinboxHeavyComputation::heavyComputation);
}

void DoubleSpinboxHeavyComputation::heavyComputation()
{
int current_n = n_;
++n_;
qDebug() << "Start computation " << current_n;
for (int i = 0; i < 1000; i++)
{
emit progressSignal(i);
usleep(1000);
}
qDebug() << "End computation" << current_n;
emit computationDone();
}

最佳答案

看起来您需要继承QDoubleSpinbox 并重新实现textFromValue 方法

class NumericEdit : public QDoubleSpinBox
{
Q_OBJECT
public:
NumericEdit(QWidget *p_parent = nullptr);

QString textFromValue(double val) const;

};

QString NumericEdit::textFromValue(double val) const
{
//default converting
// return QString::number(val);
//converting with a local representation
QLocale locale;
return locale.toString(val);
}

但这会破坏默认的 prefixsuffix 旋转框的功能

关于c++ - Qt double spin box 在处理值改变时自动填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56757781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com