Fit a QImage to Take up Whole QWidget [SOLVED]
-
Currently trying to figure out how to use scaled correctly to fit the size of the current QWidget (custom widget version though).
What I am trying to achieve is create a QWidget which contains a Image and a Label (to create an indication panel).
However no matter what I do I seem not to be able to get the Image to fit to the whole widget.
@#include "widget.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.showMaximized();return a.exec();
}
@@#include "main_image_holder.h"
#include "gui_image.h"Main_Image_Holder::Main_Image_Holder(QWidget *parent) :
QWidget(parent)
{
gui_image * Image = new gui_image(this);
QLabel * Label = new QLabel(this);
QGridLayout * Panel_Layout = new QGridLayout(this);
Panel_Layout->addWidget(Image);
Panel_Layout->addWidget(Label);
this->setLayout(Panel_Layout);
}void Main_Image_Holder::paintEvent(QPaintEvent* aEvent)
{
QStyleOption Opt;
Opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget,&Opt,&p,this);
}
@@#include "widget.h"
#include "main_image_holder.h"Widget::Widget(QWidget *parent)
: QWidget(parent)
{
Main_Layout = new QGridLayout(this);
QWidget * TopBar = new QWidget(this);
QWidget * BottomBar = new QWidget(this);Main_Image_Container = new Main_Image_Holder(this); TopBar->setStyleSheet("border: 1px solid green"); Main_Image_Container->setStyleSheet("border: 1px solid blue"); BottomBar->setStyleSheet("border: 1px solid red"); Main_Layout->addWidget(TopBar); Main_Layout->addWidget(Main_Image_Container); Main_Layout->addWidget(BottomBar); this->setLayout(Main_Layout);
}
Widget::~Widget()
{}@
@#include "gui_image.h"
#include <QDebug>
gui_image::gui_image(QWidget *parent) :
QLabel(parent)
{
//std::stringstream string;
//string << "border-image: url(:/file/radaroff.png) 0 0 0 0 stretch stretch; "
//<< "border-width: 0px; ";
//this->setStyleSheet(string.str().c_str());
Image = new QImage(QString::fromStdString(":/file/radaroff.png"),"PNG");
//Image->load(":/file/radaroff.png","PNG");
this->setImage(*Image);
this->setScaledContents(true);
//Image->scaled(this->width(),this->height(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
//qDebug() << "Image Height is = " << Image->size().height();
//this->setMinimumHeight(Image->size().height());
//this->setMinimumWidth(Image->size().width());}
void gui_image::paintEvent(QPaintEvent* aEvent)
{
//qDebug() << "Size Height = " << this->geometry().height();
//qDebug() << "Size is = " << this->geometry().width();setImage(*Image); QStyleOption Opt; Opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget,&Opt,&p,this); QPainter painter(this); if (!Image->isNull()) { //Image->scaledToWidth(50,Qt::FastTransformation); //Image->scaledToWidth(this->geometry().height(),Qt::FastTransformation); painter.drawImage(QPoint(0,0), *Image); }
}
QSize gui_image::sizeHint() const
{
return Size;
}void gui_image::setImage(const QImage& image)
{
Size = image.size();
QSize Current_Size(this->geometry().width(),this->geometry().height());
//Image->copy(this->geometry().width(),this->geometry().height(),this->geometry().width(),this->geometry().height());
*Image = image.scaled(Current_Size,Qt::KeepAspectRatio,Qt::SmoothTransformation);
//qDebug() << "Image size is = " << size().height();
//qDebug() << "Image size is = " << size().width();
update();
}
@@#ifndef WIDGET_H
#define WIDGET_H#include "main_image_holder.h"
#include <QWidget>
#include <QGridLayout>
#include <QImage>class Widget : public QWidget
{
Q_OBJECTpublic:
Widget(QWidget *parent = 0);
~Widget();private:
QGridLayout * Main_Layout;
Main_Image_Holder * Main_Image_Container;
};#endif // WIDGET_H
@@#ifndef MAIN_IMAGE_HOLDER_H
#define MAIN_IMAGE_HOLDER_H#include <QWidget>
#include <QStyleOption>
#include <QPainter>
#include <QGridLayout>
#include <QLabel>class Main_Image_Holder : public QWidget
{
Q_OBJECT
public:
explicit Main_Image_Holder(QWidget *parent = 0);void paintEvent(QPaintEvent* aEvent);
signals:
public slots:
private:
/// Filename to the state image.
std::string fImageFilename;
QImage * Image;
QSize Size;};
#endif // MAIN_IMAGE_HOLDER_H@
@#ifndef GUI_IMAGE_H
#define GUI_IMAGE_H#include "main_image_holder.h"
#include <QWidget>
#include <sstream>#include <string>
#include <QSize>
class gui_image : public QLabel
{
Q_OBJECT
public:
explicit gui_image(QWidget *parent = 0);void paintEvent(QPaintEvent* aEvent); QSize sizeHint() const; void setImage(const QImage& image);
signals:
public slots:
private:
QSize Size;
QImage * Image;};
#endif // GUI_IMAGE_H
@Currently What happens is that the item is displayed but the image data is slowly degraded as it changes size overtime, with it centred to the left hand side of the screen. What I want it to do is keep the image data (tried using copy but failed cause im useless), so that it doesn't lose quality when it's resized (guess I need to hold the original copy and then resize a copy instead), aswell as actually being centred to the middle of the widget. If anyone can help i would be very grateful as everything i seem to try just doesn't work.
-
Would using QIcon be a better method also (as thing will be used to create a button)?
-
Well I was trying to make sure that when using scaled it keeps to the aspect ratio but allow it to change it's size based on the main window (however that isnt working, it just degrades the picture overtime until the picture becomes invisible).
What I am trying to do is just create a indication panel that tells the user when a certain action has occurred (lost comms for example). So what i would like the panel to do, is to change size as the main window is made larger or smaller (keeping it relative to the rest of the main window).
Also the pointer for QImage was just a pointer cause i prefer pointers over working directly with objects, no specific reason (however quite happy to change it to just a object if it would help).
-
Hello,
Try to use QTranform.
for example in the paintEvent :@
QTransform transform;// compute the ratio before and then apply it
transform.scale(ratio, ratio);
painter.setTransform(transform);
painter.drawImage(0, 0, image);//reset the QPainter
painter.setTransform(QTransform());
@ -
So something like a QDockWidget ?
You could then just have a QLabel inside it to show your image(s) (unless I misunderstood your final goal)