Small demo of QT screenshot screen capture

Source: Internet
Author: User

No code, no truth. I'm not so embarrassed.

Demo:

 

 

 

 

Layout:

Create a graphical project and create the following layout: mesh layout, horizontal layout, and vertical layout. The main layout of the interface is vertical.

 

 

Right-clickNewClick, select the clicked () signal, and enter the following code to jump to the corresponding slot

Void screenshot: on_newscreenshot_clicked ()

{

If (ui-> checkbox-> ischecked ())

{

This-> hide ();

This-> timer = new qtimer;

Qobject: connect (timer, signal (timeout (), this, slot (shotscreen ()));

Timer-> Start (ui-> spinbox-> value () * 1000 );

// If the current check box is selected, the screenshot signal is triggered by the timer.

}

Else

{

Qapp-> BEEP ();

}

}

 

The above Code uses a custom slot, so it should be declared in the header file

 

Private slots:

Void shotscreen ();

 

Implementation slots in source files:

Void screenshot: shotscreen ()

{

Pixmap = qpixmap: grabwindow (qapplication: desktop ()-> winid ());

UI-> screenlabel-> setpixmap (pixmap. Scaled (ui-> screenlabel-> size ()));

This-> timer-> stop ();

This-> show ();

}

 

Each window has its own private ID. Just like the process number, when you access a window, you can obtain the ID of the current desktop. You can use the grabwindow () function to capture the image and assign it to pixmap. The current timer is revoked to display the window again.

 

Save the current image to a specified directory, right-click the Save button, select the clicked () signal, and enter the following code:

 

Void screenshot: on_savescrenshot_clicked ()

{

Qstring format = "PNG ";

Qstring initialpath = qdir: currentpath () + Tr ("/untitled.") + format;

 

Qstring filename = qfiledialog: getsavefilename (this, TR ("Save "),

Initialpath,

TR ("% 1 files (*. % 2); all files (*)")

. Arg (format. toupper ())

. Arg (Format ));

If (! Filename. isempty ())

Pixmap. Save (filename, format. toascii ());

}

 

 

 

 

Screenshot. cpp

# Include "screenshot. H "<br/> # include" ui_screenshot.h "</P> <p> screenshot: screenshot (qwidget * parent): <br/> qmainwindow (parent ), <br/> UI (new UI: screenshot) <br/>{< br/> UI-> setupui (this); <br/> qobject :: connect (ui-> shotscreenbutton, signal (clicked (), this, slot (newshotscreenslot (); <br/> qobject: connect (ui-> savebutton, signal (clicked (), this, slot (savescreenslot (); </P> <p >}</P> <p> screenshot ::~ Screenshot () <br/>{< br/> Delete UI; <br/>}</P> <p> void screenshot: newshotscreenslot () <br/>{< br/> If (ui-> hidecheckbox-> ischecked () <br/>{< br/> This-> hide (); <br/> This-> timer = new qtimer; </P> <p> qobject: connect (this-> timer, signal (timeout (), this, slot (shotscreenslot (); <br/> This-> timer-> Start (ui-> timespinbox-> value () * 1000 ); </P> <p >}< br/> else <br/>{< br/> qapp-> BEEP (); <br/>}</P> <P> void screenshot: shotscreenslot () <br/>{< br/> This-> pixmap = qpixmap: grabwindow (qapplication: desktop () -> winid (); <br/> UI-> screenlabel-> setpixmap (pixmap. scaled (ui-> screenlabel-> size (); <br/> This-> timer-> stop (); <br/> This-> show (); <br/>}< br/> # include <qfiledialog> <br/> # include <qdir> <br/> # include <qmessagebox> </P> <p> void screenshot:: savescreenslot () <br/>{< br/> qstring filename = qfiledi Alog: getsavefilename (this, "Screen Save", qdir: currentpath (); <br/> If (! Filename. isempty () <br/>{< br/> pixmap. save (filename); <br/>}< br/> else <br/>{< br/> qmessagebox: Information (this, "error message ", "select a name"); <br/> return; <br/>}</P> <p >}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.