scale - Fixed text inside or adjacent to QProgressBar with scaling font size in Qt -
i new qt
(using qt creator) , qprogressbar
. interested in learning how have fixed text value (not value of progress bar) inside or adjacent left of qprogressbar
, have font size scale according size of progress bar.
for example:
or
i have considered using qlabel
failed , not find examples online.
any code sample illustrating solution me understand , learn appreciated.
if label inside progressbar do, here example. might not want, should send in right direction. adjust font size in resize event. in example font size calculated based on size of label, same size progress bar.
#include <qapplication> #include <qprogressbar> #include <qwidget> #include <qlabel> #include <qlayout> #include <qtimer> class widget : public qwidget { q_object qprogressbar progressbar; qlabel *label; public: widget(qwidget *parent = nullptr) : qwidget(parent) { progressbar.setrange(0, 100); progressbar.setvalue(20); progressbar.settextvisible(false); progressbar.setsizepolicy(qsizepolicy::preferred, qsizepolicy::minimum); label = new qlabel(&progressbar); label->settext("hello world!"); setlayout(new qhboxlayout); layout()->addwidget(&progressbar); } protected: void resizeevent(qresizeevent *) { label->resize(progressbar.size()); qfontmetrics fm(label->font()); float multiplier_horizontal = (float)label->width() / fm.width(label->text()); float multiplier_vertical = (float)label->height() / fm.height(); qfont font = label->font(); font.setpointsize(font.pointsize() * qmin(multiplier_horizontal, multiplier_vertical)); label->setfont(font); } }; int main(int argc, char *argv[]) { qapplication a(argc, argv); widget w; w.show(); return a.exec(); } #include "main.moc"
Comments
Post a Comment