2

I want to use a png image with transparency and custom border in Qt mainwindow background like this.


(source: shiaupload.ir)

is there any good idea?

Community
  • 1
  • 1
mohsen amiri
  • 73
  • 1
  • 2
  • 7
  • 1
    [QWidget::setMask(const QBitmap &bitmap)](http://qt-project.org/doc/qt-5.0/qtwidgets/qwidget.html#setMask) and [QPixmap::mask()](http://qt-project.org/doc/qt-5.0/qtgui/qpixmap.html#mask) might be what you're looking for. – thuga Sep 19 '13 at 10:24
  • 2
    You can also do something like `this->setWindowFlags(Qt::FramelessWindowHint);` `this->setAttribute(Qt::WA_TranslucentBackground);` and then paint your background. – thuga Sep 19 '13 at 10:41
  • @thuga - your last post is an answer. I want to add, that if Qt4 is used - there is a bug with minimizing and restoring window: https://bugreports.qt-project.org/browse/QTBUG-17548 – Dmitry Sazonov Sep 19 '13 at 10:46

1 Answers1

3

You can do something like this:

this->setWindowFlags(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);

Then paint the background of your window in the paint event.

Note: As pointed out by Dmitry, in Qt4 there is a bug with minimizing and restoring the window.

Another option is to use QWidget::setMask(const QBitmap & bitmap), but this could be slow if the region is complex.

thuga
  • 12,601
  • 42
  • 52
  • Hi, can you point out how achieve the transparency through setting mask? – SexyBeast Jan 05 '16 at 01:29
  • @Cupidvogel Is the example in the documentation insufficient? – thuga Jan 05 '16 at 06:50
  • No, it is not working. I tried to apply that here: http://stackoverflow.com/questions/34372291/qt-render-qframe-with-image-as-background-without-using-wa-translucentbackgrou, I could not make it work. – SexyBeast Jan 05 '16 at 06:56
  • @Cupidvogel Try it on an empty project with just a main window nothing else. – thuga Jan 05 '16 at 07:28