在考虑水平滚动时获取 table header 部分的 absolute/global 位置
Get absolute/global position of table header section while accounting for horizontal scroll
我正在创建一个从 QHBoxLayout 继承的列跟踪布局,它会将几行编辑对齐到 table 列:
为此,我使用以下代码计算 ColumnAlignedLayout::setGeometry()
中每个部分大小的绝对屏幕位置:
void ColumnAlignedLayout::setGeometry(const QRect &r)
{
QHBoxLayout::setGeometry(r);
int widgetX = parentWidget()->mapToGlobal(QPoint(0, 0)).x();
int headerX = headerView->mapToGlobal(QPoint(0, 0)).x();
int delta = headerX - widgetX;
for (int ii = 0; ii < headerView->count(); ++ii) {
int pos = headerView->sectionPosition(ii);
int size = headerView->sectionSize(ii);
auto item = itemAt(ii);
auto r = item->geometry();
r.setLeft(pos + delta);
r.setWidth(size);
item->setGeometry(r);
}
}
除了 table 有水平滚动时,该布局已经有效。
完整项目位于 https://github.com/sashoalm/ColumnAlignedLayout,运行 它很容易重现问题 - 只需放大列直到出现水平滚动条,然后滚动直到隐藏第一列。
headerView->mapToGlobal(QPoint(0, 0)).x();
无论是否调整 table 的大小,都给出相同的值。
我发现了一个类似的问题,但它是针对 objective C 的,它没有考虑滚动,这是我的问题。
how to get the absolute position of a section header in tableview?
最终我进入了 Qt 的绘画事件源,假设他们需要它的实际位置,并且他们使用 sectionViewportPosition()
,它给出了相对于 QTableWidget/QTableView 的实际部分位置s 左上角.
您可以将其与 mapToGlobal()
一起使用以获得该部分的全局水平位置。
我正在创建一个从 QHBoxLayout 继承的列跟踪布局,它会将几行编辑对齐到 table 列:
为此,我使用以下代码计算 ColumnAlignedLayout::setGeometry()
中每个部分大小的绝对屏幕位置:
void ColumnAlignedLayout::setGeometry(const QRect &r)
{
QHBoxLayout::setGeometry(r);
int widgetX = parentWidget()->mapToGlobal(QPoint(0, 0)).x();
int headerX = headerView->mapToGlobal(QPoint(0, 0)).x();
int delta = headerX - widgetX;
for (int ii = 0; ii < headerView->count(); ++ii) {
int pos = headerView->sectionPosition(ii);
int size = headerView->sectionSize(ii);
auto item = itemAt(ii);
auto r = item->geometry();
r.setLeft(pos + delta);
r.setWidth(size);
item->setGeometry(r);
}
}
除了 table 有水平滚动时,该布局已经有效。
完整项目位于 https://github.com/sashoalm/ColumnAlignedLayout,运行 它很容易重现问题 - 只需放大列直到出现水平滚动条,然后滚动直到隐藏第一列。
headerView->mapToGlobal(QPoint(0, 0)).x();
无论是否调整 table 的大小,都给出相同的值。
我发现了一个类似的问题,但它是针对 objective C 的,它没有考虑滚动,这是我的问题。
how to get the absolute position of a section header in tableview?
最终我进入了 Qt 的绘画事件源,假设他们需要它的实际位置,并且他们使用 sectionViewportPosition()
,它给出了相对于 QTableWidget/QTableView 的实际部分位置s 左上角.
您可以将其与 mapToGlobal()
一起使用以获得该部分的全局水平位置。