Trouble with custom QSlider
-
@SGaist Sorry, by "put it on the stack" do you mean use a member variable? Or somehow access the painter object without instantiating it each time?
I went back to QSlider but setting the max-width for the groove doesn't change the width of the handle, it still extends to the wide size policy of the widget.
-
@Smeeth
I think he is referring to theauto painter = new QPainter(this);
like that painter get destroyed only with the form
so you should change that ...
and also is getting a new place in memory everytime you touch the slider - careful with the memory management -
@arsinte_andrei Got it. So how can I get the painter without calling new QPainter(this)?
-
QPainter painter(this);
-
@Smeeth here you have two options -
- delete
private: QPainter *painter;
and
painter = new QPainter(this);
and inside the paint event add
void AlarmLimitsSlider::paintEvent(QPaintEvent *ev) { QPainter painter; //TODO the painting
- options
you have declared the painter as a global variable... so make a global init
so in your constructor add
painter = new QPainter(this);
and delete it from the paint event
-
@arsinte_andrei Oh I see, for some reason I thought you had to create a new Painter for each time the paint event is triggered. I will try this solution.
-
Based on the documentation of QPainter:
The common use of QPainter is inside a widget's paint event: Construct and customize (e.g. set the pen or the brush) the painter. Then draw. Remember to destroy the QPainter object after drawing. For example:
-
so as SGaist sugest.. and he is right... at option 2 keep
painter = new QPainter(this);
the first line in the paintEvent and add
delete painter;
as your last line in the paint event...
but anyway I do not understand why will you want option 2 when option 1 is the best???
-
@arsinte_andrei said in Trouble with custom QSlider:
so as SGaist sugest.. and he is right... at option 2 keep
painter = new QPainter(this);
the first line in the paintEvent and add
delete painter;
as your last line in the paint event...
but anyway I do not understand why will you want option 2 when option 1 is the best???
It's not what I suggested, use a stack base painter, there's really no need to allocate if on the heap.
-
@SGaist you pointed at the option 2.. but if you look at option 1 is exactly what you have said.. this is what I've recommended...
void AlarmLimitsSlider::paintEvent(QPaintEvent *ev) { QPainter painter; //TODO the painting
or
void AlarmLimitsSlider::paintEvent(QPaintEvent *ev) { QPainter painter(this); //TODO the painting
same thing.. - except the second one has parent
-
It's not the same thing. It's not a parent, it's the paint device that the painter will be active on. If you don't pass it, you will have to call begin and end explicitly. See the constructor documentation.
-
Hmm.... After years pf doing Qt programming I have to agree that I still have a lot to learn about... I do apologize for the wrong information that I've provided....
Best explanation... God bless SGaist...