[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..

    @ ui->label->rect().center();@
    or
    @ui->label->setAlignment(Qt::AlignCenter);@
    doesn't work..So?



  • 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 :)



  • use layouts.



  • If you just want to display text in a pop-up dialog, use "QMessageBox ":http://doc.qt.nokia.com/latest/qmessagebox.html instead.



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



  • 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.



  • 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!



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


  • Moderators

    bq. 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...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.