2

How to do transparent background in QPushButton?

I have a background picture and I want to make the button transparent.

I tried to use:

reg_screen_var.new_account.setStyleSheet("QPushButton {color: #F6F6F6; background: transparent}")

and:

reg_screen_var.new_account.setAttribute(Qt.WA_TranslucentBackground, True)

But I get this result:

screenshot

I'm using Ubuntu 16.04.

full code:

from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QLineEdit, QPushButton)
from PyQt5.QtGui import (QPixmap, QFont, QMouseEvent)
from PyQt5.QtCore import (Qt, QEvent, pyqtSignal, QCoreApplication, QObject)
import sys

app = QApplication(sys.argv)
root = QWidget()


class RegisterScreenVar():

    def __init__(self):

        self.drop_logo = QLabel(root)
        self.drop_png = QPixmap("images/DropMeLogo.png")
        self.login_but = QLabel(root)
        self.login_but_img = QPixmap("images/Login_but.png")
        self.login_lab = QLabel('Login', root)
        self.pas_lab = QLabel('Password', root)
        self.login_box = QLineEdit(root)
        self.reg_box = QLineEdit(root)
        self.new_account = QPushButton("Don't have an account?", root)


reg_screen_var = RegisterScreenVar()


def drop_me():

    def register_screen(reg_screen_var, root, app):
        # design of register screen

        root.resize(1280, 720)
        root.move(400, 200)
        root.setWindowTitle('Drop_Me')
        root.setStyleSheet('background-image: url(images/reg_back.png);')

        reg_screen_var.login_but.setPixmap(reg_screen_var.login_but_img)
        reg_screen_var.login_but.setAttribute(Qt.WA_TranslucentBackground, True)
        reg_screen_var.login_but.move(520, 500)

        reg_screen_var.drop_logo.setPixmap(reg_screen_var.drop_png)
        reg_screen_var.drop_logo.setAttribute(Qt.WA_TranslucentBackground, True)
        reg_screen_var.drop_logo.move(370, 0)

        reg_screen_var.login_lab.setFont(QFont("OpenSans-Regular", 20, QFont.Bold))
        reg_screen_var.login_lab.setStyleSheet('QLabel {color: #F6F6F6}')
        reg_screen_var.login_lab.setAttribute(Qt.WA_TranslucentBackground, True)
        reg_screen_var.login_lab.move(580, 230)

        reg_screen_var.pas_lab.setFont(QFont("OpenSans-Regular", 20, QFont.Bold))
        reg_screen_var.pas_lab.setStyleSheet('QLabel {color: #F6F6F6}')
        reg_screen_var.pas_lab.setAttribute(Qt.WA_TranslucentBackground, True)
        reg_screen_var.pas_lab.move(550, 350)

        reg_screen_var.login_box.setFont(QFont("OpenSans-Regular", 18, QFont.Bold))
        reg_screen_var.login_box.setStyleSheet('QLineEdit {color:black; width: 300px}')
        reg_screen_var.login_box.move(470, 290)

        reg_screen_var.reg_box.setFont(QFont("OpenSans-Regular", 18, QFont.Bold))
        reg_screen_var.reg_box.setStyleSheet('QLineEdit {color:black; width: 300px}')
        reg_screen_var.reg_box.move(470, 400)

        reg_screen_var.new_account.setStyleSheet("QPushButton {color: #F6F6F6}")
        reg_screen_var.new_account.setFont(QFont("OpenSans-Regular", 10, QFont.Bold))
        reg_screen_var.new_account.setAutoFillBackground(True)
        reg_screen_var.new_account.move(540, 620)


        root.show()

        sys.exit(app.exec_())

    register_screen(reg_screen_var, root, app)

drop_me()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
geneus
  • 21
  • 1
  • 3
  • This problem with OS? – geneus Dec 17 '17 at 20:39
  • The solution is probably not the same for all platforms. It might help if you also posted a simple test script that shows how you created the window shown in your screenshot. – ekhumoro Dec 17 '17 at 20:49
  • Github.com/geneus003/DropMe – geneus Dec 17 '17 at 20:56
  • I'm commit actual code on github (https://github.com/Geneus003/DropMe/blob/master/DropMe.py) or U can use full code in task Sorry for late – geneus Dec 18 '17 at 12:47
  • When I add the two lines at the top of your question to the script, it works fine for me on archlinux (using qt-5.10.0 and pyqt 5.9.2). What *specifc* versions of qt and pyqt are you using? – ekhumoro Dec 18 '17 at 17:43
  • Im using PyQt 5.9.2 and Qt version 5.9.3 – geneus Dec 18 '17 at 17:48
  • I suppose there might be a bug in qt5 that only affects ubuntu. If the transparency works using qt4 (or an earlier version of qt5), that might confirm the bug. – ekhumoro Dec 18 '17 at 17:59

2 Answers2

1

In Qt Designer Linux (Debian, Ubuntu) transparent background gave me Black color , so Stylesheet of Qpush Button settings:

border : 0;
background: transparent;

i´ve spent hours trying. :)

Gaston
  • 129
  • 2
  • 5
0

I think it's because of the style.

app.setStyle('Fusion')

Gives the me the problem. Try using

app.setStyle("Windows")

That should be able to fix the problem. But you won't be able to use the 'Fusion' style. I hadn't tested it with other styles yet. I hope that solves your problem.

Cowman
  • 11
  • 4