[solved] strange sizing behavior between QWidget and QMainWindow



  • I have noticed a strange widget sizing behavior between QWidget and QMainWindow. At first I've been using a QWidget to create my application window. Then I switched to QMainWindow because I wanted to use the menubar and other related stuff. In the application I only had 2 test widgets, a QPushButton and a QSlider, which had set only the minimum widget size (not the maximum). The result was kind of unexpected; the QMainWindow window was creating the widgets with a larger size than the QWidget window. I'll attach here an image.
    !http://img.networkdigitale.com/di/V2F6/preview.png(preview.png)!
    As you can see there's a lot of difference on the slider, while the PushButton seems to keep the minimum size.

    So, I'd like to ask you, is that some default behavior of QMainWindow? As long as it derives from the QWidget class, shouldn't they behave the same?

    Anyway, setting a maximum widget size too keeps the slider at the specified size.

    EDIT
    sorry, I forgot to post the code:
    @#include <QApplication>
    #include <QtWidgets>
    #include <dwmapi.h>
    #pragma comment(lib,"dwmapi.lib")

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QWidget win;
    //QMainWindow win;
    win.setAttribute(Qt::WA_TranslucentBackground);

    MARGINS m = {-1};
    DwmExtendFrameIntoClientArea((HWND)win.winId(), &m);
    
    QSlider slider(&win);
    slider.setFocusPolicy(Qt::StrongFocus);
    slider.setTickPosition(QSlider::TicksBothSides);
    slider.setMinimum(-10);
    slider.setMaximum(10);
    slider.setTickInterval(5);
    slider.setSingleStep(1);
    slider.setMinimumSize(40,150);
    //slider.setMaximumSize(40,150);
    slider.show();
    
    QPushButton but(&win);
    but.setMinimumSize(100,50);
    //but.setMaximumSize(100,50);
    but.show();
    
    win.setMinimumSize(320,240);
    win.show();
    
    return app.exec(&#41;;
    

    }
    @



  • I am not sure if this is a bug, but at least I want to mention that you neither set a central widget, nor did you use a layout. Since the QMainWindow has areas where things like menubars or dockwidgets go, you might be asking for trouble by using it like you do. I would suggest "QMainWindow::setCentralWidget()":http://qt-project.org/doc/qt-5.1/qtwidgets/qmainwindow.html#setCentralWidget, where you take your former QWidget and make it the central widget in the QMainWindow.

    Doesn't help much in finding out where the "problem" is coming from, but maybe it will still help you.



  • Is it absolutely necessary to set the central widget?

    Anyway, I was just curious about this behavior since I thought both widgets behave the same unless you activate the menubar or other toolbars in the QMainWindow widget. Nothing to worry about though.



  • I don't know if you have to set a central widget - but at the very least you can try if that solves the problem of the strange behavior you encountered.

    As I said: The QMainWindow is designed to contain a lot of elements that would be considered standard in an application - for convenience. If you are not using it the way it is supposed to, you may have to be extra careful to not make it hiccup.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.