Qslider报告Valuechanged的不正确值()
我正在尝试将QSlider
对象连接到qlineedit
,以使用户能够使用滑块或直接输入指定值或直接输入为表单。这里的目标是,当滑块位置更改时,我们在Qlineedit
box中更新文本,反之亦然。但是,当我尝试报告qslider-> valuechanged()
的值时,无论将滑块位置设置在何处,我都会恢复1个值。我在做什么错误?
这是我的MWE :(
QSlider* decay_slider = new QSlider(Qt::Horizontal, this);
decay_slider->setMinimum(1);
decay_slider->setMaximum(11000); // increments of 0.1 years
decay_slider->setTickPosition(QSlider::TicksBothSides);
QLineEdit* le_decay_time = new QLineEdit(this);
// Map slider position signal to LineEdit text update
QSignalMapper* mapper = new QSignalMapper(this);
QObject::connect(mapper,
SIGNAL(mapped(const QString&)),
le_decay_time,
SLOT(setText(const QString&)));
QObject::connect(decay_slider, SIGNAL(valueChanged(int)), mapper, SLOT(map()));
mapper->setMapping(decay_slider,
QString::number(decay_slider->sliderPosition()));
这是QT-5.15,如果很重要。)
I'm trying to connect a QSlider
object to a QLineEdit
such to enable a user to either specify a value using the slider or direct input into a form. The goal here is when the slider position changes, we update the text in the QLineEdit
box and vice versa. However, when I try to report out the value of QSlider->valueChanged()
, I'm just getting back a value of 1, regardless of where the slider position is set to. What am I doing incorrectly?
Here is my MWE:
QSlider* decay_slider = new QSlider(Qt::Horizontal, this);
decay_slider->setMinimum(1);
decay_slider->setMaximum(11000); // increments of 0.1 years
decay_slider->setTickPosition(QSlider::TicksBothSides);
QLineEdit* le_decay_time = new QLineEdit(this);
// Map slider position signal to LineEdit text update
QSignalMapper* mapper = new QSignalMapper(this);
QObject::connect(mapper,
SIGNAL(mapped(const QString&)),
le_decay_time,
SLOT(setText(const QString&)));
QObject::connect(decay_slider, SIGNAL(valueChanged(int)), mapper, SLOT(map()));
mapper->setMapping(decay_slider,
QString::number(decay_slider->sliderPosition()));
(This is Qt-5.15, if it matters.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用
setMapping
硬编码sliderPosition
的值。好东西,我们现在有Lambdas,因此您可以用以下方式替换整个第二段
Your call to
setMapping
hard-codes the value ofsliderPosition
to the initial version.Good thing we have lambdas now, so you can replace the entire second paragraph with: