nabroyan
nabroyan

Reputation: 3275

Prevent window resize during QStatusBar resize

If application's main window has QStatusBar and if it's width becomes bigger (for example during adding a widget) than the main window's width, the window automatically resizes to fit status bar. I wonder is there way to prevent window's size changes in such cases?

Thanks in advance!

Upvotes: 3

Views: 883

Answers (3)

architectonic
architectonic

Reputation: 3129

I had the same problem and was able to fix it by simply setting a fixed size for the widget that I added to the status bar

Upvotes: 0

nabroyan
nabroyan

Reputation: 3275

I've found a way out of this situation. I added a QScrollArea to my status bar and now I add my widgets to that scroll area, so they do not affect on main window's size.

Upvotes: 3

PeterSW
PeterSW

Reputation: 5261

To do that I would write a little RAII resize blocker like this:

class ResizeBlocker
{ 
public:
    ResizeBlocker(QWidget *widget)
        : m_widget(widget)
    {
        m_widget->setFixedSize(m_widget->size());
    }

    ~ResizeBlocker()
    {
        m_widget->setFixedSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
    }

private:
    QWidget *m_widget;
};

Which can be used something like this:

void MainWindow::addToStatusBar()
{
    ResizeBlocker doNotResizeHere(this);
    ui->statusBar->addWidget(new QLabel("A Label"));
}

I've created a demo of this and uploaded it to GitHub here.

Upvotes: 0

Related Questions