0

Earlier last week I created a window in Python that resized the main window to the dimensions of the background image. I wanted to do the same in QT. I've managed to figure out the syntax to resize the main window through its constructor.

this->setFixedSize(QSize(600, 600));

I'm curious how I might now set the width and height parameters to the same parameters of an image in the resource file. I was thinking something like this:

QGraphicsPixmapItem image(QPixmap("url(:/images/background.png);"));
int x = image.width
int y = image.length
this->setFixedSize(QSize(x, y));

edit:

In summary, I want to resize the main window to the same dimensions of an image file which so the window wraps around the background image. According to this post I have to parse the image header to read the dimensions... sounds too complicated. Is there a third party library?

Community
  • 1
  • 1
the_prole
  • 8,275
  • 16
  • 78
  • 163
  • Have you tried `resize()`? – Ilya Dec 05 '14 at 14:15
  • No I haven't. I want to resize the main window to the same dimensions of an image file which so the window wraps around the background image. According to [this post](http://stackoverflow.com/questions/5354459/c-how-to-get-the-image-size-of-a-png-file-in-directory) I have to parse the image header to read the dimensions... sounds too complicated. Is there a third party library? – the_prole Dec 05 '14 at 14:17

2 Answers2

1

Instead of using QGraphicsPixmapItem use QImage to get the size of your image.

QImage image(":/images/background.png");
if(!image.isNull())
    setFixedSize(image.size());
else
    //loading the image failed, show some error message or something
thuga
  • 12,601
  • 42
  • 52
  • Thanks, I will try this. What header do I have to import to use this library `QImage`? – the_prole Dec 05 '14 at 14:20
  • @the_prole `#include `. It is also mentioned in the docs of `QImage` I linked to in the answer. – thuga Dec 05 '14 at 14:21
  • Why do the headers in my `main.cpp` look different from the headers in the QT documentation e.g. `#include "ui_mainwindow.h"` vs. `#include `? In any case, `QImage` is not recognized... – the_prole Dec 05 '14 at 14:35
  • 1
    @the_prole Check [this question](http://stackoverflow.com/questions/3162030/difference-between-angle-bracket-and-double-quotes-while-including-heade). What do you mean by not recognized? – thuga Dec 05 '14 at 14:37
0

Have you tried to adjustSize , after setting to new ?

this->setFixedSize(QSize(x, y));
this->adjustSize();
hwg
  • 65
  • 1
  • 9