Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to display pages while loading data by Qt

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge of this article "Qt how to load data while displaying the page", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Qt how to load data while displaying the page" article.

1. Define display timer

To load data as soon as the page is opened, we need to rewrite QWidget::show (), start the timer, and execute it immediately.

1: define a timer / / .h # include QTimer * masking timer; void QMyWidget::show. CPP uses m_Timer = new QTimer (this); connect (m_Timer, & QTimer::timeout, this, & QMyWidget::OnTimerLoadData); 2: timer calls void QMyWidget::show () {timer (); masking timer-> start (0);}

The timer operation needs to be performed immediately to open the page, and the parameter = 0 in start means immediate execution.

At this point, the display page has been loaded.

Because as mentioned earlier, the page has a large amount of data, it is impossible to display the page in a fake death state, so we need to load the page while displaying a gif waiting icon.

Here, we need to modify the function of show ()

After the void QMyWidget::show () {QWidget::show (); / / page starts, it directly displays the loaded gif image gPageManager::instance ()-> GetDownloadDlg ()-> SetShowMode (1); gPageManager::instance ()-> GetDownloadDlg ()-> SetTips ("loading case data, please wait a moment"); gPageManager::instance ()-> GetDownloadDlg ()-> show () If (masked timer-> isActive () = = false) {masked timer-> start (0);}}

Here, I use a singleton class: gPageManager calls a window with a gif effect.

This way can be achieved, after displaying the page, directly wait for the data to load, to prevent us from seeing the fake dead page, causing trouble to the user.

The gif picture here is displayed with a QLabel bearer, there are many ways, but not too much introduction.

A reminder here is that when using a timer in QT, it is safer to determine whether the timer is active and needs to be triggered only if it is no longer active. Here is only a warm reminder, personal code habits.

3: timer loads data

After entering the timer, data processing is carried out. In order to prevent the page from stuttering, at this time, we also need to restart a thread in the timer for data loading.

At this point, someone will want to ask, the current page has already started a timer, why create another thread?

I will answer them one by one.

In a function in C language, when you run the contents of a specified function, the running page is displayed only when you run to "}". In a specific processing function, the computer belongs to a process processing function when processing.

Therefore, the timer operation will be started as soon as the page is displayed, first showing the page to the user, doing other data processing.

So why start another thread in the timer?

Mainly because a dynamically loaded window is called in the show function, assuming that more data is directly loaded in the timer, the interface will also be in a stuttered state, causing the GIF waiting window to be stuck. To prevent this from happening, we need to continue to open a thread in the timer to prevent the page from stuttering.

Void QMyWidget::OnTimerLoadData () {/ / because the data is only loaded when the page is opened, the timer only needs to be done once. Masking timer-> stop (); / / start the thread and load the data. The specific code is not specified here. / / after the data is loaded, hide the GIF dynamic loading page gPageManager::instance ()-> GetDownloadDlg ()-> hide ();}

At this point, the page opens to show that the loading function is complete, so how do you implement the current thread?

Next, it's our second stage.

two。 Thread loading data

Generally speaking, C++ programmers naturally want to use threading when they encounter this kind of situation.

In fact, my first idea is to use threads to load data. However, the use of threads must take into account the drawbacks of threads, such as deadlocks, such as wild pointers.

There is a way to open threads in QT, which is simple and easy to use. Here I recommend it: QtConcurrent::run

The specific explanation of the function is not explained here, let's use it directly!

The first required header file:

# include

Next is the call mode, where we define the name of the function that loads the data called LoadWidgetData ().

QFuture futureResult = QtConcurrent::run (this, & QMyWidget::LoadWidgetData); while (! futureResult.isFinished ()) {QApplication::processEvents (QEventLoop::AllEvents);}

When using this threading method, it is important to note that the return value of the LoadWidgetData function must be true.

Bool QMyWidget::LoadWidgetData () {/ / specific data loading operation return true;}

The loading method of the thread has been introduced, and at this point, we can load the data once and display the waiting GIF effect again.

Next, it's time to implement how to present the loading progress in real time.

3. Real-time rendering of loading progress

As we all know, page operation content cannot be called in the thread of QT.

In general, page operations, such as window creation, control assignment, and so on, need to be carried out in the main thread, otherwise it will cause crashes. For specific reasons, you can refer to the information.

So, what should we do when we load the data and display it on the page?

Here, we can send a message to the main process in the thread and give it to the main process to handle the page operation

Bool QMyWidget::LoadWidgetData () {/ / 1: load data content 1, the specific implementation does not describe / / send data content 1 corresponding to the page processing operation emit Msg_SendSelfDataProcessing1 (); / /. The content of data loading is free to play, similar to the content of the above two steps return true;}

The code looks easy to understand, which ensures that the data loads smoothly without stuttering the main page.

The above is about "Qt how to load data while displaying the page" of this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report