MistyD
MistyD

Reputation: 17223

Cannot set width of QLabel at runtime

I am doing this

ui.label->geometry().setWidth(12);

However the error I get is

Error   1   error C2662: 'QRect::setWidth' : cannot convert 'this' pointer from 'const QRect' to 'QRect &'  

Any suggestions on how to resolve this ?

Upvotes: 1

Views: 676

Answers (2)

Zlatomir
Zlatomir

Reputation: 7034

Geometry returns a const QRect reference, so you need to use it like this:

QRect r = ui.label->geometry();
r.setWidth(12);
ui.label->setGeometry(r);

Or you can use resize:

ui.label->resize(12, ui.label->height());

But you can also tell us what are you trying to accomplish and maybe we can find a solution that puts the QLabel into a layout and you won't need to manually resize it.

Upvotes: 1

Predelnik
Predelnik

Reputation: 5246

geometry() returns you const reference to QRect so it can only be used as read-only information.

Not very beautiful way but you may try calling setMinimumWidth(), setMaximumWidth() functions with the same desired value as an argument.

Actually resizing label despite it's contents is very suspicious operation)

Upvotes: 0

Related Questions