3

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!

nabroyan
  • 3,225
  • 6
  • 37
  • 56

3 Answers3

3

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.

nabroyan
  • 3,225
  • 6
  • 37
  • 56
0

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.

PeterSW
  • 4,921
  • 1
  • 24
  • 35
  • @nabroyan In what way doesn't it work? A compiler error, or the window still resizes? Could you show what you tried? – PeterSW Mar 08 '14 at 20:11
  • It compiles and runs, but window still resizes. I've just copy-pasted your code and passed the pointer of my main window. – nabroyan Mar 08 '14 at 20:17
  • Are you using it within the scope that contains the call that triggers the resize? In my example it only blocks the resize with in the addToStatusBar() function, so that the user can still resize the window. – PeterSW Mar 08 '14 at 20:22
  • Yes, I create a `ResizeBlocker` class object right before `addWidget()` function call – nabroyan Mar 08 '14 at 21:42
  • Well, it turned out, that it resizes, but when widgets are being removed and when user tries to resize window it returns to it's original size. – nabroyan Mar 09 '14 at 07:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49353/discussion-between-petersw-and-nabroyan) – PeterSW Mar 09 '14 at 13:04
0

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

architectonic
  • 2,871
  • 2
  • 21
  • 35