Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] strange sizing behavior between QWidget and QMainWindow
Forum Updated to NodeBB v4.3 + New Features

[solved] strange sizing behavior between QWidget and QMainWindow

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by
    #1

    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;;
    

    }
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thEClaw
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thEClaw
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved