PDA

View Full Version : Need to display a QFrame with only background image and nothing else



Cupidvogel
2nd June 2015, 08:59
I am making a desktop carousel app. There I need to show image widgets, which might contain other sub-widgets as well. For that I am using a QFrame with the required image as background. Here is the image I am trying to use: http://static.vecteezy.com/system/resources/previews/000/010/121/original/iPad_Vector.png. What I want is that only the image shows up, no background image or anything shows up as well, so to the user it looks like just the image. Here is my code:



setGeometry(QRect(100, 20, 325,400));
setFrameStyle(QFrame::StyledPanel);
setStyleSheet("QFrame#ImageFrame { background-color: transparent; background: url(:icon/ipad-skin); }");
setAutoFillBackground(false);


However, I ma getting this as a result:

11190

How do I make the grey background of the QFrame go and display only the image?

Cupidvogel
17th December 2015, 20:48
I managed to find a working solution -

main.h:



#ifndef main_h
#define main_h

#include <QFrame>
#include <QPixmap>

class MyFrame : public QFrame
{
public:
MyFrame(QWidget * parent);

virtual void paintEvent(QPaintEvent * e);

private:
QPixmap _pixmap;
};

#endif


main.cpp



#include <QApplication>
#include <QPainter>
#include "main.h"

MyFrame :: MyFrame(QWidget * parent) : QFrame(parent, Qt::Window|Qt::FramelessWindowHint)
{
setAttribute(Qt::WA_TranslucentBackground);

_pixmap.load("/Users/jaf/iPad_Vector.png");
resize(_pixmap.size());
}

void MyFrame :: paintEvent(QPaintEvent * /*e*/)
{
QPainter p(this);
p.drawPixmap(0,0,width(),height(), _pixmap);
}

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

MyFrame f(NULL);
f.show();

return app.exec();
}


This works. However, in my use case, I need to display an image that is rendered via GL inside the QFrame (specifically, in the viewport within the iPad image we can see here). In Windows, setting the Qt::WA_TranslucentBackground property makes that GL-rendered image invisible. It works fine in Mac, though. How can I make it behave in the same way without using Qt::WA_TranslucentBackground?