QFrame与背景图象和其他透明背景 [英] QFrame with background image and otherwise transparent background

查看:4477
本文介绍了QFrame与背景图象和其他透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作桌面轮播应用程式。在那里我需要显示图像小部件,它可能包含其他子小部件。为此,我使用一个 QFrame 与所需的图像作为背景。以下是我要使用的图片:,并在此处:无边框和透明的窗口qt5 但这些不能解决我的问题。最后一个解决方案使我的QFrame看起来像这样:





QFrame现在带有一个标题栏,它只是我想要的第一个。



编辑 - 此解决方案适用,但在我的使用情况下,我需要显示一个图像,通过GL渲染在QFrame(具体来说,在iPad图像可以看到这里)。在Windows中,设置 Qt :: WA_TranslucentBackground 属性使得GL渲染的图像不可见。它工作正常在Mac,虽然。所以我正在寻找一个解决方案,也将在Windows中工作。

解决方案

这段代码适用于我(在MacOS / X 10.10.3下使用Qt 5.5.0-beta测试; d期望它在任何Qt版本4.5.0或更高版本下工作):



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>
#includemain.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();
}

截图(显示桌面背景应用程序的窗口) p>


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: image link. 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 am getting this as a result:

I tried this as well (obtained from here) (and removing the stylesheet):

void MyWidget::paintEvent(QPaintEvent *p)
{
    QPainter* pPainter = new QPainter(this);
    pPainter->drawPixmap(rect(), QPixmap(":icon/newskin.png"));
    delete pPainter;
    QWidget::paintEvent(p);
}

Nothing different, the exact same result. The greyness of the background still shows.

How do I make the grey background of the QFrame go and display only the image (the dimensions I am setting are the same as the image)?

P.S I am aware that similar questions hve been answered here: QWidget transparent background (but not the children), and here: Frameless and transparent window qt5 but these doesn't solve my problem. The last solution makes my QFrame look like this:

The QFrame now comes with a title bar which is anything but what I wanted in the first place.

Edit - This solution works, but 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. So I am looking for a solution which will work in Windows as well.

解决方案

This code works for me (tested under MacOS/X 10.10.3, using Qt 5.5.0-beta; I'd expect it to work under any Qt version 4.5.0 or higher though):

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();
}

Screenshot (showing the app's window in front of my desktop background):

这篇关于QFrame与背景图象和其他透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆