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?
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. -
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.@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?
-
@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?
-
@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.
-
@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) -
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.@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 inMainWindow
constructor?!while @mpergand 's code runs after the widgets are shown and positioned on the screen.
-
@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 inMainWindow
constructor?!while @mpergand 's code runs after the widgets are shown and positioned on the screen.
-
@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.
@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?