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. Can't center QDialog.

Can't center QDialog.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.7k Views
  • 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.
  • Q Offline
    Q Offline
    Qingshui Kong
    wrote on last edited by
    #1

    Could somebody help me?

    I defined a customDialog that inherited from QDialog. I want it to be frameless and topmost. But its location is (0, 0) by default. I want it to be centered.

    Here is the main code.

    customDialog::customDialog(QWidget *parent)
    QDialog(parent)
    {
    ui->setupui(this);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::Dialog);
    }

    mainWindow::showDialog()
    {
    customDialog dialog(this);
    dialog.exec();
    }

    My environment:
    Ubuntu 16.04
    0_1542955488816_03128193-a1ef-4454-bb90-03634206969a-image.png
    0_1542955452252_18d8a8f2-8531-471b-89a0-f4d8266d043b-image.png

    Thanks

    RatzzR Q 2 Replies Last reply
    0
    • Q Qingshui Kong

      Could somebody help me?

      I defined a customDialog that inherited from QDialog. I want it to be frameless and topmost. But its location is (0, 0) by default. I want it to be centered.

      Here is the main code.

      customDialog::customDialog(QWidget *parent)
      QDialog(parent)
      {
      ui->setupui(this);
      this->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::Dialog);
      }

      mainWindow::showDialog()
      {
      customDialog dialog(this);
      dialog.exec();
      }

      My environment:
      Ubuntu 16.04
      0_1542955488816_03128193-a1ef-4454-bb90-03634206969a-image.png
      0_1542955452252_18d8a8f2-8531-471b-89a0-f4d8266d043b-image.png

      Thanks

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        You need to work with setGeometry(..) function. I don't see anything specific to center it.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        Q 1 Reply Last reply
        1
        • dheerendraD dheerendra

          You need to work with setGeometry(..) function. I don't see anything specific to center it.

          Q Offline
          Q Offline
          Qingshui Kong
          wrote on last edited by
          #4

          @dheerendra

          Thank you!

          If I don't setWindowFlags, the dialog show in the center by default. I don't know the reason, but I think there should something I can do to fix it. But I don't know.

          What's more, I test it on Windows system, and it works.

          J.HilkJ 1 Reply Last reply
          0
          • Q Qingshui Kong

            @dheerendra

            Thank you!

            If I don't setWindowFlags, the dialog show in the center by default. I don't know the reason, but I think there should something I can do to fix it. But I don't know.

            What's more, I test it on Windows system, and it works.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Qingshui-Kong said in Can't center QDialog.:

            What's more, I test it on Windows system, and it works.

            Qt::X11BypassWindowManagerHint I doubt this one works on Wndows. And usually the window manager positions the dialog centered.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            Q 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @Qingshui-Kong said in Can't center QDialog.:

              What's more, I test it on Windows system, and it works.

              Qt::X11BypassWindowManagerHint I doubt this one works on Wndows. And usually the window manager positions the dialog centered.

              Q Offline
              Q Offline
              Qingshui Kong
              wrote on last edited by
              #6

              @J.Hilk
              Thanks you!
              I know it works on Linux.
              But here is the snapshot of the document.
              0_1542961370313_0a4231ad-5984-4735-a8ca-a3ccc5950a2c-image.png

              So I think they both are the same.

              1 Reply Last reply
              0
              • Q Qingshui Kong

                Could somebody help me?

                I defined a customDialog that inherited from QDialog. I want it to be frameless and topmost. But its location is (0, 0) by default. I want it to be centered.

                Here is the main code.

                customDialog::customDialog(QWidget *parent)
                QDialog(parent)
                {
                ui->setupui(this);
                this->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::Dialog);
                }

                mainWindow::showDialog()
                {
                customDialog dialog(this);
                dialog.exec();
                }

                My environment:
                Ubuntu 16.04
                0_1542955488816_03128193-a1ef-4454-bb90-03634206969a-image.png
                0_1542955452252_18d8a8f2-8531-471b-89a0-f4d8266d043b-image.png

                Thanks

                Q Offline
                Q Offline
                Qingshui Kong
                wrote on last edited by
                #7

                @Qingshui-Kong
                If you override showEvent, you must call the same function of the base class. Or it will skip the same function of the base class. Then there will be some bugs.

                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