Set Qlabel width

I'm trying to make a ratio between 2 numbers (likes & dislikes).

The result is a number in percent. Then I want to make the width from the label equal that percent.

So If there are 100 likes and 1 dislike. I want to make the dislike label 1 percent the width of the like bar. So basically like the youtube rating system.

Example image:

But I don't see any function to set the width of the label. So how do I need to do this, anyone have suggestions?

Answers


QLabel::setFixedWidth(int). Though to make it more flexible (don't have to worry about layouts changing things), I would subclass QWidget, add slots for the two numbers, and reimplement paintEvent(..) to draw the two sections.


Hm, i`m just using resize() method. For example my widget, implemented from QLabel:

BenchItem *itm=static_cast<BenchItem*>(widget); itm->resize(this->width(),itm->height());

i don`t need change height so it uses itself height, but width should change due to parents width().


I have used setGeometry() method to set the QLabel width dynamically. I have qlabel in Form Ui.

ui->qlabel->setGeometry(x, y, width, height);

Looking at QT documentation, this element can only contain

"Plain text, Rich text, A pixmap, A movie, A number, or Nothing"

where the most promising of these, "pixmap", seems not to have the image manipulation functionality you would need, instead being more about displaying images:

http://qt-project.org/doc/qt-4.8/qpixmap.html

Instead, I'd suggest looking into more lightweight solutions for your bar graph problem, like CSS. Here's a way to do this using two div elements:

http://www.1080degrees.net/archive/journal/simple_css_bar_graph/

If you prefer displaying an image after all, and have GD libraries enabled, perhaps have a look here:

http://php.net/manual/en/function.imagecreate.php


Need Your Help

How to vertically align both image and text in a DIV using CSS?

html css image

I have an image and some text inside a div and I'd like to put the image and the text in the vertical center of the div using CSS. The problem is that I don't know how many lines of text I will hav...

Is it possible in android to define constants in XML that vary with configuration

android xml layout constants

I am interested in having an XML layouts in Android change size depending on landscape or portrait viewing (and maybe other configurations later).