igorotvertka
igorotvertka

Reputation: 97

Popup widget in qt

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

Upvotes: 4

Views: 8363

Answers (1)

mugiseyebrows
mugiseyebrows

Reputation: 4818

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

Upvotes: 4

Related Questions