QML在收到错误之前检查文件/图像是否存在 [英] QML Check if file/image exists before getting the error
问题描述
我想检查本地目录中是否存在图像,如果不存在,请加载默认图像.我通过使用以下方法找到了锻炼方式:
I want to check if an image exists in the local directory, and if not, load a default one. I found a workout by using:
Image {
id: image
source: source1
onStatusChanged: {
if ( (image.status == Image.Error) && source !== default_source ) {
source = default_source
}
}
}
但是我更喜欢在得到错误之前测试文件是否存在.尝试加载文件之前,有没有更干净的方法来测试file_paths/url/images?
But I would prefer to test if the file exists before getting the error. Is there any cleaner way to test file_paths/url/images before trying to load them?
谢谢!
推荐答案
目前尚无办法单独使用QML做到这一点.
There's currently no way to do this with QML alone.
我写了一个C ++类来为我的一个项目做到这一点:
I wrote a C++ class to do this for one of my projects:
https://github.com/mitchcurtis/slate/blob/master/lib/filevalidator.h
/*
Copyright 2016, Mitch Curtis
This file is part of Slate.
Slate is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Slate is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Slate. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILEVALIDATOR_H
#define FILEVALIDATOR_H
#include <QObject>
#include <QUrl>
class FileValidator : public QObject
{
Q_OBJECT
Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
Q_PROPERTY(bool fileValid READ isFileValid NOTIFY fileValidChanged)
Q_PROPERTY(QString fileErrorMessage READ fileErrorMessage WRITE setFileErrorMessage NOTIFY fileErrorMessageChanged)
Q_PROPERTY(bool treatAsImage READ treatAsImage WRITE setTreatAsImage NOTIFY treatAsImageChanged)
public:
explicit FileValidator(QObject *parent = 0);
QUrl url() const;
void setUrl(const QUrl &url);
bool isFileValid() const;
QString fileErrorMessage() const;
void setFileErrorMessage(const QString &fileErrorMessage);
bool treatAsImage() const;
void setTreatAsImage(bool treatAsImage);
signals:
void urlChanged();
void fileValidChanged();
void fileErrorMessageChanged();
void treatAsImageChanged();
protected:
virtual void validate();
QUrl mUrl;
QString mFileErrorMessage;
bool mTreatAsImage;
};
#endif // FILEVALIDATOR_H
https://github.com/mitchcurtis/slate/blob/master/lib/filevalidator.cpp
/*
Copyright 2016, Mitch Curtis
This file is part of Slate.
Slate is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Slate is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Slate. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filevalidator.h"
#include <QFile>
#include <QImage>
FileValidator::FileValidator(QObject *parent) :
QObject(parent),
mTreatAsImage(false)
{
setFileErrorMessage("Must specify a file");
}
QUrl FileValidator::url() const
{
return mUrl;
}
void FileValidator::setUrl(const QUrl &url)
{
if (url == mUrl)
return;
mUrl = url;
if (mUrl.isEmpty()) {
setFileErrorMessage(tr("Must specify a file"));
} else if (!QFile::exists(mUrl.toLocalFile())) {
setFileErrorMessage(tr("File doesn't exist"));
} else {
if (mTreatAsImage) {
QImage image(mUrl.toLocalFile());
if (image.isNull()) {
setFileErrorMessage(tr("Image can not be opened"));
} else {
// The image was loaded successfully, so we can clear
// whatever was here before.
setFileErrorMessage(QString());
}
} else {
// The file was loaded successfully.
setFileErrorMessage(QString());
}
}
if (mFileErrorMessage.isEmpty()) {
// Let derived classes check for problems.
validate();
}
emit urlChanged();
}
bool FileValidator::isFileValid() const
{
return mFileErrorMessage.isEmpty();
}
QString FileValidator::fileErrorMessage() const
{
return mFileErrorMessage;
}
void FileValidator::setFileErrorMessage(const QString &fileErrorMessage)
{
if (fileErrorMessage == mFileErrorMessage)
return;
bool wasValid = isFileValid();
mFileErrorMessage = fileErrorMessage;
if (isFileValid() != wasValid) {
emit fileValidChanged();
}
emit fileErrorMessageChanged();
}
bool FileValidator::treatAsImage() const
{
return mTreatAsImage;
}
void FileValidator::setTreatAsImage(bool treatAsImage)
{
if (treatAsImage == mTreatAsImage)
return;
mTreatAsImage = treatAsImage;
emit treatAsImageChanged();
}
void FileValidator::validate()
{
}
然后您将其注册到QML:
You'd then register it with QML:
qmlRegisterType<FileValidator>("App", 1, 0, "FileValidator");
并像这样使用它:
import QtQuick 2.9
import QtQuick.Controls 2.0
import App 1.0
ApplicationWindow {
width: 400
height: 400
visible: true
FileValidator {
id: validator
url: source1
treatAsImage: true
}
Image {
source: validator.fileValid ? source1 : default_source
}
}
这篇关于QML在收到错误之前检查文件/图像是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!