7-3 response when the system is busy (staying responsive during intensive processing)

Source: Internet
Author: User
Tags rowcount

When we call qapplication: exec (), QT starts the event loop. At startup, QT issues a display and draw event to display the control. Then, the event loop starts, constantly checks whether an event occurs, and then assigns the event to the qobject object in the program.

When an event is being processed, other events have been generated and added to the event queue of QT. If we spend a lot of time processing an event, during this period, the user interface will not have any response. For example, when a program saves a file, the events generated by the window will not be processed and can only be processed after the file is saved. During the saving process, the application will not process the drawing events of the window. The solution to this problem is multithreading: one thread processes the user interface, and the other thread stores files or other time-consuming operations. In this way, the user interface of the program will remain responsive during file storage. This method is described in Chapter 1. Another simple method is to call qapplication: processevents () multiple times while saving the file (). During the call, QT will process the paused event and return to continue saving the file. In fact, qapplication: exec () is also a while loop that calls processevents. In the following example, spreadsheet uses processevents () to respond to the User Interface: bool Spreadsheet: writefile (const qstring & filename) {qfile file (filename );... for (int row = 0; row <rowcount; ++ row) {for (INT column = 0; column <columncount; ++ column) {qstring STR = formula (row, column); If (! Str. isempty () out <quint16 (ROW) <quint16 (column) <STR;} qapp-> processevents ();} return true ;} however, there is a danger in doing so. If you close the main window while saving the file, or click the file | save menu again, it will easily lead to an endless loop. The solution is to replace the code qapp-> processevents () with qapp-> processevents (qeventloop: excludeuserinputevents). In this way, QT will not process keyboard and mouse events. Qprogressdialog is often used when an application is performing operations for a long time, prompting you about the completion of ongoing operations. Qprogressdialog also provides a Cancel button that allows you to cancel the current operation. The following code uses the qprogressdialog code when spreadsheet saves the file: bool Spreadsheet: writefile (const qstring & filename) {qfile file (filename );... qprogressdialog progress (this); Progress. setlabeltext (TR ("Saving % 1 "). arg (filename); Progress. setrange (0, rowcount); Progress. setmodal (true); For (int row = 0; row <rowcount; ++ row) {progress. setvalue (ROW); qapp-> processevents (); If (Progress. wascanceled () {file. remove (); Return false;} For (INT column = 0; column <columncount; ++ column) {qstring STR = formula (row, column); If (! Str. isempty () out <quint16 (ROW) <quint16 (column) <STR ;}return true;} First, create a qprogressdialog and set numrows as the total number of steps. After saving a row, call setvalue () to update the progress bar status. Qprogressdialog automatically calculates the percentage of completion based on the number of current steps and the total number of steps. Call qapplication: processevents () to process possible draw events. You can click events or Keyboard Events. If you click the cancel button, cancel the save operation and delete the files being saved. We didn't call the show () function of qprogressdialog because qprogressdialog will process it by ourselves. If the file to be saved is small and takes a short time, qprogressdialog can detect this situation without displaying a progress bar. In addition to multithreading and qprogressdialog, there is also a completely different way to handle this time-consuming operation: this type of operation is performed when the program is idle, rather than waiting for the user's request to do it. However, the idle time of the program cannot be estimated. The condition of this method is that the operation can be safely aborted and continued. The specific implementation is to start a 0 Ms timer. This event is triggered as long as there are no other events to be processed in the program. The following timeevent () function is the implementation of this method: void Spreadsheet: timerevent (qtimerevent * event) {If (Event-> timerid () = mytimerid) {While (Step <maxstep &&! Qapp-> haspendingevents () {extends mstep (STEP); ++ step ;}} else {qtablewidget: timerevent (event) ;}} if haspendingevents () returns true, pause the operation and run the QT control program. When QT does not have any event to process, the operation continues.

 

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.