[QT 강좌] applicationStateChanged()

Posted by [하늘이]
2016. 11. 28. 16:23 IT/QT
반응형


QT 창을 실행 시킨 이후 Forground or Inactive 등 상태 체크를 위하여 signal / slot 을 등록해주고 사용할 수 있내요.


SIGNAL(applicationStateChanged(Qt::ApplicationState))에 등록 시 아래와 같은 상태 값을 받을 수 있습니다.

ConstantValueDescription
Qt::ApplicationSuspended0x00000000The application is about to suspend. When entering this state, the application should save its state, cease all activities, and be prepared for code execution to stop. While suspended, the application can be killed at any time without further warnings (e.g. when low memory forces the OS to purge suspended applications).
Qt::ApplicationHidden0x00000001The application is hidden and runs in the background. This is the normal state for applications that need to do background processing, like playing music, while the user interacts with other applications. The application should free up all graphical resources when entering this state.
Qt::ApplicationInactive0x00000002The application is visible, but not selected to be in front. On desktop platforms, this typically means that the user activated another application. On mobile platforms, it is more common to enter this state when the OS is interrupting the user with e.g. incoming calls or SMS-messages. While in this state, consider reducing CPU-intensive tasks.
Qt::ApplicationActive0x00000004The application is visible and selected to be in front.



Sample --------------간단한 화면을 하나 출력 시키기 하여....만들어봤습니다.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
#include "textfinder.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TextFinder w;
    w.show();
 
    return a.exec();
}
 
cs

TextFinder.h

아래 파일의 중요한 점은 private slots : 에 slot에 등록할 함수를 넣는 것입니다.

    void onApplicationStateChange(Qt::ApplicationState state);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef TEXTFINDER_H
#define TEXTFINDER_H
 
#include <QWidget>
#include <QApplication>
#include <QAction>
 
 
namespace Ui {
class TextFinder;
}
 
class TextFinder : public QWidget
{
    Q_OBJECT
 
public:
    explicit TextFinder(QWidget *parent = 0);
    ~TextFinder();
 
 
 
private slots:
    void on_findButton_clicked();
    void onApplicationStateChange(Qt::ApplicationState state);
 
private:
    Ui::TextFinder *ui;
    void loadTextFile();
 
};
 
#endif // TEXTFINDER_H
cs

TextFinder.cpp

TextFinder 가 생성될 때 connect 로 signal과 slot 을 설정해주면 됩니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
TextFinder::TextFinder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TextFinder)
{
    ui->setupUi(this);
 
    loadTextFile();
 
    connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(onApplicationStateChange(Qt::ApplicationState)));
 
}
 
void TextFinder::onApplicationStateChange(Qt::ApplicationState state){
    switch (state) {
        case Qt::ApplicationSuspended:
            qDebug() << "onApplicationStateChange ApplicationSuspended:" << state;
            break;
 
        case Qt::ApplicationHidden:
            qDebug() << "onApplicationStateChange ApplicationHidden:" << state;
            break;
 
        case Qt::ApplicationInactive:
            qDebug() << "onApplicationStateChange ApplicationInactive:" << state;
            break;
 
        case Qt::ApplicationActive:
            qDebug() << "onApplicationStateChange ApplicationActive:" << state;
            break;
 
        default:
            qDebug() << "onApplicationStateChange state:" << state;
            break;
    }
}


cs


Output : 

Debug$ ./Example_Widget_TextFinder 

onApplicationStateChange ApplicationActive: 4 

onApplicationStateChange ApplicationInactive: 2 

onApplicationStateChange ApplicationActive: 4 

onApplicationStateChange ApplicationInactive: 2 

onApplicationStateChange ApplicationActive: 4 

onApplicationStateChange ApplicationInactive: 2 

onApplicationStateChange ApplicationActive: 4 

onApplicationStateChange ApplicationInactive: 2 

onApplicationStateChange ApplicationActive: 4 

onApplicationStateChange ApplicationInactive: 2 

onApplicationStateChange ApplicationActive: 4 

onApplicationStateChange ApplicationInactive: 2 

onApplicationStateChange ApplicationActive: 4 




Refer : http://doc.qt.io/qt-5/qguiapplication.html






반응형