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. Automagically resize window
Forum Updated to NodeBB v4.3 + New Features

Automagically resize window

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 9.2k 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.
  • J Offline
    J Offline
    jdarnold
    wrote on last edited by
    #1

    I have a simple window, that has a QLabel in it. I want to draw various images into the pixmap. How do I get it to resize the window when I create a new QPixmap for the QLabel? Is there some kind of Layout I need to use, or some way I tell the QDialog to recheck its layout? If I set a vertical layout on the dialog, and set it to a fixed constraint, it resizes correctly, but then the user can't resize the window.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VC15
      wrote on last edited by
      #2

      When a widget is added to a layout it automatically resizes when it's contents changes, doesn't it?

      For example,

      @#include <QApplication>
      #include <QtGui>

      int main(int argc, char* argv[])
      {
      QApplication app(argc, argv);

      QWidget w;
      QVBoxLayout mainLayout(&w);

      QLabel l;
      mainLayout.addWidget(&l);

      l.setPixmap(QPixmap("1.png"));
      l.setPixmap(QPixmap("2.png"));
      w.show();

      return app.exec();
      }@

      1.png and 2.png are of different sizes. But when the widget is shown the entire image is visible.
      Thus you must add your QLabel to a QLayout. It will automatically resize when you call QLabel::setPixmap().

      1 Reply Last reply
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on last edited by
        #3

        vc15, one problem I think with the above would be (without actually trying the code), the label will resize to a large pixmap, and further scale up if another larger pixmap is loaded. but then if you load a smaller pixmap, it won't scale back. this is something i have observed ...

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jdarnold
          wrote on last edited by
          #4

          That works just fine. In fact, it works in both directions (big then small or small then big). But if instead of a QWidget, I create a QDialog, it doesn't resize the same way. Is there a way I can get the QDialog to resize like a QWidget does?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jdarnold
            wrote on last edited by
            #5

            Okay, here's some more info, and I can see where chetankjain sees the problem. If you do a resize on the widget, things get a little trickier. If you resize it to be smaller than the pixmap, it will change size to be bigger. But if the resize is bigger, it won't change size to be smaller. So the following creates a 400x400 widget, with the label inside of it:

            @#include <QApplication>
            #include <QtGui>

            int main(int argc, char* argv[])
            {
            QApplication app(argc, argv);

            QWidget w;
            w.resize(400,400);

            QVBoxLayout mainLayout(&w);

            QLabel l;
            mainLayout.addWidget(&l);

            l.setPixmap(*new QPixmap(100,200));
            w.show();

            return app.exec();
            }
            @

            This iseven more important because Widgets (& dialogs) created by Qt Creator always have a resize call when they get created behind the curtain. So the trick is to resize the widget to be smaller than the smallest pixmap you can imagine getting inserted.

            1 Reply Last reply
            0
            • ? This user is from outside of this forum
              ? This user is from outside of this forum
              Guest
              wrote on last edited by
              #6

              so would the solution be ...

              if the pixmap size is smaller than the current label size, then call setGeometry on the dialog based on the pixmap size?

              also if not programatically, then one can use the setSizeGripEnabled on the dialog I think.

              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