I have recently developed a screenshot program for Qt. I have referred to several examples on the Internet. However, I have added a lot of content that the author has to do for those examples. Others seem to be struggling. I have sorted it out, actually, a few lines of code really work, so I think it is necessary to share it.
Qpixmap fullscreen; int screenwidget = 640; // set the screen size. Write another function to modify the size. Here int screenhight = 480; // This step captures full screen fullscreen = qpixmap: grabwindow (qapplication: desktop ()-> winid (), screenwidget, screenhight ); // In this step, the pop-up box is set to ask about the storage location and name. Modify the location and name by yourself. // qfiledialog: Options options; // qstring selectedfilter; // qstring filename = qfiledialog: getsavefilename (this, // tr ("Save picture"), // "savescreen.jpg", // & selectedfilter, // options ); // set the area. The area is to copy the corresponding area from the screen. Write the function here. // int x = 0; // int y = 0; // int W = 300; // int H = 300; /**************************** the qpixmap can be properly saved in the following steps, you can skip qiameg *************************** // fullscreen = fullscreen. copy (X, Y, W, H); // change the screenshot area here. // fullscreen. save (filename, "jpg") requires the Save dialog box to write // If (fullscreen. save ("/savescreen.jpg", "jpg") // qdebug ("---------- save screen success ----------"); /************** the following steps are for embedded systems, the number of digits of the image is limited, to 8-bit images, on the PC end, you do not need to **************/qimage image = fullscreen. toimage (); // image = image. copy (X, Y, W, H); // change the screenshot area here. // convert it to 8-bit image = image. converttoformat (qimage: format_indexed8, QT: autocolor); // image. save (filename, "jpg") needs to write if (image. save ("/savescreen.jpg", "jpg") qdebug ("---------- save screen success ----------");
The above code can be run as long as it is written in an empty project. I am running in an embedded Linux environment, so there is a limit on the number of digits of the image. I need to turn it over to qimage to reduce the number of digits, I saved the image directly in a fixed path with a fixed name and ran a screenshot. This is the minimum and most concise one.
Then I made comments on all the areas to be extended. For example, if I want to write the interface to set the region, where can I change it? I want to write the Save path myself, I also marked out where to change it. If it runs on the PC end, you can directly save qpixmap.
Only hope to help others