60 setSizePolicy(copySlider->sizePolicy());
61 setMinimumSize(copySlider->minimumSize());
62 setMaximumSize(copySlider->maximumSize());
63 setFocusPolicy(copySlider->focusPolicy());
64 setOrientation(copySlider->orientation());
67 setToolTip(copySlider->toolTip());
76 font = QFont(
"Arial", 13);
77 slideHandleMargin = 2;
92 unsigned int digits = 0;
109 void TextBubbleSlider::setMaxPixelWidth()
118 QString maximumWidthString;
119 for (
int i = 0;
i < maxNumDigits;
i++) {
120 maximumWidthString.append(
"0");
124 QFontMetrics fontMetrics(font);
125 maximumFontWidth = fontMetrics.width(QString(
"%1").arg(maximumWidthString));
126 maximumFontHeight = fontMetrics.height();
129 slideHandleWidth = maximumFontWidth + 6;
130 setStyleSheet(QString(
"QSlider::handle:horizontal { width: %1px; margin: -5px 0;}")
131 .arg(slideHandleWidth));
142 QSlider::setMinimum(max);
156 QSlider::setMaximum(max);
181 int sliderHeight = height();
182 const double indicatorWidth = 5.0;
186 indicatorValue >= (minimum() * -0.1) && indicatorValue <= (1.1 * maximum());
187 qreal indicatorPos = 0;
188 QPen indicatorPen(QBrush(QColor(255, 0, 0, 180)), indicatorWidth, Qt::SolidLine, Qt::RoundCap,
192 p.setRenderHint(QPainter::Antialiasing);
193 p.setPen(indicatorPen);
195 p.drawLine(QLineF(indicatorPos, indicatorWidth / 2.0, indicatorPos,
196 sliderHeight - indicatorWidth / 2.0));
200 QSlider::paintEvent(paintEvent);
203 QPainter painter(
this);
204 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
208 indicatorPen.setColor(QColor(255, 0, 0, 50));
209 painter.setPen(indicatorPen);
210 painter.drawLine(QLineF(indicatorPos, indicatorWidth / 2.0, indicatorPos,
211 sliderHeight - indicatorWidth / 2.0));
218 painter.setFont(font);
219 painter.setPen(Qt::SolidLine);
220 QString neutralStringWidth = QString(
"%1").arg(value());
221 QFontMetrics fontMetrics(font);
222 int textWidth = fontMetrics.width(neutralStringWidth);
223 painter.drawText(QRectF(valuePos + maximumFontWidth - textWidth,
224 ceil((sliderHeight - maximumFontHeight) / 2.0), textWidth,
231 bool changed = indicatorValue != us;
239 qreal offset = invertedAppearance() ? maximum() - val : val - minimum();
240 return slideHandleWidth / 2 + slideHandleMargin +
241 offset / (maximum() - minimum()) * (width() - (slideHandleWidth + slideHandleMargin) - 1);
TextBubbleSlider(QWidget *parent=nullptr)
TextBubbleSlider::TextBubbleSlider Constructs a regular text-bubble slider.
qreal sliderPosFromValue(const int val)
Creates a slider with a text bubble showing the slider value.
void setMaximum(int)
TextBubbleSlider::setMaximum Reimplements setMaximum. Ensures that the slider handle is the correct s...
void setHidden(bool)
Hide the slider by not painting it.
void setMinimum(int)
TextBubbleSlider::setMinimum Reimplements setMinimum. Ensures that the slider handle is the correct s...
unsigned int numIntegerDigits(int number)
numIntegerDigits Counts the number of digits in an integer
void construct()
TextBubbleSlider::construct This function needs to be called from all constructors. It provides a single point where settings can be changed.
void setIndicatorValue(int us)
void paintEvent(QPaintEvent *event)
TextBubbleSlider::paintEvent Reimplements QSlider::paintEvent.