qt - Draw text on scrollbar -
i wondering if possible draw text inside scrollbar. more specifically, inside "grab-able block" of scroll widget. i'm using pythonqt, can't make own widgets. need default widgets.
way purely in qt?
a more graphical representation of need:
(vanilla: 'x's 'grabbale handle')
-------------------------------------------------------
|......xxxxxxxxxxxx......................................|
-------------------------------------------------------
(target)
-------------------------------------------------------
|......xx custom text here! xx.......................|
-------------------------------------------------------
thanks!
edit: removed claustrophobic pythonqt requirement :)
http://www.qtcentre.org/threads/15032-qscrollbar-subclass-constructor-ignores-orientation-qt4-4
you want similar qscrollbar
subclass.
mainwindow.cpp
#include "mainwindow.h" #include <qscrollarea> #include "scrollbar.h" mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent) { qscrollarea * sa = new qscrollarea; sa->setverticalscrollbar(new scrollbar(qt::vertical)); sa->sethorizontalscrollbar(new scrollbar(qt::horizontal)); qwidget *w = new qwidget; sa->setwidget(w); w->setfixedsize(1000, 1000); this->setcentralwidget(sa); }
scrollbar.h
#ifndef scrollbar_h #define scrollbar_h #include <qscrollbar> #include <qpaintevent> class scrollbar : public qscrollbar { q_object public: scrollbar ( qt::orientation orientation, qwidget * parent = 0 ); public slots: signals: protected: void paintevent(qpaintevent* event); }; #endif // scrollbar_h
scrollbar.cpp
#include "scrollbar.h" #include <qpainter> double map(double x1, double x, double x2, double y1, double y2) { return y1 + (y2 -y1)*(x - x1)/(x2 - x1); } scrollbar::scrollbar( qt::orientation orientation, qwidget * parent ): qscrollbar (orientation, parent) { } void scrollbar::paintevent(qpaintevent * event) { int x, y, w, h; this->rect().getrect(&x,&y,&w,&h); // calls base class's paint calls qscrollbar::paintevent(event); // following painted on top of qpainter p(this); p.setpen(qpen(qt::red,1)); if(this->orientation() == qt::horizontal) { // difficult inspect size of slider out of qscrollbar // , position. draws red box moves slider pos int delta = map(this->minimum(), this->sliderposition(), this->maximum(), 0, this->width()*3/4); p.drawrect(sliderposition(),y,width()/4,h); } else { int delta = map(this->minimum(), this->sliderposition(), this->maximum(), 0, this->height()*3/4); p.drawrect(x,sliderposition(),w,height()/4); } // draw text inside p.drawtext }
determining exact size of slider might possible other methods used qstyle. here information digging through of qt sources:
qscrollbar.cpp
excerpts qt sources
void qscrollbar::paintevent(qpaintevent *) { q_d(qscrollbar); qpainter p(this); qstyleoptionslider opt; initstyleoption(&opt); opt.subcontrols = qstyle::sc_all; if (d->pressedcontrol) { opt.activesubcontrols = (qstyle::subcontrol)d->pressedcontrol; if (!d->pointeroutsidepressedcontrol) opt.state |= qstyle::state_sunken; } else { opt.activesubcontrols = (qstyle::subcontrol)d->hovercontrol; } style()->drawcomplexcontrol(qstyle::cc_scrollbar, &opt, &p, this); }
qcommonstyle.cpp
// style()->drawcomplexcontrol(qstyle::cc_scrollbar if (const qstyleoptionslider *scrollbar = qstyleoption_cast<const qstyleoptionslider *>(opt)) { // make copy here , reset each primitive. qstyleoptionslider newscrollbar = *scrollbar; state saveflags = scrollbar->state; if (scrollbar->subcontrols & sc_scrollbarsubline) { newscrollbar.state = saveflags; newscrollbar.rect = proxy()->subcontrolrect(cc, &newscrollbar, sc_scrollbarsubline, widget); if (newscrollbar.rect.isvalid()) { if (!(scrollbar->activesubcontrols & sc_scrollbarsubline)) newscrollbar.state &= ~(state_sunken | state_mouseover); proxy()->drawcontrol(ce_scrollbarsubline, &newscrollbar, p, widget); } } if (scrollbar->subcontrols & sc_scrollbaraddline) { newscrollbar.rect = scrollbar->rect; newscrollbar.state = saveflags; newscrollbar.rect = proxy()->a (cc, &newscrollbar, sc_scrollbaraddline, widget); if (newscrollbar.rect.isvalid()) { if (!(scrollbar->activesubcontrols & sc_scrollbaraddline)) newscrollbar.state &= ~(state_sunken | state_mouseover); proxy()->drawcontrol(ce_scrollbaraddline, &newscrollbar, p, widget); } } if (scrollbar->subcontrols & sc_scrollbarsubpage) { newscrollbar.rect = scrollbar->rect; newscrollbar.state = saveflags; newscrollbar.rect = proxy()->subcontrolrect(cc, &newscrollbar, sc_scrollbarsubpage, widget); if (newscrollbar.rect.isvalid()) { if (!(scrollbar->activesubcontrols & sc_scrollbarsubpage)) newscrollbar.state &= ~(state_sunken | state_mouseover); proxy()->drawcontrol(ce_scrollbarsubpage, &newscrollbar, p, widget); } } if (scrollbar->subcontrols & sc_scrollbaraddpage) { newscrollbar.rect = scrollbar->rect; newscrollbar.state = saveflags; newscrollbar.rect = proxy()->subcontrolrect(cc, &newscrollbar, sc_scrollbaraddpage, widget); if (newscrollbar.rect.isvalid()) { if (!(scrollbar->activesubcontrols & sc_scrollbaraddpage)) newscrollbar.state &= ~(state_sunken | state_mouseover); proxy()->drawcontrol(ce_scrollbaraddpage, &newscrollbar, p, widget); } } if (scrollbar->subcontrols & sc_scrollbarfirst) { newscrollbar.rect = scrollbar->rect; newscrollbar.state = saveflags; newscrollbar.rect = proxy()->subcontrolrect(cc, &newscrollbar, sc_scrollbarfirst, widget); if (newscrollbar.rect.isvalid()) { if (!(scrollbar->activesubcontrols & sc_scrollbarfirst)) newscrollbar.state &= ~(state_sunken | state_mouseover); proxy()->drawcontrol(ce_scrollbarfirst, &newscrollbar, p, widget); } } if (scrollbar->subcontrols & sc_scrollbarlast) { newscrollbar.rect = scrollbar->rect; newscrollbar.state = saveflags; newscrollbar.rect = proxy()->subcontrolrect(cc, &newscrollbar, sc_scrollbarlast, widget); if (newscrollbar.rect.isvalid()) { if (!(scrollbar->activesubcontrols & sc_scrollbarlast)) newscrollbar.state &= ~(state_sunken | state_mouseover); proxy()->drawcontrol(ce_scrollbarlast, &newscrollbar, p, widget); } } if (scrollbar->subcontrols & sc_scrollbarslider) { newscrollbar.rect = scrollbar->rect; newscrollbar.state = saveflags; newscrollbar.rect = proxy()->subcontrolrect(cc, &newscrollbar, sc_scrollbarslider, widget); if (newscrollbar.rect.isvalid()) { if (!(scrollbar->activesubcontrols & sc_scrollbarslider)) newscrollbar.state &= ~(state_sunken | state_mouseover); proxy()->drawcontrol(ce_scrollbarslider, &newscrollbar, p, widget); if (scrollbar->state & state_hasfocus) { qstyleoptionfocusrect fropt; fropt.qstyleoption::operator=(newscrollbar); fropt.rect.setrect(newscrollbar.rect.x() + 2, newscrollbar.rect.y() + 2, newscrollbar.rect.width() - 5, newscrollbar.rect.height() - 5); proxy()->drawprimitive(pe_framefocusrect, &fropt, p, widget); } } } } break;
hope helps.
Comments
Post a Comment