QFileOpenEvent Class

QFileOpenEvent 클래스는 파일 또는 URL을 열라는 요청이 있을 때 전송되는 이벤트를 제공합니다. 더 보기...

헤더: #include <QFileOpenEvent>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
상속합니다: QEvent

공용 함수

QString file() const
QUrl url() const

상세 설명

파일 열기 이벤트는 운영 체제에서 파일 또는 URL을 열어야 한다고 요청할 때 QApplication::instance()로 전송됩니다. 이 이벤트는 사용자의 데스크톱 환경에 따라 다양한 사용자 작업(예: macOS의 Finder에서 파일 아이콘을 두 번 클릭)으로 인해 발생할 수 있는 상위 수준의 이벤트입니다.

이 이벤트는 애플리케이션에 요청을 알리는 데만 사용됩니다. 안전하게 무시해도 됩니다.

참고: 이 클래스는 현재 macOS에서만 지원됩니다.

macOS 예제

macOS에서 이벤트를 트리거하려면 애플리케이션이 어떤 종류의 파일에 반응해야 하는지 OS에 알려주도록 애플리케이션을 구성해야 합니다.

예를 들어 다음 Info.plist 파일은 애플리케이션이 확장자가 PNG인 파일에 대한 뷰어 역할을 할 수 있다고 선언합니다:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>png</string>
            </array>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
        </dict>
    </array>
</dict>
</plist>

다음 QApplication 서브클래스의 구현은 예를 들어 애플리케이션의 Dock 아이콘에 드래그한 파일을 열기 위해 QFileOpenEvent를 처리하는 방법을 보여줍니다.

#include <QApplication>
#include <QDebug>
#include <QFileOpenEvent>
#include <QPushButton>

class MyApplication : public QApplication
{
public:
    MyApplication(int &argc, char **argv)
        : QApplication(argc, argv)
    {
    }

    bool event(QEvent *event) override
    {
        if (event->type() == QEvent::FileOpen) {
            QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(event);
            const QUrl url = openEvent->url();
            if (url.isLocalFile()) {
                QFile localFile(url.toLocalFile());
                // read from local file
            } else if (url.isValid()) {
                // process according to the URL's schema
            } else {
                // parse openEvent->file()
            }
        }

        return QApplication::event(event);
    }
};

QFileOpenEvent::file()QFile 을 사용하여 열 수 있는 로컬 파일의 이름이라고 보장할 수 없다는 점에 유의하세요. 문자열의 내용은 소스 애플리케이션에 따라 다릅니다.

멤버 함수 문서

QString QFileOpenEvent::file() const

애플리케이션이 열어야 하는 파일의 이름을 반환합니다.

로컬 파일의 경로일 필요는 없습니다.

QUrl QFileOpenEvent::url() const

애플리케이션이 열어야 하는 URL을 반환합니다.

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.