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 Get position of the window
QtWS25 Last Chance

Can't Get position of the window

Scheduled Pinned Locked Moved General and Desktop
positionqmainwindow
5 Posts 5 Posters 5.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.
  • E Offline
    E Offline
    erfan
    wrote on 14 Aug 2015, 16:28 last edited by
    #1

    Hello
    I am new to Qt
    I have written a simple application and I want to get current position of the QMainWindow.
    this is my code:

    M::M(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::M)
    {
    ui->setupUi(this);
    QMessageBox msg;
    msg.setText(QString::number("what returns the X or Y of the window?"));
    msg.exec();
    }

    I searched a lot. There where topics about setting the position, not getting it.
    I also tried geometry().x(), but it always returns 0 !!!
    Please help me. I need this.
    Best regards...

    C K 2 Replies Last reply 14 Aug 2015, 17:03
    0
    • E erfan
      14 Aug 2015, 16:28

      Hello
      I am new to Qt
      I have written a simple application and I want to get current position of the QMainWindow.
      this is my code:

      M::M(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::M)
      {
      ui->setupUi(this);
      QMessageBox msg;
      msg.setText(QString::number("what returns the X or Y of the window?"));
      msg.exec();
      }

      I searched a lot. There where topics about setting the position, not getting it.
      I also tried geometry().x(), but it always returns 0 !!!
      Please help me. I need this.
      Best regards...

      C Offline
      C Offline
      ChrisW
      wrote on 14 Aug 2015, 17:03 last edited by
      #2

      @erfan

      Hi,

      I didn't test this on my end, so it might not work. This is what I recommend:

      Call geometry.x() after you called the show() function for your window widget. The reason why I recommend this is because recently, I was
      not getting the correct button size until I requested it after I showed the window. When I called it before hand, it gave me answers that I knew were incorrect. Also, looking at your code, I don't see where you called a window's widget so that it can be displayed and with the embedded QMessageBox on that widget.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KiwiJeff
        wrote on 14 Aug 2015, 17:13 last edited by
        #3

        Not tested, but the doc seems to say that it provides what you want.

        http://doc.qt.io/qt-5/qwidget.html#pos-prop

        1 Reply Last reply
        0
        • E erfan
          14 Aug 2015, 16:28

          Hello
          I am new to Qt
          I have written a simple application and I want to get current position of the QMainWindow.
          this is my code:

          M::M(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::M)
          {
          ui->setupUi(this);
          QMessageBox msg;
          msg.setText(QString::number("what returns the X or Y of the window?"));
          msg.exec();
          }

          I searched a lot. There where topics about setting the position, not getting it.
          I also tried geometry().x(), but it always returns 0 !!!
          Please help me. I need this.
          Best regards...

          K Offline
          K Offline
          kumararajas
          wrote on 14 Aug 2015, 17:36 last edited by
          #4

          @erfan Just to be more clear on what you want..

          When you say current position, current position in where?

          Meaning, In your desktop do you want to know where your UI is? Or, Inside the window, where the widgets are present?

          --Kumara

          --Kumar

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 14 Aug 2015, 17:50 last edited by Chris Kawa
            #5

            You can't get a position of your window inside that window's constructor because it doesn't have one yet.
            The window manager of your OS gives your window a position when it is first shown (or if you explicitly called move() before).
            The window is shown when you call on it show()or any of the related functions like showMaximized().

            After the window is shown you can get the window position including window frame with pos() and without the window frame with geometry().topLeft().

            1 Reply Last reply
            0

            1/5

            14 Aug 2015, 16:28

            • Login

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