接受停靠图标上的文件下拉

时间:2015-10-30 18:17:02

标签: c++ qt

我正在为我的MacOs(10.11)应用程序使用Qt C ++,但我无法实现它接受文件提示。

这是Info.plist文件:

<?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.1">
<dict>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
    <key>CFBundleIconFile</key>
    <string></string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleGetInfoString</key>
    <string>Created by Qt/QMake</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleExecutable</key>
    <string>QParser</string>
    <key>CFBundleIdentifier</key>
    <string>com.yourcompany.QParser</string>
    <key>NOTE</key>
    <string>This file was generated by Qt/QMake.</string>
        <key>CFBundleDocumentTypes</key>
            <array>
                    <dict>
                            <key>CFBundleTypeExtensions</key>
                            <array>
                                <string>csv</string>
                                <string>public.comma-separated-values-text</string>
                                <string>comma-separated-values-text</string>
                                <string>txt</string>
                                <string>text</string>
                            </array>
                            <key>CFBundleTypeRole</key>
                            <string>Editor</string>
                    </dict>
            </array>
  <key>NSPrincipalClass</key>
  <string>NSApplication</string>

</dict>
</plist>

在我的主窗口中,我安装了eventFilter,但事件未被触发(它确实进入了事件过滤器,但没有进入事件QEvent::FileOpen)。

我已经检查了这些链接,但没有帮助:

这是我的MainWindows.cpp

#include <QMessageBox>
#include <QProcess>
#include <QFileOpenEvent>

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setAcceptDrops(true);
    this->installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_goButton_clicked()
{
    checkFile();
}

bool MainWindow::eventFilter(QObject * sender, QEvent *event)
{
    switch (event->type()) {
    case QEvent::FileOpen:
    {
        event->accept();

        qDebug() << "Event File Open";
        ui->lineEdit->setText(static_cast<QFileOpenEvent*>(event)->file());
        checkFile();

        break;
    }
    case QEvent::DragEnter:
    {
        event->accept();
        qDebug() << "Event DragEnter";
        break;
    }
    case QEvent::Drop:
    {
        event->accept();

        const QMimeData* mimeData = static_cast<QDropEvent *>(event)->mimeData();
        qDebug() << "Event Drop";
        // If there is one file (not more) we open it
        if (mimeData->urls().length() == 1) {
            QString fileName = mimeData->urls().first().toLocalFile();
            qDebug() << fileName;
        }
        break;
    }
    default:
        return false;
    }
    return true;
}
void MainWindow::checkFile()
{
    // Some stuff
}

我应该在哪里挖掘以使其有效?

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试处理QEvent::FileOpen上的QMainWindow事件,但该事件已发送到QApplication::instance(),因此您可以在此处安装事件过滤器。

来自Qt documentation

  

文件打开事件将在发送时发送到QApplication :: instance()   操作系统请求打开文件或URL。