How to implement Qt screenshot control
This article mainly explains "how to implement the Qt screenshot control". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the Qt screenshot control".
I. Preface
Screen capture controls are useful in many of my projects, especially the software on embedded systems, because in embedded systems, the system is basically very concise, not even UI, after boot is directly run is the Qt program, often need to save screenshots of the software, used to write documents and introductions, and so on, after all, the effect of running directly on the board is the best. There is another way is to compile the system into the win version, with screenshots of the system, but embedded on a lot of code is actually very inconvenient to run on win, or even can not run, but also a lot of external interfaces to get the real running effect, so or directly in the board of Qt procedures directly integrated screenshot function, when needed directly right mouse button pop-up can be selected.
2. ScreenWidget::ScreenWidget (QWidget * parent): QWidget (parent) {/ / this- > setWindowFlags (Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); menu = new QMenu (this); menu- > addAction ("Save current Screenshot", this, SLOT (saveScreen ()); menu- > addAction ("Save full screen Screenshot", this, SLOT (saveFullScreen () Menu- > addAction ("Screenshot Save as", this, SLOT (saveScreenOther (); menu- > addAction ("full screen Save as", this, SLOT (saveFullOther (); menu- > addAction ("exit Screenshot", this, SLOT (hide (); / / get screen size screen = new Screen (QApplication::desktop ()-> size ()); / / Save full screen image fullScreen = new QPixmap () } void ScreenWidget::paintEvent (QPaintEvent *) {int x = screen- > getLeftUp (). X (); int y = screen- > getLeftUp (). Y (); int w = screen- > getRightDown (). X ()-x; int h = screen- > getRightDown (). Y ()-y; QPainter painter (this); QPen pen; pen.setColor (Qt::green); pen.setWidth (2); pen.setStyle (Qt::DotLine); painter.setPen (pen) Painter.drawPixmap (0,0, * bgScreen); if (w! = 0 & & h! = 0) {painter.drawPixmap (x, y, fullScreen- > copy (x, y, w, h));} painter.drawRect (x, y, w, h); pen.setColor (Qt::yellow); painter.setPen (pen) Painter.drawText (x + 2, y-8, tr ("Screenshot range: (% 1 x 2)-(3 x 4) Picture size: (5 x 6)") .Arg (x) .arg (y) .Arg (x + w) .arg (y + h) .arg (w) .arg (h);} void ScreenWidget::showEvent (QShowEvent *) {QPoint point (- 1,-1) Screen- > setStart (point); screen- > setEnd (point); # if (QT_VERSION grabWindow (QApplication::desktop ()-> winId (), 0,0, screen- > width (), screen- > height ()); # else QScreen * pscreen = QApplication::primaryScreen (); * fullScreen = pscreen- > grabWindow (QApplication::desktop ()-> winId (), 0,0, screen- > width (), screen- > height ()) # endif / / set transparency to blur background QPixmap pix (screen- > width (), screen- > height ()); pix.fill ((QColor (160,160,160,200)); bgScreen = new QPixmap (* fullScreen); QPainter p (bgScreen); p.drawPixmap (0,0, pix);} III.
Thank you for your reading, the above is the content of "how to achieve Qt screenshot control". After the study of this article, I believe you have a deeper understanding of how to achieve this problem of Qt screenshot control, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!