我使用的是QT QDouleSpinBox,单步值为1.0
当我改变这个值时,它每增加一次就会改变一次。
当我按住Control键并改变值时,每增加一次就会增加10.0
现在我想添加Alt键并将每个增量改为0.1,我该怎么做呢?
我正在尝试用这个类在QT设计器中推广QDoubleSpinBox小部件。
如何实现stepBy函数?
#pragma once
#include< QDoubleSpinBox>
class spinboxsumit : public QDoubleSpinBox
{
Q_OBJECT
public:
spinboxsumit(QWidget * parent = 0);
void stepBy(double steps);
};
/////////////////////////////////////////////////////////////////////////////////////////////////
#include "spinboxsumit.h"
spinboxsumit::spinboxsumit(QWidget * parent) : QDoubleSpinBox( parent)
{
}
void spinboxsumit::stepBy(double steps)
{
}发布于 2019-11-24 21:47:28
问得好。考虑到可用修改键的数量,更灵活的修改键会更好!对于这一点,没有什么“内置”。我看到你正在尝试重新实现一个自定义版本...这也是我的想法。
这是我能想到的(几乎)最简单的版本。顺便说一句,由于某些原因,ALT修饰符对我(Win7)和鼠标滚轮不起作用(根本没有调整,即使是“股票”旋转框),所以我在这里使用SHIFT作为测试的修饰符。(不知道为什么Alt+wheel不工作,可能是我的系统出问题了。)
#include <QDoubleSpinBox>
#include <QApplication>
class DoubleBox : public QDoubleSpinBox
{
Q_OBJECT
public:
using QDoubleSpinBox::QDoubleSpinBox; // inherit c'tors
// re-implement to keep track of default step (optional, could hard-code step in stepBy())
void setSingleStep(double val)
{
m_defaultStep = val;
QDoubleSpinBox::setSingleStep(val);
}
// override to adjust step size
void stepBy(int steps) override
{
// set the actual step size here
double newStep = m_defaultStep;
if (QApplication::queryKeyboardModifiers() & Qt::ShiftModifier)
newStep *= 0.1;
// be sure to call the base setSingleStep() here to not change m_defaultStep.
QDoubleSpinBox::setSingleStep(newStep);
QDoubleSpinBox::stepBy(steps);
}
private:
double m_defaultStep = 1.0;
};还有一个快速测试:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog d;
d.setLayout(new QVBoxLayout);
d.layout()->addWidget(new DoubleBox(&d));
return d.exec();
}
#include "main.moc"更进一步,可以重新实现更完整的stepBy(int)版本(current source),或者通过重新实现wheel/key事件在较低级别重新实现。
https://stackoverflow.com/questions/59017792
复制相似问题