Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How could this be improved?



  • Ponder the current code:

    int main(int argc, char *argv[])
    {
      QApplication app(argc, argv);
    
      MainPanel *mainPanel = new MainPanel;
    
      QScrollArea *area = new QScrollArea;
      area->setWidget(mainPanel);
      area->setMaximumSize(mainPanel->size() + QSize(2, 2));
      area->resize(area->maximumSize());
      area->show();
    
      return app.exec();
    }
    
    

    This works (almost) the way I want it to, but it feels like a cludge + that there's a problem too (see point 3). So I'm wondering:

    1. Shouldn't this be possible to solve with policies instead of brute forcing the size? I've tried but I can't get it to work as I want it to.

    2. Why do I have to add QSize(2, 2) to the maximum size to get rid of the scroll bars? Sounds like a bug to me. (This might of course be solved with 1?)

    3. Forcing the window to a size like this doesn't respect actual screen size. MainPanel is (to be exact) 2722x828 pixels which works nice on my 4K screen, but my colleges that's gonna run this has smaller screens and that means that the window goes "out of screen". How can I make the program limit it's window size to max screen width?



  • Ok, solved bullet 3 with another cludge I guess (and added menus as well, thus the extra code compared to above). It works, but isn't there a cleaner way to do this or should I consider this kosher?

    int main(int argc, char *argv[])
    {
      QApplication app(argc, argv);
    
      MainPanel *mainPanel = new MainPanel;
    
      QScrollArea *area = new QScrollArea;
      area->setWidget(mainPanel);
      area->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      area->setMaximumSize(mainPanel->size() + QSize(2, 0));
      area->setMinimumHeight(mainPanel->height());
    
      MainWindow *mainWindow = new MainWindow(mainPanel);
      mainWindow->setCentralWidget(area);
      mainWindow->setMaximumWidth(mainPanel->width() + 2);
      QDesktopWidget desktop;
      mainWindow->resize(desktop.geometry().width(), mainPanel->height());
    
      mainWindow->show();
    
      return app.exec();
    }
    
    

  • Lifetime Qt Champion

    Hi,

    Why not use showMaximized ?



  • @SGaist Mostly because I didn't realize it existed. ;)

    I could use that as a cleaner solution instead of the mainWindow->resize, but then I have to constrain the maximumSize in a good way too. Is there a way to tell that the window should never be bigger than the max size of MainPanel + the size of the meny, status bar etc? If I use maximumSize the window gets expanded to full screen height which looks rather ugly.


  • Lifetime Qt Champion

    What are the ideal dimensions of your application ?


  • Moderators

    @etla said in How could this be improved?:

    1. Shouldn't this be possible to solve with policies instead of brute forcing the size? I've tried but I can't get it to work as I want it to.

    You could place the scroll area(s) inside the main panel, instead of the other way round.

    1. Why do I have to add QSize(2, 2) to the maximum size to get rid of the scroll bars? Sounds like a bug to me. (This might of course be solved with 1?)

    QScrollArea is designed to make a larger widget shrink to fit inside a constrained space. Your use-case (expanding the QScrollArea to make it fit the larger widget) is less common.

    To put it another way: People normally use QScrollArea when they want scrollbars. That's why it's cludgy to make it get rid of scrollbars ;)

    1. Forcing the window to a size like this doesn't respect actual screen size. MainPanel is (to be exact) 2722x828 pixels which works nice on my 4K screen, but my colleges that's gonna run this has smaller screens and that means that the window goes "out of screen". How can I make the program limit it's window size to max screen width?

    Use QScreen or QDesktopWidget to query the screen size and write some simple logic to adjust your app's window size accordingly.


Log in to reply