Как в QSpinBox, QDoubleSpinBox задать некоторый множественный диапазон? Примеры как должен работать этот SpinBox, аналогично для DoubleSpinBox: 1) Должен принимать 0, и значения в диапазоне 100-200. 2) Должен принимать значения в диапазоне от 30 до 50, 80-100. 3) Должен принимать числа 10, 20, 30, и числа в диапазоне 50-60. Сейчас у меня есть реализация только для одного диапазона который принимает SpinBox, код прикрепляю. Как лучше(грамотно) сделать реализацию для диапазона с множеством значений? Буду благодарен минимальным примерам.
main.cpp
#include <QApplication>
#include "spinboxvalidate.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SpinBoxIntValidator spinBox;
spinBox.setRange(500,600); // Здесь передать перечень диапазонов
spinBox.setToolTip(QString("Введите значение в диапазоне от %1 до %2").arg(500).arg(600));
spinBox.resize(100,100);
spinBox.show();
return a.exec();
}
spinboxvalidate.h
#ifndef SPINBOXVALIDATE_H
#define SPINBOXVALIDATE_H
#include <QToolTip>
#include <QValidator>
#include <QSpinBox>
#include <QMouseEvent>
#include <QStyle>
#include <QStyleOptionSpinBox>
class SpinBoxIntValidator : public QSpinBox
{
Q_OBJECT
public:
explicit SpinBoxIntValidator(QWidget *parent = 0);
virtual QIntValidator::State validate(QString &str, int &pos) const;
protected:
virtual void mousePressEvent(QMouseEvent *event);
virtual void keyPressEvent(QKeyEvent *event);
virtual void wheelEvent(QWheelEvent * event);
};
#endif // SPINBOXVALIDATE_H
#ifndef DOUBLESPINBOXVALIDATE_H
#define DOUBLESPINBOXVALIDATE_H
#include <QToolTip>
#include <QValidator>
#include <QDoubleSpinBox>
#include <QDoubleValidator>
#include <QMouseEvent>
#include <QStyle>
#include <QStyleOptionSpinBox>
#include <QApplication>
#include <QVariant>
class DoubleSpinBoxIntValidator : public QDoubleSpinBox
{
Q_OBJECT
public:
explicit DoubleSpinBoxIntValidator(QWidget *parent = 0) : QDoubleSpinBox(parent)
{
}
virtual QDoubleValidator::State validate(QString &str, int &pos) const;
protected:
virtual void mousePressEvent(QMouseEvent *event);
virtual void keyPressEvent(QKeyEvent *event);
virtual void wheelEvent(QWheelEvent * event);
};
#endif // DOUBLESPINBOXVALIDATE_H
spinboxvalidate.cpp
#include "spinboxvalidate.h"
SpinBoxIntValidator::SpinBoxIntValidator(QWidget *parent) : QSpinBox(parent)
{
}
QIntValidator::State SpinBoxIntValidator::validate(QString &str, int &pos) const
{
QIntValidator validator;
validator.setRange(minimum(), maximum());
QIntValidator::State state = validator.validate(str, pos);
if (state == QIntValidator::Invalid)
{
QToolTip::showText(QCursor::pos(), toolTip());
return state;
}
else if (state == QIntValidator::Intermediate)
{
return state;
}
else
{
return state;
}
}
void SpinBoxIntValidator::mousePressEvent(QMouseEvent *event)
{
QStyleOptionSpinBox opt;
this->initStyleOption(&opt);
if (this->style()->subControlRect(QStyle::CC_SpinBox, &opt,
QStyle::SC_SpinBoxUp).contains(event->pos()))
{
if (this->value() == maximum())
{
QToolTip::showText(QCursor::pos(), toolTip());
}
}
else if (this->style()->subControlRect(QStyle::CC_SpinBox, &opt,
QStyle::SC_SpinBoxDown).contains(event->pos()))
{
if (this->value() == minimum())
{
QToolTip::showText(QCursor::pos(), toolTip());
}
}
QSpinBox::mousePressEvent(event);
event->accept();
}
void SpinBoxIntValidator::keyPressEvent(QKeyEvent *event)
{
QSpinBox::keyPressEvent(event);
if (event->key() == Qt::Key_Up)
{
if (this->value() == maximum())
{
QToolTip::showText(QCursor::pos(), toolTip());
}
}
else if (event->key() == Qt::Key_Down)
{
if (this->value() == minimum())
{
QToolTip::showText(QCursor::pos(), toolTip());
}
}
}
void SpinBoxIntValidator::wheelEvent(QWheelEvent * event)
{
const int steps = (event->delta() > 0 ? 1 : -1);
if ((steps == -1 && this->value() == minimum()) || (steps == 1 && this->value() == maximum()))
{
QToolTip::showText(QCursor::pos(), toolTip());
}
else if (stepEnabled() & (steps > 0 ? StepUpEnabled : StepDownEnabled))
{
stepBy(event->modifiers() & Qt::ControlModifier ? steps * 10 : steps);
}
event->accept();
}
QDoubleValidator::State DoubleSpinBoxIntValidator::validate(QString &str, int &pos) const
{
QDoubleValidator validator(NULL);
validator.setNotation(QDoubleValidator::StandardNotation);
validator.setRange(minimum(),maximum(), 2);
QDoubleValidator::State state = validator.validate(str, pos);
if (state == QDoubleValidator::Invalid)
{
QToolTip::showText(QCursor::pos(), toolTip());
return state;
}
else if (state == QDoubleValidator::Acceptable)
{
return state;
}
if (state == QDoubleValidator::Intermediate && (str.toDouble() > maximum()) || (str.toDouble() < minimum() && pos > 1))
{
QToolTip::showText(QCursor::pos(), toolTip());
return QValidator::Invalid;
}
else
{
return state;
}
}
void DoubleSpinBoxIntValidator::mousePressEvent(QMouseEvent *event)
{
QStyleOptionSpinBox opt;
this->initStyleOption(&opt);
if (this->style()->subControlRect(QStyle::CC_SpinBox, &opt,
QStyle::SC_SpinBoxUp).contains(event->pos()))
{
if (this->value() == maximum())
{
QToolTip::showText(QCursor::pos(), toolTip());
}
}
else if (this->style()->subControlRect(QStyle::CC_SpinBox, &opt,
QStyle::SC_SpinBoxDown).contains(event->pos()))
{
if (this->value() == minimum())
{
QToolTip::showText(QCursor::pos(), toolTip());
}
}
QDoubleSpinBox::mousePressEvent(event);
event->accept();
}
void DoubleSpinBoxIntValidator::keyPressEvent(QKeyEvent *event)
{
QDoubleSpinBox::keyPressEvent(event);
if (event->key() == Qt::Key_Up)
{
if (this->value() == maximum())
{
QToolTip::showText(QCursor::pos(), toolTip());
}
}
else if (event->key() == Qt::Key_Down)
{
if (this->value() == minimum())
{
QToolTip::showText(QCursor::pos(), toolTip());
}
}
}
void DoubleSpinBoxIntValidator::wheelEvent(QWheelEvent * event)
{
const int steps = (event->delta() > 0 ? 1 : -1);
if ((steps == -1 && this->value() == minimum()) || (steps == 1 && this->value() == maximum()))
{
QToolTip::showText(QCursor::pos(), toolTip());
}
else if (stepEnabled() & (steps > 0 ? StepUpEnabled : StepDownEnabled))
{
stepBy(event->modifiers() & Qt::ControlModifier ? steps * 10 : steps);
}
event->accept();
}
Кофе для программистов: как напиток влияет на продуктивность кодеров?
Рекламные вывески: как привлечь внимание и увеличить продажи
Стратегії та тренди в SMM - Технології, що формують майбутнє сьогодні
Выделенный сервер, что это, для чего нужен и какие характеристики важны?
Современные решения для бизнеса: как облачные и виртуальные технологии меняют рынок
Столкнулся с проблемой, но на существующих топиках об этой проблеме не нашел решенияЯ в затруднении, все include'ы правильно расставлены вроде,...
у меня есть std::set с кастомным компаратором, в set я кладу свой тип данных, который содержит два параметра, уникальность должна обеспечиваться...
Начал изучать Java и Spring frameworkДелаю задание, часть которого - хранение набора точек с привязкой к какой-то площади (не важно к какой, к вопросу...
Могу ли я в контексте Spring + Hibernate сделать простой sql запрос и получить список элементов ( как в JDBC - ResultSet) для запроса данных из таблицы, не входящей...