gpt4 book ai didi

c++ - 旋转框的值,就在它改变之前

转载 作者:行者123 更新时间:2023-11-30 02:31:56 25 4
gpt4 key购买 nike

我正在使用 qt 库编写代码,其中我需要在旋转框发生变化之前(通过信号)获取它的值。

我有:

QSpinBox spinBoxWidth:
QSpinBox spinBoxScale;

我想将一个信号从 spinBoxWidth 连接到 spinBoxScale,以便 SpinBoxScale 的值始终是“更改后 SpinBoxWidth 的值”到“更改前的值”。

(比例 = width_new/width_old)

我没有在 Qt 中找到任何在更改值时返回旋转框旧值的插槽。我能以某种方式为此写一个插槽吗?

最好的问候

最佳答案

有两种方法:

  • 在变化发生之前捕捉变化并使用事件系统(QKeyEventQMouseEvent)存储旧值。这很容易出错,因为可以手动设置 spinBoxWidth 的值。
  • spinBoxWidthvalueChanged(int) 信号连接到一个插槽,并引用调用它的最后一个值。我推荐这种方法。

尝试这样的事情:

class MonitoringObject : public QObject
{
Q_OBJECT
int lastValue;
int currentValue;
...
public Q_SLOTS:
void onValueChanged(int newVal)
{
lastValue = currentValue;
currentValue = newVal;
if (lastValue == 0) //catch divide-by-zero
emit ratioChanged(0);
else
emit ratioChanged(currentValue/lastValue);
}
Q_SIGNALS:
void ratioChanged(int);

连接信号后,流程应如下所示:

  • spinBoxWidth 发出 valueChanged(int)
  • MonitoringObject::onValueChanged(int) 被调用,完成它的工作并发出 ratioChanged(int)
  • spinBoxScale 在其 setValue(int) 槽中接收信号并设置适当的值。

关于c++ - 旋转框的值,就在它改变之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37001492/

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