简体   繁体   中英

Set priority to GUI thread in Qt

Is it possible to set priority to the main GUI thread so it has higher priority comparing to the other threads (QThread)?

My aim is to not to freeze up the GUI while the other threads are doing some intensive computation which may occupy CPU to 100% load. It would be great if someone can share a way to make sure GUI will not freeze during this period while the other computation threads can still try to maximize the CPU usage.

I thought about managing other threads, so I don't start too many computation threads at the same time.

Change the priority of the current thread when the current thread is the gui thread:

int main(int argc, char ** argv) {
  QApplication app(argc, argv);
  QThread::currentThread()->setPriority(QThread::HighPriority);
  ...
}

You can submit the change from any other thread, too – as long as the main (GUI) thread has a running event loop:

QMetaObject::invokeMethod(qApp, []{
  QThread::currentThread()->setPriority(QThread::HighPriority);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM