QT / Necessitas - 合理QFileDialog更换/皮肤? [英] Qt/Necessitas - reasonable QFileDialog replacement/skin?

查看:439
本文介绍了QT / Necessitas - 合理QFileDialog更换/皮肤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在找一个很好的方式来解决Qt应用程序移植到QT / Necessitas(安卓)。

I'm looking for a nice way to address porting Qt applications to Qt/Necessitas (Android).

有些QtGUI部件是绝对残暴 - 不幸的是,其中包括QFileDialog。

Some of the QtGUI widgets are absolutely atrocious - unfortunately, including QFileDialog.

你知道任何替代与适当的外观和感觉? 正在QFileDialog可用的高优先级附近的任何地方为Necessitas开发商?

Do you know of any replacements with a proper look and feel? Is making QFileDialog usable anywhere near high priority for Necessitas developers?

#include <QApplication>

#include <QFileDialog>

int main(int argc, char* argv[]) {
    QApplication a(argc, argv);

    QString fileName = QFileDialog::getOpenFileName(NULL,
      QObject::tr("Open Image"), "/home/jana", QObject::tr("Image Files (*.png *.jpg *.bmp)"));

    a.exec();
}

推荐答案

Android不具备自己的,本地文件对话框。我们可以用QtAndroidExtras来调用外部应用程序,它能够选择一个文件:

Android does not have own, native file dialog. We can use QtAndroidExtras to invoke external application which is able to pick a file:

我写包装器,其可用于这一点。这里是全code:

I wrote wrapper, that could be used for that. Here's full code:

androidfiledialog.h

androidfiledialog.h

#ifndef ANDROIDFILEDIALOG_H
#define ANDROIDFILEDIALOG_H

#include <QObject>
#include <QAndroidJniObject>
#include <QtAndroid>
#include <QAndroidActivityResultReceiver>

class AndroidFileDialog : public QObject
{
    Q_OBJECT

public:
    explicit AndroidFileDialog(QObject *parent = 0);
    virtual ~AndroidFileDialog();
    bool provideExistingFileName();

private:
    class ResultReceiver : public QAndroidActivityResultReceiver {
        AndroidFileDialog *_dialog;
    public:
        ResultReceiver(AndroidFileDialog *dialog);
        virtual ~ResultReceiver();
        void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data);
        QString uriToPath(QAndroidJniObject uri);
    };

    static const int EXISTING_FILE_NAME_REQUEST = 1;
    ResultReceiver *receiver;
    void emitExistingFileNameReady(QString result);

signals:
    void existingFileNameReady(QString result);
};

#endif // ANDROIDFILEDIALOG_H

androidfiledialog.cpp

androidfiledialog.cpp

#include "androidfiledialog.h"

AndroidFileDialog::ResultReceiver::ResultReceiver(AndroidFileDialog *dialog) : _dialog(dialog) {}
AndroidFileDialog::ResultReceiver::~ResultReceiver() {}

void AndroidFileDialog::ResultReceiver::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
{
    jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
    if (receiverRequestCode == EXISTING_FILE_NAME_REQUEST && resultCode == RESULT_OK) {
        QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
        QString path = uriToPath(uri);
        _dialog->emitExistingFileNameReady(path);
    } else {
        _dialog->emitExistingFileNameReady(QString());
    }
}

QString AndroidFileDialog::ResultReceiver::uriToPath(QAndroidJniObject uri)
{
    if (uri.toString().startsWith("file:", Qt::CaseInsensitive)) {
        return uri.callObjectMethod("getPath", "()Ljava/lang/String;").toString();
    } else {
        QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
        QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), 0, 0, 0, 0);
        QAndroidJniObject DATA = QAndroidJniObject::fromString("_data");
        jint columnIndex = cursor.callMethod<jint>("getColumnIndexOrThrow", "(Ljava/lang/String;)I", DATA.object<jstring>());
        cursor.callMethod<jboolean>("moveToFirst", "()Z");
        QAndroidJniObject result = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
        return result.isValid() ? result.toString() : QString();
    }
}

AndroidFileDialog::AndroidFileDialog(QObject *parent) : QObject(parent)
{
    receiver = new ResultReceiver(this);
}

AndroidFileDialog::~AndroidFileDialog()
{
    delete receiver;
}

bool AndroidFileDialog::provideExistingFileName()
{
    QAndroidJniObject ACTION_GET_CONTENT = QAndroidJniObject::fromString("android.intent.action.GET_CONTENT");
    QAndroidJniObject intent("android/content/Intent");
    if (ACTION_GET_CONTENT.isValid() && intent.isValid()) {
        intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_GET_CONTENT.object<jstring>());
        intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("file/*").object<jstring>());
        QtAndroid::startActivity(intent.object<jobject>(), EXISTING_FILE_NAME_REQUEST, receiver);
        return true;
    } else {
        return false;
    }
}

void AndroidFileDialog::emitExistingFileNameReady(QString result)
{
    emit existingFileNameReady(result);
}

您已经添加到您的* .pro文件:

You have to add to your *.pro file:

QT += androidextras

使用例如:

AndroidFileDialog *fileDialog = new AndroidFileDialog();
connect(fileDialog, SIGNAL(existingFileNameReady(QString)), this, SLOT(openFileNameReady(QString)));
bool success = fileDialog->provideExistingFileName();
if (!success) {
    qDebug() << "Problem with JNI or sth like that...";
    disconnect(fileDialog, SIGNAL(existingFileNameReady(QString)), this, SLOT(openFileNameReady(QString)));
    //or just delete fileDialog instead of disconnect
}

插槽实现:

void MyClass::openFileNameReady(QString fileName)
{
    if (!fileName.isNull()) {
        qDebug() << "FileName: " << fileName;
    } else {
        qDebug() << "User did not choose file";
    }
}

请确认这个解决方案正常工作,您的设备上。

这篇关于QT / Necessitas - 合理QFileDialog更换/皮肤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆