QScrollBar vertical scrollbar set width
-
@hskoglund , one thing that comes to mind....the slider value is normally at the center point of the handle? Can this be offset? If the handle is adjusted to show a proportional representation then the value would have to be at the very top of the handle.
-
@hskoglund , I'm not sure this is going to work, for starters using the StyleSheet to set the handle width and height doesn't seem to work and there is the other issue where the origin / value seems to be central to the orientation of the handle.
-
Hi, dug up some old code that assumes you have a vertical QSlider with a height of say 400 pixels, minimum = 0, maximum = 400, singleStep = 1 and pageStep = 25:
mpSlider->setStyleSheet( "QSlider::groove { background: transparent; width: 4px; } " "QSlider::handle { background: blue; border-radius: 5px; border: 1px solid gray; height:200px;}");
then moving the slider will give you values between 0 and 400 :-)
Edit:
here's the simplified test program, create a vanilla QWidget app, in mainwindow.cpp and #include "qslider.h" and insert this code in MainWindow::MainWindow() after ui->setupUi(this):auto mpSlider = new QSlider(Qt::Vertical,this); mpSlider->setGeometry(QRect(100,100,100,400)); mpSlider->setRange(0,400); mpSlider->setStyleSheet( "QSlider::groove { background: transparent; width: 4px; } " "QSlider::handle { background: blue; border-radius: 5px; border: 1px solid gray; height:200px;}"); connect(mpSlider,&QSlider::valueChanged,[this](int value) { setWindowTitle("Slider value: " + QString::number(value));});
should give you this:
-
QSlider don't show the current value as far as i know.
there is a handy subclass here to get text the handle.
https://stackoverflow.com/questions/18383885/qslider-show-min-max-and-current-valueAlso do note that a 4 pixel slider will drive people nuts on a hi res screen.
-
@hskoglund , sorry for the delay, been busy with other stuff.
QRect rctGeom(rect()); int intHeight(rctGeom.height()), //Using the maximum and minimum actual values calculate percentage visible intSpan(truth::msintMaximum - truth::msintMinimum); double dblPercent = (double)intHeight / (double)intSpan, //Now work out how much of the scale the percentage into the visible area dblVisible = dblPercent * intHeight; QString strStyle(QString("QSlider::handle {" "background:blue;" "border-radius: 0px;" "border: 1px solid gray;" "height: %1px;}").arg((int)dblVisible)); truth::mspSlider->setStyleSheet(strStyle);
intHeight contains 486
intSpan contains 2000
dblPercent contains 0.243
dblVisible contains 118.098
strStyle contains:QSlider::handle {background:blue;border-radius: 0px;border: 1px solid gray;height: 118px;}
However what I'm seeing on the screen is:
So clearly the handle is not 118 px, what haven't I done? -
Hi, I also stumbled on this (QSlider refusing to cooperate). What's odd here is to make that height of 118px work, you also have to mess with QSlider's groove in your setStyleShee() call. And the easiest way was to make it invisible, like this:
QString strStyle(QString( "QSlider::groove { background: transparent; } " "QSlider::handle {" "background:blue;" "border-radius: 0px;" "border: 1px solid gray;" "height: %1px;}").arg((int)dblVisible));
Ok problem solved, but now you got another problem: no groove.
If you need one, you can draw your own, I used a QFrame say like this:auto pFrame = new QFrame(mpSlider->parentWidget()); pFrame->setGeometry(mpSlider->geometry()); pFrame->setFrameStyle(QFrame::VLine | QFrame::Sunken); pFrame->stackUnder(mpSlider); // z-order (important!)
note: maybe some more settings are needed on that QFrame for grey color etc. to make it look more like a QSlider's groove :-)