user1633254
user1633254

Reputation:

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: enter image description here

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?

Upvotes: 19

Views: 66200

Answers (4)

Gaurang
Gaurang

Reputation: 87

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

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

Upvotes: 1

AlexBee
AlexBee

Reputation: 368

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().

Upvotes: 1

J Griffiths
J Griffiths

Reputation: 729

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

Upvotes: 0

cmannett85
cmannett85

Reputation: 22346

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.

Upvotes: 33

Related Questions