我有一个耗时的计算,取决于双精度值。为此,我创建了一个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();
}
答案 0 :(得分:1)
好像您需要继承<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<div class="form-row">
<div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
<label for="nameInput">First Name</label>
<div class="input-group">
<input type="text" class="form-control" aria-label="Text input with dropdown button" name="nameInput" id="nameInput">
</div>
</div>
<div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
<label for="lastNameInput">Last Name</label>
<div class="input-group">
<input type="tel" class="form-control" aria-label="Text input with dropdown button" name="lastNameInput" id="lastNameInput">
</div>
</div>
<div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-2">
<label for="classInput">Class</label>
<div class="input-group">
<input type="text" class="form-control" aria-label="Text input with dropdown button" name="classInput" id="classInput">
</div>
</div>
</div>
<button type="button" class="commonButton" id="save">
Save
</button>
的子类并重新实现QDoubleSpinbox
方法
textFromValue
但这会破坏Spinbox的默认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);
}
和preffix
功能