3

How to create it in qt?

When you click on button - should be shown popup widget and its width should be = button width. And if main window (main form) drag to another place on the screen - popup widget should continuously follow the button (must be attached to the bottom border of the button).

before click image:
before click image

after click image:
after click image

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
igorotvertka
  • 87
  • 1
  • 7

1 Answers1

4

Create widget, don't put it any layout, set it's parent to button's parent (lets call it "host"), set window flags to Qt::Window | Qt::FramelessWindowHint

mPopup = new QWidget(this);
mPopup->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);

Override host's resizeEvent and moveEvent and adjust popup's geometry there using button's geometry.

void Host::adjustPopup() {
    if (!mPopup->isVisible()) {
        return;
    }
    QRect rect = mButton->geometry();
    QPoint bottomLeft = this->mapToGlobal(rect.bottomLeft());
    mPopup->setGeometry(QRect(bottomLeft, QSize(rect.width(),200)));
}

void Host::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    adjustPopup();
}

void Host::moveEvent(QMoveEvent *event)
{
    QWidget::moveEvent(event);
    adjustPopup();
}

full source: button-popup

mugiseyebrows
  • 4,138
  • 1
  • 14
  • 15