July 3, 2011

Leon Leon
Robot Herder
400 posts

[Solved]How to move a widget to the center of the dialog?

 

So you have a dialog and a label with a text..
For alignment it is center at both horizontal and vertical..
But i want the label to be at the center of the dialog not only the text of the label to be at the center of it..

  1.  ui->label->rect().center();

or
  1. ui->label->setAlignment(Qt::AlignCenter);

doesn’t work..So?

8 replies

July 3, 2011

Leon Leon
Robot Herder
400 posts

There is one noob solution.. Setting the width of the label the same as the window and the X position to 0..
So the text will be at center and no need for the label to be at center.. But if there is another solution for this i would be glad to know :)

July 3, 2011

romankr romankr
Lab Rat
16 posts

use layouts.

July 3, 2011

HuXiKa HuXiKa
Lab Rat
83 posts

If you just want to display text in a pop-up dialog, use QMessageBox [doc.qt.nokia.com] instead.

 Signature 

If you can find faults of spelling in the text above, you can keep them.

July 3, 2011

Leon Leon
Robot Herder
400 posts

Layouts doesn’t work in this case..
No i want dialog :)

July 3, 2011

Andre Andre
Area 51 Engineer
6031 posts

Layouts do work, but it depends on what else is on the dialog. If you want, you can position the label manually as well. It will overlap other widgets that are on the form though, then.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

July 4, 2011

Leon Leon
Robot Herder
400 posts

I am ok with my first solution!
Layouts doesn’t do what i want.. And i don’t want to position the label manually cause i need accuracy!

July 4, 2011

Andre Andre
Area 51 Engineer
6031 posts

Positioning manually, in this sense, means through code. You can get pixel precision that way. The best place to put such code is in the QWidget::resizeEvent handler. That way, your label will stay in the center of the dialog even if there is a layout change (like a user resizing the dialog).

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

July 4, 2011

Tobias Hunger Tobias Hunger
Mad Scientist
3156 posts

There is one noob solution.. Setting the width of the label the same as the window and the X position to 0.. So the text will be at center and no need for the label to be at center.. But if there is another solution for this i would be glad to know :)

Which is what a layout does for you…

 
  ‹‹ trouble with slots and signals      [QtNetwork] How to identify dead connection ››

You must log in to post a reply. Not a member yet? Register here!