Results 1 to 2 of 2

Thread: If the background is transparent, PrimaryScreen().grabWindow(self.winId()) Question.

  1. #1
    Join Date
    Jun 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default If the background is transparent, PrimaryScreen().grabWindow(self.winId()) Question.

    I'm trying to use the primaryScreen().grabWindow(self.winId() to implement a screenshot, but when the background is not clear, the screenshot is good.
    If the background is transparent, the stored image is saved as a black image. Why is this happening?

    Qt Code:
    1. import os , sys , time
    2. from PyQt5 import QtWidgets
    3. from PyQt5.QtWidgets import *
    4. from PyQt5.QtGui import *
    5. from PyQt5.QtCore import *
    6. import numpy as np
    7.  
    8. class MyApp(QWidget):
    9.  
    10. def __init__(self):
    11. super().__init__()
    12. self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
    13. self.setAttribute(Qt.WA_TranslucentBackground)
    14. self.initUI()
    15.  
    16. def initUI(self):
    17. self.setGeometry(30, 30, 300, 300)
    18. self.setWindowTitle('blRect')
    19. self.show()
    20.  
    21. def screenshot(self):
    22. self.preview_screen = QApplication.primaryScreen().grabWindow(self.winId() )
    23. self.preview_screen.save('test.jpg', "jpg")
    24.  
    25. def paintEvent(self, e):
    26. qp = QPainter()
    27. qp.begin(self)
    28. self.draw_rect(qp)
    29. self.screenshot()
    30. qp.end()
    31.  
    32. def draw_rect(self, qp):
    33. qp.setBrush(QColor(255, 255, 0))
    34. qp.setPen(QPen(QColor(0, 0, 0), -1))
    35. qp.setBrush(QColor(0, 0, 0))
    36. # print(self.width() , self.height())
    37. for i in range(3):
    38. rand_x = 100 * np.random.randn()
    39. rand_y = 100 * np.random.randn()
    40. qp.drawRect(self.width() /2 + rand_x, self.height() / 2 + rand_y, 100, 20)
    41.  
    42. def main():
    43. app = QApplication(sys.argv)
    44. ex = MyApp()
    45. sys.exit(app.exec_())
    46.  
    47. if __name__ == '__main__':
    48. main()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,649
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: If the background is transparent, PrimaryScreen().grabWindow(self.winId()) Questi

    Quote Originally Posted by yskioi View Post
    I'm trying to use the primaryScreen().grabWindow(self.winId() to implement a screenshot, but when the background is not clear, the screenshot is good.
    If the background is transparent, the stored image is saved as a black image. Why is this happening?
    The translucent area of your widget's client area is all black with a zero alpha channel. You save as JPEG, which has no alpha channel, and therefore the result is a solid black background with black boxes drawn on top. If you save as PNG, which has alpha channel support, then you get a transparent image with black boxes draw on top.

    None of the content you can see through the translucent background is part of your widget's content, so it is not captured. You see it on screen because your window manager (whatever platform) is composing your widget with what lies behind (On KDE toggle compositing with Shift-Alt-F12, maybe switch off Aero on Windows?). It is the relevant portion of the composed screen buffer you need to be accessing.

    Have a look at Spectacle for an example of how you might do this on X11 systems.
    Maybe Lightscreen on Windows.
    QScreen::grabWindow() is probably useful.
    Last edited by ChrisW67; 20th July 2020 at 06:28.

Similar Threads

  1. Gif as Background for QListWidget or transparent background
    By gunturrohith in forum Qt Programming
    Replies: 0
    Last Post: 21st March 2016, 07:43
  2. Replies: 2
    Last Post: 27th April 2015, 11:36
  3. Replies: 4
    Last Post: 27th November 2013, 15:15
  4. transparent background
    By fruzzo in forum Qt Programming
    Replies: 13
    Last Post: 18th March 2008, 14:42
  5. Transparent background on QLabel on transparent QWidget
    By codeslicer in forum Qt Programming
    Replies: 1
    Last Post: 13th February 2008, 02:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.