如何将 QSlider 连接到 QDoubleSpinBox

发布于 2024-12-26 01:39:51 字数 388 浏览 2 评论 0原文

我想将 QSlider 连接到 QDoubleSpinBox,但是虽然代码编译良好并可以运行简单的 QSpinBox,但它不适用于 QDoubleSpinBox

QSlider *horizontalSlider1 = new QSlider();
QDoubleSpinBox *spinBox1 = new QDoubleSpinBox();

connect(spinBox1, SIGNAL(valueChanged(double)),horizontalSlider1,SLOT(setValue(double)) );
connect(horizontalSlider1,SIGNAL(valueChanged(double)),spinBox1,SLOT(setValue(double)) );

I want to connect a QSlider to a QDoubleSpinBox but while the code compiles fine and runs for simple QSpinBox, it doesn't work for QDoubleSpinBox

QSlider *horizontalSlider1 = new QSlider();
QDoubleSpinBox *spinBox1 = new QDoubleSpinBox();

connect(spinBox1, SIGNAL(valueChanged(double)),horizontalSlider1,SLOT(setValue(double)) );
connect(horizontalSlider1,SIGNAL(valueChanged(double)),spinBox1,SLOT(setValue(double)) );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

记忆で 2025-01-02 01:39:51

QSlider 和 QDoubleSpinBox 在 valueChanged/setValue 中采用不同类型的参数(当然,QSlider 使用整数,QDoubleSpinBox 使用双精度)。更改滑块信号和槽的参数类型可能会有所帮助:

connect(spinBox1, SIGNAL(valueChanged(double)),horizontalSlider1,SLOT(setValue(int)) );
connect(horizontalSlider1,SIGNAL(valueChanged(int)),spinBox1,SLOT(setValue(double)) );

我不确定 Qt 是否可以自动为您处理这种类型转换;如果没有,您必须定义自己的插槽才能在正确的对象上调用 setValue()

QSlider and QDoubleSpinBox take different types of arguments in valueChanged/setValue (QSlider uses ints and QDoubleSpinBox uses doubles, of course). Changing the argument type for the slider's signal and slot might help:

connect(spinBox1, SIGNAL(valueChanged(double)),horizontalSlider1,SLOT(setValue(int)) );
connect(horizontalSlider1,SIGNAL(valueChanged(int)),spinBox1,SLOT(setValue(double)) );

I'm not sure if Qt can automatically handle this type conversion for you; if not, you'll have to define your own slots to call setValue() on the correct object

孤者何惧 2025-01-02 01:39:51

您必须添加自己的插槽来转换参数类型并发出信号或直接更新滑块。

You'll have to add your own slot which converts the argument type and emits a signal or updates the slider directly.

纸伞微斜 2025-01-02 01:39:51

正如 Dave Kilian 回答的那样,QSlider 的信号和插槽不使用双精度。另外,Qt 4 不会自动转换类型;它不会允许您连接它们。因此,您必须编写自己的转换槽来更新其他项目的值。

As Dave Kilian answered, the signals and slots for QSlider don't use double. Also, Qt 4 does not automatically convert the types; it will not allow you to connect them. So, you will have to write your own conversion slot which updates the value of the other item.

三生路 2025-01-02 01:39:51

希望这可以帮助你。

#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QSlider>
#include <QLabel>

class DoubleSlider : public QSlider {
    Q_OBJECT

public:
    DoubleSlider(QWidget *parent = 0) : QSlider(parent) {
        connect(this, SIGNAL(valueChanged(int)),
            this, SLOT(notifyValueChanged(int)));
    }

signals:
    void doubleValueChanged(double value);

public slots:
    void notifyValueChanged(int value) {
        double doubleValue = value / 10.0;
        emit doubleValueChanged(doubleValue);
    }
};

class Test : public QWidget {
    Q_OBJECT
public:
    Test(QWidget *parent = 0) : QWidget(parent),
        m_slider(new DoubleSlider()),
        m_label(new QLabel())
    {
        m_slider->setOrientation(Qt::Horizontal);
        m_slider->setRange(0, 100);
        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(m_slider);
        layout->addWidget(m_label);
        connect(m_slider, SIGNAL(doubleValueChanged(double)),
            this, SLOT(updateLabelValue(double)));
        updateLabelValue(m_slider->value());
    }

public slots:
    void updateLabelValue(double value) {
        m_label->setText(QString::number(value, 'f', 2));
    }

private:
    QSlider *m_slider;
    QLabel *m_label;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Test *wid = new Test();
    wid->show();

    return a.exec();
}

#include "main.moc"

Hope this could help you.

#include <QApplication>
#include <QtGui>
#include <QVBoxLayout>
#include <QSlider>
#include <QLabel>

class DoubleSlider : public QSlider {
    Q_OBJECT

public:
    DoubleSlider(QWidget *parent = 0) : QSlider(parent) {
        connect(this, SIGNAL(valueChanged(int)),
            this, SLOT(notifyValueChanged(int)));
    }

signals:
    void doubleValueChanged(double value);

public slots:
    void notifyValueChanged(int value) {
        double doubleValue = value / 10.0;
        emit doubleValueChanged(doubleValue);
    }
};

class Test : public QWidget {
    Q_OBJECT
public:
    Test(QWidget *parent = 0) : QWidget(parent),
        m_slider(new DoubleSlider()),
        m_label(new QLabel())
    {
        m_slider->setOrientation(Qt::Horizontal);
        m_slider->setRange(0, 100);
        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(m_slider);
        layout->addWidget(m_label);
        connect(m_slider, SIGNAL(doubleValueChanged(double)),
            this, SLOT(updateLabelValue(double)));
        updateLabelValue(m_slider->value());
    }

public slots:
    void updateLabelValue(double value) {
        m_label->setText(QString::number(value, 'f', 2));
    }

private:
    QSlider *m_slider;
    QLabel *m_label;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Test *wid = new Test();
    wid->show();

    return a.exec();
}

#include "main.moc"
慈悲佛祖 2025-01-02 01:39:51

将 QSlider 或 QDial 同步到 QDoubleSpinBox 的一个问题是,当 QDoubleSpinBox 更新 QDial 或 QSlider 时,QDial 或 QSlider valueChanged 信号将被触发,导致它们更新 QDoubleSpinBox 作为回报。这会导致您无法在 QDoubleSpinBox 中输入小数值的问题,因为当您输入小数点“.”时,QDial/Slider 更新会覆盖小数点,从而阻止您输入小数点值的其余部分。

要解决此问题,请让每个输入小部件检查它们的值是否已经与它们正在写入的小部件中的值匹配,如果匹配,则不要更新该值。下面是一个允许 QDial 和 QDoubleSpinBox 相互更新的函数示例:

    def syncDialAndSpinbox(self, widget_in, widget_out):
        if isinstance(widget_in, QtWidgets.QDial):
            value =  float(widget_in.value())*100.0/float(widget_in.maximum())
            out_value = widget_out.value()
        else:
            value = round(widget_in.value()*widget_out.maximum()/100)
            out_value = widget_out.value()

        if value != out_value:
            widget_out.setValue(value)

在此示例中,QDoubleSpinBox 是百分比 (0.0-100.0),QDial 有 100,000 步支持百分比的浮点输出,精确到小数点后三位 ( 0.000% 至 100.000%)。

One catch to syncing a QSlider or QDial to a QDoubleSpinBox is that when the QDoubleSpinBox updates the QDial or QSlider, the QDial or QSlider valueChanged signal will be triggered causing them to update the QDoubleSpinBox in return. This causes an issue where you cannot enter decimal values into the QDoubleSpinBox, because when you enter a decimal ".", the QDial/Slider update overwrites the decimal, blocking you from entering the rest of the decimal value.

To fix this, have each input widget check if their value already matches the value in the widget they are writing to, and if they do, then do not update the value. Here is an example of a function that allows a QDial and QDoubleSpinBox to update each other:

    def syncDialAndSpinbox(self, widget_in, widget_out):
        if isinstance(widget_in, QtWidgets.QDial):
            value =  float(widget_in.value())*100.0/float(widget_in.maximum())
            out_value = widget_out.value()
        else:
            value = round(widget_in.value()*widget_out.maximum()/100)
            out_value = widget_out.value()

        if value != out_value:
            widget_out.setValue(value)

In this example, the QDoubleSpinBox is a percentage (0.0-100.0) and the QDial has 100,000 steps to support a float output of percentages to three decimal places (0.000% to 100.000%).

轻拂→两袖风尘 2025-01-02 01:39:51

在这个非常相似的问题中有一些关于如何创建自己的插槽并发出信号的示例代码:

使用创建的向量作为 QDoubleSpinBox 和 QSlider 的范围

There is some example code on how to create your own slots and emit signals in this very similar question:

use a created vector as range for QDoubleSpinBox and QSlider

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文