Skip to content

Instantly share code, notes, and snippets.

@jniemann66
Last active August 27, 2019 01:15
Show Gist options
  • Save jniemann66/7e9f383be7357c41beccb5e756034acd to your computer and use it in GitHub Desktop.
Save jniemann66/7e9f383be7357c41beccb5e756034acd to your computer and use it in GitHub Desktop.
Qt align tops of widgets in separate layouts
// in Widget A, residing in some other layout, but part of the same window as widget B
// someBox is a widget somewhere in widgetA
void WidgetA::resizeEvent(QResizeEvent *event)
{
widgetB->setBoxYPos(someBox->mapTo(this->window(), someBox->rect().topLeft()).y());
QWidget::resizeEvent(event);
}
// in widget B, which has a member QVBoxLayout* mainLayout
// someWidget is a widget somewhere in Widget B's mainLayout
void WidgetB::setBoxYPos(int y)
{
mainLayout->setSpacing(mainLayout->spacing() + y - someWidget->mapTo(this->window(), someWidget->rect().topLeft()).y());
}
// note: use rect().topLeft() instead of pos()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment