scantool配置界面禁用滚轮,增加滑块与spinbox关联

This commit is contained in:
yangjiaxuan 2024-04-26 14:11:33 +08:00
parent 4b62a7bba0
commit 0db6f10642
2 changed files with 134 additions and 5 deletions

View File

@ -11,6 +11,7 @@
#include <QSpinBox>
#include <QSpacerItem>
#include <QPushButton>
#include <QEvent>
Form_DeviceConfig::Form_DeviceConfig(SANE_Handle devHandle, const std::vector<DeviceConfig>& deviceConfigs, QWidget *parent)
: QWidget(parent)
@ -164,6 +165,19 @@ std::vector<DeviceConfig> Form_DeviceConfig::GetDeviceConfigs()
return deviceConfigs;
}
bool Form_DeviceConfig::eventFilter(QObject *target, QEvent *event)
{
if (typeid(*target) == typeid(QSlider) || typeid(*target) == typeid(QComboBox) || typeid(*target) == typeid(QSpinBox))
{
if (event->type() == QEvent::Wheel)
{
return true;
}
}
return QWidget::eventFilter(target, event);
}
void Form_DeviceConfig::Init(SANE_Handle devHandle)
{
// 1.重置设备
@ -347,6 +361,9 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
// 创建控件
QWidget *ctrl = nullptr;
QWidget *extraCtrl = nullptr;
QWidget* sliderSpinWidget = new QWidget(scrollArea);
if (1 == deviceConfig.rangeType)
{
assert(1 == deviceConfig.valueType);
@ -354,6 +371,7 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
for (int k = 0; k < (int)deviceConfig.stringValueList.size(); ++k)
comboBox->addItem(QString::fromStdString(deviceConfig.stringValueList[k]));
//comboBox->setCurrentText(QString::fromStdString(deviceConfig.stringValue));
comboBox->installEventFilter(this);
ctrl = comboBox;
}
else if (2 == deviceConfig.rangeType)
@ -363,6 +381,7 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
for (int k = 0; k < (int)deviceConfig.intValueList.size(); ++k)
comboBox->addItem(QString::number(deviceConfig.intValueList[k]));
//comboBox->setCurrentText(QString::number(deviceConfig.intValue));
comboBox->installEventFilter(this);
ctrl = comboBox;
}
else if (3 == deviceConfig.rangeType)
@ -372,6 +391,7 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
for (int k = 0; k < (int)deviceConfig.doubleValueList.size(); ++k)
comboBox->addItem(QString::number(deviceConfig.doubleValueList[k]));
//comboBox->setCurrentText(QString::number(deviceConfig.doubleValue));
comboBox->installEventFilter(this);
ctrl = comboBox;
}
else if (4 == deviceConfig.rangeType)
@ -381,7 +401,23 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
slider->setOrientation(Qt::Horizontal);
slider->setRange(deviceConfig.intValueMin, deviceConfig.intValueMax);
//slider->setValue(deviceConfig.intValue);
slider->installEventFilter(this);
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(on_sliderClicked(int)));
QSpinBox* spinBox = new QSpinBox;
spinBox->setMinimumWidth(75);
spinBox->setMaximumWidth(75);
spinBox->setRange(deviceConfig.intValueMin, deviceConfig.intValueMax);
spinBox->setProperty("config_name", QString::fromStdString(deviceConfig.name));
spinBox->installEventFilter(this);
connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(on_spinBoxClicked(int)));
ctrl = slider;
extraCtrl = spinBox;
QHBoxLayout* hLayout = new QHBoxLayout;
hLayout->addWidget(ctrl);
hLayout->addWidget(extraCtrl);
sliderSpinWidget->setLayout(hLayout);
}
else if (5 == deviceConfig.rangeType)
{
@ -390,7 +426,25 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
slider->setOrientation(Qt::Horizontal);
slider->setRange(round(deviceConfig.doubleValueMin * 100), round(deviceConfig.doubleValueMax * 100));
//slider->setValue(round(deviceConfig.doubleValue * 100));
slider->installEventFilter(this);
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(on_sliderClicked(int)));
QDoubleSpinBox* doubleSpinBox = new QDoubleSpinBox;
doubleSpinBox->setMinimumWidth(75);
doubleSpinBox->setMaximumWidth(75);
doubleSpinBox->setRange(round(deviceConfig.doubleValueMin * 100), round(deviceConfig.doubleValueMax * 100));
doubleSpinBox->setDecimals(2);
doubleSpinBox->setSingleStep(0.01);
doubleSpinBox->setProperty("config_name", QString::fromStdString(deviceConfig.name));
doubleSpinBox->installEventFilter(this);
connect(doubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(on_doubleSpinboxClicked(double)));
ctrl = slider;
extraCtrl = doubleSpinBox;
QHBoxLayout* hLayout = new QHBoxLayout;
hLayout->addWidget(ctrl);
hLayout->addWidget(extraCtrl);
sliderSpinWidget->setLayout(hLayout);
}
else if (0 == deviceConfig.rangeType)
{
@ -401,13 +455,11 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
else if (2 == deviceConfig.valueType)
{
QSpinBox* spinBox = new QSpinBox;
#ifdef HG_CMP_MSC
spinBox->setMinimumWidth(75);
#else
spinBox->setMinimumWidth(150);
#endif
spinBox->setMaximumWidth(75);
spinBox->setRange(-1, 1000);
//spinBox->setValue(deviceConfig.intValue);
spinBox->installEventFilter(this);
ctrl = spinBox;
}
else if (3 == deviceConfig.valueType)
@ -428,7 +480,14 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
m_ctrlList.push_back(ctrl);
}
layout->addRow(label, ctrl);
if (nullptr != extraCtrl)
{
layout->addRow(label, sliderSpinWidget);
}
else
{
layout->addRow(label, ctrl);
}
}
tabWidget->addTab(scrollArea, QString::fromStdString(m_baseDeviceConfigsGroups[i].groupTitle));
@ -515,3 +574,67 @@ void Form_DeviceConfig::on_defaultBtn_clicked()
{
Update(m_baseDeviceConfigsGroups);
}
void Form_DeviceConfig::on_sliderClicked(int value)
{
QSlider *slider = qobject_cast<QSlider*>(sender());
QString sliderProverty = slider->property("config_name").toString();
QList<QAbstractSpinBox*> buttons = this->findChildren<QAbstractSpinBox*>();
foreach(QAbstractSpinBox* abstractSpinBox, buttons)
{
QString spinBoxProverty = abstractSpinBox->property("config_name").toString();
if (spinBoxProverty == sliderProverty)
{
if (typeid(*abstractSpinBox) == typeid(QSpinBox))
{
QSpinBox* spinBox = reinterpret_cast<QSpinBox*>(abstractSpinBox);
spinBox->setValue(value);
}
else
{
QDoubleSpinBox* doubleSpinBox = reinterpret_cast<QDoubleSpinBox*>(abstractSpinBox);
double temp = value * doubleSpinBox->singleStep();
doubleSpinBox->setValue(temp);
}
break;
}
}
}
void Form_DeviceConfig::on_spinBoxClicked(int value)
{
QSpinBox* spinBox = qobject_cast<QSpinBox*>(sender());
QString spinBoxProverty = spinBox->property("config_name").toString();
QList<QSlider*> buttons = this->findChildren<QSlider*>();
foreach(QSlider* slider, buttons)
{
QString sliderProverty = slider->property("config_name").toString();
if (sliderProverty == spinBoxProverty)
{
slider->setValue(value);
break;
}
}
}
void Form_DeviceConfig::on_doubleSpinboxClicked(double value)
{
QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox*>(sender());
QString spinBoxProverty = spinBox->property("config_name").toString();
QList<QSlider*> buttons = this->findChildren<QSlider*>();
foreach(QSlider* slider, buttons)
{
QString sliderProverty = slider->property("config_name").toString();
if (sliderProverty == spinBoxProverty)
{
int temp = value / spinBox->singleStep() + 0.5;
slider->setValue(temp);
break;
}
}
}

View File

@ -81,12 +81,18 @@ public:
std::vector<DeviceConfig> GetDeviceConfigs();
private:
virtual bool eventFilter(QObject *target, QEvent *event) override;
void Init(SANE_Handle devHandle);
void Update(std::vector<DeviceConfigsGroup> &deviceConfigsGroups);
private slots:
void on_defaultBtn_clicked();
void on_sliderClicked(int value);
void on_spinBoxClicked(int value);
void on_doubleSpinboxClicked(double value);
private:
std::vector<DeviceConfigsGroup> m_baseDeviceConfigsGroups;
std::vector<QWidget*> m_ctrlList;