Papipone
Papipone

Reputation: 1123

Qt5 overlay widget not resizing properly

I am trying to design a widget which overlaps another one. I successfully done it, but I am facing a little problem during the resize event of my QMainWindow.

I created an OverlayView widget, derived from QWidget. The constructor used is defined as follows:

OverlayView::OverlayView(QWidget* widget, const QColor& color, float opacity, OverLayPosition position) :
    QWidget(nullptr),
    referenceWidget_(widget),
    overlayPosition_(position),
    opacity_(opacity),
    overlayColor_(color)
{
    overlayRect_ = referenceWidget_->rect();
    overlayRect_.setWidth(overlayRect_.width() * 0.05f);
    setAutoFillBackground(false);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

The referenceWidget_ class member is a pointer to a widget which is used to get its size. In my project, I used my QMainWindow.

I have just made a little experiment to see if the OverlayView widget was resizing correctly:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QPlainTextEdit* pte = new QPlainTextEdit(this);
    pte->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setCentralWidget(pte);

    overlayView_ = new OverlayView(this, QColor(0x33, 0x99, 0x38, 120));
    overlayView_->setParent(this);

    connect(this, SIGNAL(updateOverlaySize(const QRect&)), overlayView_, SLOT(updateOverlaySize(const QRect&)));
}

The OverlayView widget is connected to the updateOverlaySize slot through the updateOverlaySize signal of my QMainWindow used as follows:

void MainWindow::resizeEvent(QResizeEvent* event)
{
    QMainWindow::resizeEvent(event);
    emit updateOverlaySize(rect());
}

The updateOverlaySize slot of the OverlayView is defined like this:

void OverlayView::updateOverlaySize(const QRect& rect)
{
    overlayRect_ = rect;
}

Just for testing purposes, the overlay occupy the QMainWindow.

Finally, the overlay is drawn by redefining the paintEvent of the OverlayView:

void OverlayView::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.fillRect(overlayRect_, overlayColor_);
}

Here is the result:

Expected result

Everything seems to be fine...

Not an expected result

...mmmh not really. The overlay stops to be resized when the size of the QMainWindow exceed a certain value.

The QRect object received from the updateOverlaySize slot shows correct values.

If someone could help me to fix this. Thanks for your answers.

Upvotes: 2

Views: 1182

Answers (1)

Papipone
Papipone

Reputation: 1123

I will answer my own question.

I just ommited to update the geometry of my widget. According to Qt doc, the geometry property do this:

This property holds the geometry of the widget relative to its parent and excluding the window frame.

When changing the geometry, the widget, if visible, receives a move event (moveEvent()) and/or a resize event (resizeEvent()) immediately. If the widget is not currently visible, it is guaranteed to receive appropriate events before it is shown.

The size component is adjusted if it lies outside the range defined by minimumSize() and maximumSize().

The overlayRect_ class member was updated successfully but not the size of my widget. This seems to be a problem.

Updating the geometry in the updateOverlaySize slot fixed this:

void OverlayView::updateOverlaySize(const QRect& rect)
{
    overlayRect_ = rect;
    setgeometry(rect);
}

Now, the size() method of the OverlayView widget is returning its real size.

Upvotes: 2

Related Questions