[英]Qt: Creating a QDoubleSlider
I want to connect a QDoubleSpinBox
with a QSlider
like this: 我想一个连接
QDoubleSpinBox
与QSlider
这样的:
QObject::connect(ui->myDoubleSpinBox, SIGNAL(valueChanged(double)),
ui->mySlider, SLOT(setValue(double)));
QObject::connect(ui->mySlider, SIGNAL(valueChanged(double)),
ui->myDoubleSpinBox, SLOT(setValue(double)));
This won't work, for a QSlider only handles int values. 这不起作用,因为QSlider只处理int值。 So I think i need to add a custom slot to QSlider.
所以我想我需要为QSlider添加一个自定义插槽。
I thought about creating a new class derived from QSlider and then implementing the slot, like this: 我想创建一个从QSlider派生的新类,然后实现插槽,如下所示:
QDoubleSlider.hpp QDoubleSlider.hpp
#ifndef QDOUBLESLIDER_H
#define QDOUBLESLIDER_H
#include <QSlider>
class QDoubleSlider : public QSlider
{
Q_OBJECT
public:
explicit QDoubleSlider(QObject *parent = 0);
signals:
public slots:
void setValue(double givenValue);
};
#endif // QDOUBLESLIDER_H
QDoubleSlider.cpp QDoubleSlider.cpp
#include "qdoubleslider.h"
QDoubleSlider::QDoubleSlider(QObject *parent) :
QSlider(parent)
{
}
void QDoubleSlider::setValue(double givenValue)
{
// code
}
Now I have two problems: 现在我有两个问题:
QObject*
to QWidget*
in the constructor. QObject*
到QWidget*
转换无效。 Any ideas? 有任何想法吗?
parent
needs to be a QWidget*
, just as the error states parent
必须是QWidget*
,正如错误所述 double
as a member and set the underlying slider to the appropriate integer equivalent. double
存储为成员,并将基础滑块设置为适当的等效整数。 Remember to only send valueChanged
signals if the value really changes. valueChanged
信号。 Furthermore you should probably inherit from QWidget
instead and have a QSlider
as a child, as you do not want users of your class to change the integer range of your internal slider. 此外,您可能应该继承
QWidget
并将QSlider
作为子项继承,因为您不希望类的用户更改内部滑块的整数范围。
QSlider ( QWidget * parent = 0 )
QSlider ( QWidget * parent = 0 )
void QDoubleSlider::setDoubleValue(double givenValue) { setValue(static_cast<int>(givenValue)); }
void QDoubleSlider::setDoubleValue(double givenValue) { setValue(static_cast<int>(givenValue)); }
void QDoubleSlider::setDoubleValue(double givenValue) { setValue(static_cast<int>(givenValue)); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.