Results 1 to 5 of 5

Thread: Cant get Screenshot of Desktop right as Widget-Background

  1. #1
    Join Date
    Dec 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Cant get Screenshot of Desktop right as Widget-Background

    Hi,

    i tried to get the desktop as screenshot and then display exactly the part of it which my widget/window overlaps in the widget's background.

    i work on a pc with 2 monitors and get different result ( both are broken ) for each

    Left Monitor (primary):

    - Image gets distorted on Window resize
    - Image contains the widget itself, and i dont know how to hide it during screenshoting the desktop

    i tried hide, get screenshot, show with setUpdateEnabled false and true again, but some events still are fired -> infinite loop, bad crash

    Right Monitor:

    - cant copy part of Screenshot, it is always the full screenshot as widget-background

    So my most important questions will be:

    1. How do i hide and show widget without getting floods of event, signals
    2. How do i get the QScreen object from the widget position
    3. What are your opinions/suggestions

    TransparentPanel.h
    Qt Code:
    1. #include <QWidget>
    2. #include <QPixmap>
    3.  
    4. // ============================================================================
    5. /// @class TransparentPanel
    6. // ============================================================================
    7.  
    8. class TransparentPanel : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. private:
    13. QPixmap mBG;
    14.  
    15. public:
    16. ///@brief Constructor
    17. explicit TransparentPanel( QWidget* parent = 0 );
    18.  
    19. ///@brief Destructor
    20. ~TransparentPanel();
    21.  
    22. void updateBG();
    23.  
    24. protected:
    25. virtual void paintEvent( QPaintEvent* event ) override;
    26. virtual void resizeEvent(QResizeEvent* event) override;
    27. virtual void moveEvent( QMoveEvent* event) override;
    28. // virtual void enterEvent( QEvent* event ) override;
    29. // virtual void leaveEvent( QEvent* event ) override;
    30. // virtual void mousePressEvent ( QMouseEvent* event );
    31. // virtual void mouseReleaseEvent ( QMouseEvent* event );
    32.  
    33. };
    To copy to clipboard, switch view to plain text mode 

    TransparentPanel.cpp
    Qt Code:
    1. #include "TransparentPanel.h"
    2.  
    3. #ifndef VERBOSE
    4. #define VERBOSE 1
    5. #endif
    6.  
    7. #include <QPainter>
    8. #include <QMouseEvent>
    9. #include <QApplication>
    10. #include <QDesktopWidget>
    11. #include <QScreen>
    12. #include <QDebug>
    13.  
    14. // ============================================================================
    15. TransparentPanel::TransparentPanel( QWidget* parent )
    16. : QWidget(parent)
    17. // ============================================================================
    18. {
    19. setMinimumSize(10,10);
    20. setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    21. setAutoFillBackground( false );
    22. //setAttribute( Qt::WA_OpaquePaintEvent );
    23. setAttribute( Qt::WA_NoSystemBackground );
    24. //setAttribute( Qt::WA_TranslucentBackground );
    25. setUpdatesEnabled( true );
    26.  
    27. //MMDebug("TransparentPanel::TransparentPanel()\n")
    28. //MMDebug("QApplication::desktop()->winId() = %x\n", (void*)QApplication::desktop()->winId() )
    29. //MMDebug("GetDesktopWindow() = %x\n", (void*)GetDesktopWindow() )
    30. updateBG();
    31. }
    32.  
    33. // ============================================================================
    34. TransparentPanel::~TransparentPanel()
    35. // ============================================================================
    36. {
    37. //MMDebug("TransparentPanel::~TransparentPanel()\n")
    38. }
    39.  
    40. // ============================================================================
    41. void TransparentPanel::paintEvent( QPaintEvent* event )
    42. // ============================================================================
    43. {
    44. if (!isVisible())
    45. return;
    46.  
    47. //MMDebug("TransparentPanel::paintEvent()\n")
    48.  
    49. QPainter dc(this);
    50. dc.setRenderHint( QPainter::NonCosmeticDefaultPen );
    51.  
    52. //dc.fillRect( rect(), Qt::white);
    53. if (!mBG.isNull())
    54. {
    55. dc.drawPixmap( rect(), mBG);
    56. }
    57.  
    58. QWidget::paintEvent(event);
    59. }
    60.  
    61. // ============================================================================
    62. void TransparentPanel :: resizeEvent(QResizeEvent* event)
    63. // ============================================================================
    64. {
    65. //MMDebug("TransparentPanel::resizeEvent()\n")
    66. updateBG();
    67. //QWidget::resizeEvent(event);
    68. }
    69.  
    70. // ============================================================================
    71. void TransparentPanel::moveEvent( QMoveEvent* event)
    72. // ============================================================================
    73. {
    74. //MMDebug("TransparentPanel::moveEvent()\n")
    75. updateBG();
    76. //QWidget::moveEvent(event);
    77. }
    78.  
    79. //// ============================================================================
    80. //void TransparentPanel::enterEvent( QEvent* event )
    81. //// ============================================================================
    82. //{
    83. // QWidget::enterEvent(event);
    84. //}
    85.  
    86. //// ============================================================================
    87. //void TransparentPanel::leaveEvent( QEvent* event )
    88. //// ============================================================================
    89. //{
    90. // QWidget::leaveEvent(event);
    91. //}
    92.  
    93. //// ============================================================================
    94. //void TransparentPanel :: mousePressEvent ( QMouseEvent* event )
    95. //// ============================================================================
    96. //{
    97. // QWidget::mousePressEvent( event );
    98. //}
    99.  
    100. //// ============================================================================
    101. //void TransparentPanel :: mouseReleaseEvent ( QMouseEvent* event )
    102. //// ============================================================================
    103. //{
    104. // QWidget::mouseReleaseEvent( event );
    105. //}
    106.  
    107.  
    108. // ============================================================================
    109. void TransparentPanel::updateBG()
    110. // ============================================================================
    111. {
    112. // if (!isVisible())
    113. // return;
    114.  
    115. //// setVisible(false);
    116. // mBG = QPixmap(); // clear image for low memory situations on weak devices.
    117. // QPoint screenTopLeft = mapToGlobal( geometry().topLeft() );
    118. // int x = screenTopLeft.x();
    119. // int y = screenTopLeft.y();
    120. // int w = width();
    121. // int h = height();
    122. // //mBG = QPixmap::grabWindow(QApplication::desktop()->winId(), x,y, w,h );
    123.  
    124. // //QScreen* screen = QApplication::screens()->primaryScreen();
    125. // QScreen* screen = QApplication::primaryScreen();
    126. // if (screen)
    127. // //mBG = screen->grabWindow(QApplication::desktop()->window()->winId(), x,y, w,h );
    128. // mBG = screen->grabWindow((WId)GetDesktopWindow(), x,y, w,h );
    129.  
    130. //// setVisible(true);
    131. // update();
    132.  
    133. QRect g = geometry();
    134. QPoint p1 = mapToGlobal( g.topLeft() );
    135. QPoint p2 = mapToGlobal( g.bottomRight() );
    136.  
    137. // setVisible(false);
    138. QPoint screenTopLeft = mapToGlobal( pos() );
    139. int ax = screenTopLeft.x();
    140. int ay = screenTopLeft.y();
    141. int aw = width();
    142. int ah = height();
    143.  
    144. QRect r( p1, p2 );
    145.  
    146. // QImage img = pix.toImage();
    147. // for ( int j = 0; j < img.height(); ++j )
    148. // {
    149. // for ( int i = 0; i < img.width(); ++i )
    150. // {
    151. // QColor c = img.pixel(i,j);
    152. // c.setAlpha(255);
    153. // img.setPixel( i,j, c.rgba() );
    154. // }
    155. // }
    156.  
    157. // //QImage img = pix.toImage();
    158. // dc.drawImage( rect(), img );
    159.  
    160. // setUpdatesEnabled(false);
    161. // hide();
    162. //QPixmap pix = QDesktopWidget().grab( QRect(x,y,w,h) );
    163. //QPixmap pix = QPixmap::grabWindow(QApplication::desktop()->winId(), x,y, w,h );
    164. //QPixmap screenShot = QApplication::primaryScreen()->grabWindow( (WId)GetDesktopWindow() );
    165. //mBG = screenShot.copy( ax,ay,aw,ah );
    166. //mBG = screenShot.copy( r );
    167.  
    168. // mBG = QApplication::primaryScreen()->grabWindow( (WId)GetDesktopWindow(), r.left(), r.top(), r.width(), r.height() );
    169.  
    170. QPixmap scr = QApplication::primaryScreen()->grabWindow( (WId)GetDesktopWindow() );
    171. mBG = scr.copy( r );
    172.  
    173.  
    174.  
    175. #ifdef VERBOSE
    176. std::stringstream s;
    177. s << "pos ( x=" << pos().x() << ",y=" << pos().y() << ")\n";
    178. s << "a ( x=" << ax << ",y=" << ay << ",w=" << aw << ",h=" << ah << ")\n";
    179. s << "g ( x=" << g.left() << ",y=" << g.top() << ",w=" << g.width() << ",h=" << g.height() << ")\n";
    180. s << "r ( x=" << r.left() << ",y=" << r.top() << ",w=" << r.width() << ",h=" << r.height() << ")\n";
    181. s << "scr ( w=" << scr.width() << ",h=" << scr.height() << ")\n";
    182. s << "mBG ( w=" << mBG.width() << ",h=" << mBG.height() << ")\n";
    183. qDebug() << QString::fromStdString(s.str());
    184. //MMPrint("%s", s.str().c_str())
    185. #endif
    186.  
    187. // show();
    188. // setUpdatesEnabled(true);
    189. update();
    190. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cant get Screenshot of Desktop right as Widget-Background

    Quote Originally Posted by gerdb View Post
    1. How do i hide and show widget without getting floods of event, signals
    By calling hide() and show() respectively.

    2. How do i get the QScreen object from the widget position
    No idea what you mean,.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cant get Screenshot of Desktop right as Widget-Background

    Hi,

    1.) i did add hide(); command before getting screenshot and right after that i called show();

    --> Results in crash ( as i already said )

    2.) When i take the screenshot

    aka QPixmap scr = QApplication:rimaryScreen()->grabWindow( (WId)GetDesktopWindow() );

    i get a 1920x1080 pixels, meaning that it is only one of my monitors and not both, thatswhy i assumed the screenshot is taken monitor-wise

    and i wanna know how to determine the current monitor where the widgets is one and take the screenshot from that monitor?

    Is that more clear?

    Thx in advance

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cant get Screenshot of Desktop right as Widget-Background

    Quote Originally Posted by gerdb View Post
    Hi,

    1.) i did add hide(); command before getting screenshot and right after that i called show();

    --> Results in crash ( as i already said )
    You said you called setUpdatesEnabled(false). That's something completely different to calling hide().

    2.) When i take the screenshot

    aka QPixmap scr = QApplication:rimaryScreen()->grabWindow( (WId)GetDesktopWindow() );

    i get a 1920x1080 pixels, meaning that it is only one of my monitors and not both, thatswhy i assumed the screenshot is taken monitor-wise

    and i wanna know how to determine the current monitor where the widgets is one and take the screenshot from that monitor?
    Hmm... QWindow::screen()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2015
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cant get Screenshot of Desktop right as Widget-Background

    Can you solve problem?
    i use full construction:

    QRect systemScreenRect = getSystemScreenRect();
    return QApplication::screens().at(1)->grabWindow(0,0,0,
    systemScreenRect.width(),
    systemScreenRect.height());

    but problem not solved full

Similar Threads

  1. Replies: 5
    Last Post: 10th August 2013, 09:52
  2. Don't redraw desktop, use a screenshot instead.
    By danciac in forum Qt Programming
    Replies: 2
    Last Post: 8th September 2010, 11:40
  3. QT Desktop Screenshot not working
    By harleyskater in forum Qt Programming
    Replies: 11
    Last Post: 27th June 2010, 04:25
  4. setting my widget background as desktop
    By sanjayshelke in forum Qt Programming
    Replies: 4
    Last Post: 8th July 2009, 05:35
  5. Widget as desktop background
    By abeinoe in forum The GraphicsView Framework
    Replies: 0
    Last Post: 30th January 2009, 07:15

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
  •  
Qt is a trademark of The Qt Company.