Edit: forgot to add - I am using Qt 5.15 under Windows 10 MSYS2 MinGW
I am trying to toggle a QStatusBar in my QMainWindow and resize the main window content accordingly. The content has a QLabel with a QPixmap inside a QScrollArea that may or may not be larger than the window, so I calculate the window size manually after each change to the pixmap. In order for the status bar to not overlap the pixmap when it should not be larger than the window, I need to get the status bar size on it's creation so that my resize function can access it. However I cannot figure out how to get the correct size. My status bar toggle looks like this
void toggle_status_bar() { // member of QMainWindow subclass
if (m_status_bar)
m_status_bar = nullptr;
else {
m_status_bar->showMessage("some status message");
m_status_bar->adjustSize(); // * this gives the wrong size ...
}
setStatusBar(m_status_bar);
}
void toggle_status_bar() { // member of QMainWindow subclass
if (m_status_bar)
m_status_bar = nullptr;
else {
m_status_bar = new QStatusBar { this };
m_status_bar->showMessage("some status message");
m_status_bar->adjustSize(); // * this gives the wrong size ...
}
setStatusBar(m_status_bar);
}
To copy to clipboard, switch view to plain text mode
But here the marked line (// *) calculates the wrong size: it resizes the status bar to height 18. Then my resize function, which is essentially
void resize_content() { // member of QMainWindow subclass
// calculate new_size ...
resize(new_size);
}
void resize_content() { // member of QMainWindow subclass
// calculate new_size ...
resize(new_size);
}
To copy to clipboard, switch view to plain text mode
, makes scrollbars appear for the pixmap. If I now request another resize while the program is running, i.e. calling resize_content again, the status bar is resized to height 20 and the scrollbar disappears like it should.
I tried different things at the end of toggle_status_bar(), all of which calculate the wrong height 18:
- Call resize_content()
- Call resize_content() multiple times
- Call update()
- Call m_status_bar->update()
- All of the above
- All of the above, multiple times
The only thing that produces the corrent result is calling
m_status_bar->resize(m_status_bar->width(), 20);
m_status_bar->resize(m_status_bar->width(), 20);
To copy to clipboard, switch view to plain text mode
after creating the status bar, but this is obviously bad ..
Otherwise, the correct height 20 is only calculated when I request a resize as user during runtime, which I do like this:
void keyPressEvent
(QKeyEvent* e
) { // member of QLabel subclass that contains the pixmap switch(e-key()) {
//cases ..
resize_pixmap();
}
}
void resize_pixmap() { // member of QLabel subclass that contains the pixmap
// calculate new_size / resize pixmap ...
emit pixmap_resized(new_size); // this signal is connected to a slot in the QMainWindow sublass that calls resize_content
}
void keyPressEvent(QKeyEvent* e) { // member of QLabel subclass that contains the pixmap
switch(e-key()) {
//cases ..
resize_pixmap();
}
}
void resize_pixmap() { // member of QLabel subclass that contains the pixmap
// calculate new_size / resize pixmap ...
emit pixmap_resized(new_size); // this signal is connected to a slot in the QMainWindow sublass that calls resize_content
}
To copy to clipboard, switch view to plain text mode
I don't know what is the difference between requesting the resize during runtime as opposed to calling resize_content in toggle_status_bar programatically, but I assume there is some interal Qt slot called after resize? Which one, and how to do it programatically? Also I am confused why adjustSize() calculates the wrong status bar size in the first place .. can anyone help?
Bookmarks