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. How to get a global position of a Widget in QToolbar?
Forum Updated to NodeBB v4.3 + New Features

How to get a global position of a Widget in QToolbar?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 798 Views 2 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.
  • X Offline
    X Offline
    Xodiqus
    wrote on last edited by
    #1

    Hello!
    I have the code:

    auto toolbar = new QToolBar(&mainWidget);
    toolbar->move(50,50); // random pos, but not (0,0);
    toolbar->addWidget(new QPushButton("1-button"));
    auto secondButton = new QPushButton("2-button");
    toolbar->addWidget(secondButton);
    qDebug() <<  secondButton->pos(); // (0,0) ok because there's local coords.
    qDebug() <<  secondButton->mapToGlobal(QPoint{0,0}); // expected secondButton's global pos, but found toolbar pos. Why?
    

    Also I had tried some variants of usage of methods 'geomtry' and 'rect' that followed to the same result. Using Qt 6.6.
    How to get a global position of a Widget in QToolbar? Thanks.

    jsulmJ Pl45m4P 2 Replies Last reply
    0
    • X Xodiqus

      Hello!
      I have the code:

      auto toolbar = new QToolBar(&mainWidget);
      toolbar->move(50,50); // random pos, but not (0,0);
      toolbar->addWidget(new QPushButton("1-button"));
      auto secondButton = new QPushButton("2-button");
      toolbar->addWidget(secondButton);
      qDebug() <<  secondButton->pos(); // (0,0) ok because there's local coords.
      qDebug() <<  secondButton->mapToGlobal(QPoint{0,0}); // expected secondButton's global pos, but found toolbar pos. Why?
      

      Also I had tried some variants of usage of methods 'geomtry' and 'rect' that followed to the same result. Using Qt 6.6.
      How to get a global position of a Widget in QToolbar? Thanks.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Xodiqus said in How to get a global position of a Widget in QToolbar?:

      but found toolbar pos

      What does this mean? What pose do you get exactly?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      X 1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        Put your toolbar in a layout,
        or use QMainWindow and addToolBar method.

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @Xodiqus said in How to get a global position of a Widget in QToolbar?:

          but found toolbar pos

          What does this mean? What pose do you get exactly?

          X Offline
          X Offline
          Xodiqus
          wrote on last edited by
          #4

          @jsulm, English is not my native. We both know that short one is meaning 'position'.

          Christian EhrlicherC jsulmJ M 3 Replies Last reply
          0
          • X Xodiqus

            @jsulm, English is not my native. We both know that short one is meaning 'position'.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Xodiqus said in How to get a global position of a Widget in QToolbar?:

            @jsulm, English is not my native. We both know that short one is meaning 'position'.

            Even then I really don't understand what "but found toolbar position" should mean in your context.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • X Xodiqus

              @jsulm, English is not my native. We both know that short one is meaning 'position'.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Xodiqus It is clear to me what pos means. I asked what "found toolbar pos" means and I also asked what exact coordinates you get. If you want to get help then please provide needed information in an understandable way.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • X Xodiqus

                @jsulm, English is not my native. We both know that short one is meaning 'position'.

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                @Xodiqus
                Not what I see.

                QMainWindow mw;
                mw.resize(300,100);
                mw.move(400,50);
                
                auto toolbar = new QToolBar;
                mw.addToolBar(Qt::TopToolBarArea,toolbar);
                toolbar->move(50,50); // random pos, but not (0,0);
                toolbar->addWidget(new QPushButton("1-button"));
                auto secondButton = new QPushButton("2-button");
                toolbar->addWidget(secondButton);
                
                mw.show();
                qDebug() <<  secondButton->pos(); // (0,0) ok because there's local coords.
                qDebug() <<  secondButton->mapToGlobal(QPoint{0,0});
                
                

                QPoint(110,1)
                QPoint(510,73) // pos relative to the screen pos ( 400+110)

                1 Reply Last reply
                1
                • X Xodiqus

                  Hello!
                  I have the code:

                  auto toolbar = new QToolBar(&mainWidget);
                  toolbar->move(50,50); // random pos, but not (0,0);
                  toolbar->addWidget(new QPushButton("1-button"));
                  auto secondButton = new QPushButton("2-button");
                  toolbar->addWidget(secondButton);
                  qDebug() <<  secondButton->pos(); // (0,0) ok because there's local coords.
                  qDebug() <<  secondButton->mapToGlobal(QPoint{0,0}); // expected secondButton's global pos, but found toolbar pos. Why?
                  

                  Also I had tried some variants of usage of methods 'geomtry' and 'rect' that followed to the same result. Using Qt 6.6.
                  How to get a global position of a Widget in QToolbar? Thanks.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #8

                  @Xodiqus said in How to get a global position of a Widget in QToolbar?:

                  Hello!
                  I have the code:

                  auto toolbar = new QToolBar(&mainWidget);
                  toolbar->move(50,50); // random pos, but not (0,0);
                  toolbar->addWidget(new QPushButton("1-button"));
                  auto secondButton = new QPushButton("2-button");
                  toolbar->addWidget(secondButton);
                  qDebug() <<  secondButton->pos(); // (0,0) ok because there's local coords.
                  qDebug() <<  secondButton->mapToGlobal(QPoint{0,0}); // expected secondButton's global pos, but found toolbar pos. Why?
                  

                  I think it's important where you run this code.
                  I assume this code above is in MainWindow constructor?!

                  while @mpergand 's code runs after the widgets are shown and positioned on the screen.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  X 1 Reply Last reply
                  2
                  • Pl45m4P Pl45m4

                    @Xodiqus said in How to get a global position of a Widget in QToolbar?:

                    Hello!
                    I have the code:

                    auto toolbar = new QToolBar(&mainWidget);
                    toolbar->move(50,50); // random pos, but not (0,0);
                    toolbar->addWidget(new QPushButton("1-button"));
                    auto secondButton = new QPushButton("2-button");
                    toolbar->addWidget(secondButton);
                    qDebug() <<  secondButton->pos(); // (0,0) ok because there's local coords.
                    qDebug() <<  secondButton->mapToGlobal(QPoint{0,0}); // expected secondButton's global pos, but found toolbar pos. Why?
                    

                    I think it's important where you run this code.
                    I assume this code above is in MainWindow constructor?!

                    while @mpergand 's code runs after the widgets are shown and positioned on the screen.

                    X Offline
                    X Offline
                    Xodiqus
                    wrote on last edited by
                    #9

                    @Pl45m4 , thanks for that you have answered!

                    No, there is the code in the main:

                    int main() {
                        QApplication a(argc, argv);
                        Widget w;
                        // the code typed upper. 
                    }
                    

                    I don't use MainWindow.

                    jsulmJ 1 Reply Last reply
                    0
                    • X Xodiqus

                      @Pl45m4 , thanks for that you have answered!

                      No, there is the code in the main:

                      int main() {
                          QApplication a(argc, argv);
                          Widget w;
                          // the code typed upper. 
                      }
                      

                      I don't use MainWindow.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Xodiqus said in How to get a global position of a Widget in QToolbar?:

                      I don't use MainWindow

                      Doesn't matter, Widget is a QWidget and you have that code in its constructor, right?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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