[Python] PyQt6: Unwanted Bounding Box Around My Round Button Overlay On Top Of VLC Video Widget

Discussão em 'Python' iniciado por Stack, Maio 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to put a round button on top of a playing video (VLC video widget). I'm using PyQt6. The round button displays fine before the video is playing. But, once the video starts to play, Qt adds a bounding box around my round button. One suboptimal option is to use a circular clipping mask. But the round button will have jagged edge since the mask is binary. I've tried many suggested solutions and still unable to get rid of the box around my round button. I made a minimal demo of what I have tried below. Instead of QPushbutton, I made a QWidget that draws a simple circle. The problem is the same: can't get rid of the black box around the circle.

    enter image description here

    import vlc
    import sys
    from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget
    from PyQt6.QtCore import Qt, QRectF
    from PyQt6.QtGui import QPainter, QColor, QBrush, QPen

    # --- Circle Widget ---
    class CircleWidget(QWidget):
    def __init__(self, parent=None, diameter=100, color=QColor(255, 0, 0, 128)): # Red, 50% transparent
    super().__init__(parent)
    self.diameter = diameter
    self.color = color
    self.setFixedSize(diameter, diameter)

    # self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True)
    self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True)
    self.setAutoFillBackground(False)
    self.setWindowFlags(Qt.WindowType.FramelessWindowHint)
    self.setAttribute(Qt.WidgetAttribute.WA_NoSystemBackground, True)

    self.setStyleSheet("background: transparent; border: none;")

    def paintEvent(self, event):
    painter = QPainter(self)
    painter.setRenderHint(QPainter.RenderHint.Antialiasing)
    painter.setBrush(QBrush(self.color))
    painter.setPen(QPen(Qt.PenStyle.NoPen))
    painter.drawEllipse(QRectF(self.rect()))
    # -------------------------------------

    VIDEO_PATH = r"D:\test.mp4"

    app = QApplication(sys.argv)
    main_window = QMainWindow()
    main_window.setWindowTitle("PyQt6 VLC Sibling Overlay Test")
    main_window.setGeometry(100, 100, 640, 480)

    main_container = QWidget()
    main_window.setCentralWidget(main_container)

    # Video Output Widget (Child of container)
    video_widget = QWidget(parent=main_container)
    video_widget.setStyleSheet("background-color: black;")

    # Overlay the Circle on top of the video
    circle_overlay = CircleWidget(parent=main_container)

    player = None
    instance = None

    try:
    instance = vlc.Instance()
    player = instance.media_player_new()
    media = instance.media_new_path(VIDEO_PATH) # any video file
    player.set_media(media)

    main_window.show()
    QApplication.processEvents()

    video_widget.setGeometry(main_container.rect())

    # Position the circle in the container
    overlay_x = 80
    overlay_y = 80
    circle_overlay.move(overlay_x, overlay_y)
    circle_overlay.show()
    circle_overlay.raise_() # Ensure it's visually on top of video

    win_id = video_widget.winId()
    if sys.platform.startswith('win'):
    player.set_hwnd(win_id)
    print(f"Set HWND to {win_id}")
    else:
    print(f"Warning: Platform specific VLC output binding not implemented for {sys.platform}")

    # --- Play Video ---
    play_result = player.play()
    if play_result != 0:
    print("Error: Failed to start playback.")
    sys.exit(1)

    status = app.exec()

    except Exception as e:
    print(f"An error occurred: {e}")

    finally:
    player.stop()


    Any help would be much appreciated!

    I just need to display a clean round button over the video playing in the background.

    Continue reading...

Compartilhe esta Página