You need to get a QRect specific tab in QTabBar , that is, its coordinates and width with height. Method tabRect(); , unfortunately, returns only a rectangle with width and height, but with coordinates 0, 0. How can I get the global coordinates of a tab at a certain index? Just as tabRect() works, only so that there are also coordinates. PS tab means only the panel in the tab bar (not QTabWidget ).

  • Try using mapToGlobal / mapToParent methods of QWidget - ixSci 4:52 pm
  • @ixSci Well, mapToGlobal takes a QPoint parameter, I ask where to get these coordinates. - Nik.
  • one
    Those. pass there (0,0) you did not guess? - ixSci
  • one
    @ixSci, newRect = mapToParent(rect.topLeft()) or even rect.translate(mapToParent(rect.topLeft())) . - ߊߚߤߘ

1 answer 1

In Qt, there is no hard-wired return of the zero coordinates of the top left corner of the tab. This can be seen by looking at the source code of this framework.

/src/gui/widgets/qtabbar.cpp (fragments responsible for the formation of tab coordinates are given):

 QRect QTabBar::tabRect(int index) const { Q_D(const QTabBar); if (const QTabBarPrivate::Tab *tab = d->at(index)) { if (d->layoutDirty) const_cast<QTabBarPrivate*>(d)->layoutTabs(); QRect r = tab->rect; if (verticalTabs(d->shape)) r.translate(0, -d->scrollOffset); else r.translate(-d->scrollOffset, 0); if (!verticalTabs(d->shape)) r = QStyle::visualRect(layoutDirection(), rect(), r); return r; } return QRect(); } // .... QTabBarPrivate::Tab *QTabBarPrivate::at(int index) { return validIndex(index)?&tabList[index]:0; } // ... void QTabBarPrivate::layoutTabs() { // ... for (i = 0; i < tabList.count(); ++i) { const QLayoutStruct &lstruct = tabChain.at(i + 1); if (!vertTabs) tabList[i].rect.setRect(lstruct.pos, 0, lstruct.size, maxExtent); else tabList[i].rect.setRect(0, lstruct.pos, maxExtent, lstruct.size); } // ... } 

That is, the horizontal tabs zero is only the Y coordinate. The second coordinate increases linearly and depends on the tab number, the width of the previous tabs and the shift of the entire panel (you can set the arrows in the case when the row of tabs does not fit on the screen).

Another thing is that the coordinates are stored relative to QTabBar , and therefore it is necessary to translate the coordinates into the space of the desired widget:

 QRect tabRect = myTabBar->tabRect(tabId); tabRect.translate(mapTo(targetWidget, tabRect.topLeft)); 
  • All is clear, thank you. It works now. And what is this declaration of an exotic QTabBarPrivate::Tab *QTabBarPrivate::at , what does it mean? - Nik
  • one
    The at method of QTabBarPrivate class, which returns a pointer to QTabBarPrivate::Tab . In C ++, it doesn’t matter what the asterisk pointer is “stuck” to, only its relative order among the “words” of the source code is important. - ߊߚߤߘ