Results 1 to 13 of 13

Thread: QStatusBar wrong adjustSize()? When is it calculated?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2020
    Posts
    19
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default QStatusBar wrong adjustSize()? When is it calculated?

    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

    Qt Code:
    1. void toggle_status_bar() { // member of QMainWindow subclass
    2. if (m_status_bar)
    3. m_status_bar = nullptr;
    4. else {
    5. m_status_bar = new QStatusBar { this };
    6. m_status_bar->showMessage("some status message");
    7. m_status_bar->adjustSize(); // * this gives the wrong size ...
    8. }
    9. setStatusBar(m_status_bar);
    10. }
    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
    Qt Code:
    1. void resize_content() { // member of QMainWindow subclass
    2. // calculate new_size ...
    3. resize(new_size);
    4. }
    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
    Qt Code:
    1. 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:
    Qt Code:
    1. void keyPressEvent(QKeyEvent* e) { // member of QLabel subclass that contains the pixmap
    2. switch(e-key()) {
    3. //cases ..
    4. resize_pixmap();
    5. }
    6. }
    7. void resize_pixmap() { // member of QLabel subclass that contains the pixmap
    8. // calculate new_size / resize pixmap ...
    9. emit pixmap_resized(new_size); // this signal is connected to a slot in the QMainWindow sublass that calls resize_content
    10. }
    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?
    Last edited by Maryu; 4th September 2020 at 21:25. Reason: add platform/version info

Similar Threads

  1. Replies: 1
    Last Post: 24th October 2009, 05:52
  2. QStatusBar
    By newermind in forum Qt Programming
    Replies: 3
    Last Post: 1st June 2009, 21:48
  3. Question about QWidget::adjustSize()
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2007, 13:33
  4. about QStatusBar
    By Pang in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2006, 04:15
  5. adjustSize for right adjustement
    By square in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2006, 09:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.