Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Moving widget to the window center

[SOLVED] Moving widget to the window center

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 12.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Giperboloid
    wrote on last edited by
    #1

    Hi.
    I'm trying to move QLabel with setted QMovie in it in the center of my main window.
    But after QLabel moving QMovie is not displayed on the screen. Where I have mistaken and what are other ways of solving this problem?
    @ui->label->move( ui->label->parentWidget()->geometry().center());@

    1 Reply Last reply
    0
    • guidupasG Offline
      guidupasG Offline
      guidupas
      wrote on last edited by
      #2

      Have not tested but you can try this:
      @
      ui->label->move(MainWindow->geometry().center());
      @

      Att.
      Guilherme Cortada Dupas

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Giperboloid
        wrote on last edited by
        #3

        I tried your variant but label's position is not in the center after execution of that code, it's position in my case is in lower right corner.

        1 Reply Last reply
        0
        • guidupasG Offline
          guidupasG Offline
          guidupas
          wrote on last edited by
          #4

          Well, I need to understand what you really want and it is difficult with just one line of code. I need to see a little more.

          Cheers.

          Att.
          Guilherme Cortada Dupas

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Giperboloid
            wrote on last edited by
            #5

            Ok, here is all programm's code:
            @MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);
            this->setFixedSize(200, 200);
            ui->label->move(this->geometry().center());
            }

            MainWindow::~MainWindow()
            {
            delete ui;
            }@
            And the result: https://lh6.googleusercontent.com/oHS2HQBFsn4C6fFDhCKRJVIC7D1n0zIyIq_p2mS0PL4=s219-p-no

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The code you both presented doesn't move the label to the center. It moves the label so that the upper left corner of it is in the center, which might mean that the label itself lands in the corner if it's big enough. To properly center it you need to take under consideration the fact that label has width and height i.e. you need to subtract half of it from the center to position it correctly.

              Another thing is that you take whatever->geometry().center() which is actually the center point of the whatever in the parent coordinates, which is garbage for the label. The center of label parent would be label->parentWidget()->rect().center(). In your case it would be just rect()->center() since you're in the main window constructor.

              Mix those two things and you'll be ok.
              Or... just use a layout and don't think about coordinates anymore.

              1 Reply Last reply
              0
              • guidupasG Offline
                guidupasG Offline
                guidupas
                wrote on last edited by
                #7

                Yes, thats right. The easiest way is a layout. Or you need to make some calculations to center it.

                Att.
                Guilherme Cortada Dupas

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Giperboloid
                  wrote on last edited by
                  #8

                  I just want to have something like little loading circle. For that purpose I selected label and setted QMovie in it. Now I want to move it to the center of form. Maybe it is better decision to invoke another single window with label on it?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    ankursaxena
                    wrote on last edited by
                    #9

                    But question remain alive ?? how can you bring another single window in the center?? If u can do that.. than you can bring label in the center.

                    Well, I am totally agree with chris kawa. He is absolutely right.
                    or
                    why don't u try some kind of calculation to bring label in perfect center ??

                    1 Reply Last reply
                    0
                    • guidupasG Offline
                      guidupasG Offline
                      guidupas
                      wrote on last edited by
                      #10

                      You can try this

                      @
                      ui->label->move(window.width() / 2 - ui->label.width() / 2, window.height() / 2 - ui->label.height() / 2);
                      @

                      Cheers

                      Att.
                      Guilherme Cortada Dupas

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        Giperboloid
                        wrote on last edited by
                        #11

                        Thanks for all comments: I did like you said and just calculated the center of the form.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved