QIconProvider's icon method is called for files that are not in the filter - qt

I am trying to populate a QListWidget (in icon view) with a QFileSystemModel. I want to list folders and only files with specific extension. I want to show the preview of my files as their thumbnail, so I am subclassing QIconProvider class and I am setting this to my model.
Before setting my QIconProvider to the model I have already filtered the files that I want on my model, icon(const QFileInfo & info) is called for every file that exists in the listed directory.
I have found a work around checking the extension of the file before returning my custom icon, but I am wondering if thre is a way to avoid this.
m_itemsModel = new QFileSystemModel(this);
m_itemsModel->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
m_itemsModel->setRootPath(QDir::homePath());
QStringList filters = formatsList();
m_itemsModel->setNameFilters(filters);
m_itemsModel->setNameFilterDisables(false);
ui.listView->setModel(m_itemsModel);
m_itemsModel->setIconProvider(new ThumbnailIconProvider(QRect(0, 0, 50, 50)));
my QIconProvider:
ThumbnailIconProvider::ThumbnailIconProvider(const QRect &rect)
: QFileIconProvider() {
m_rect = rect;
}
QIcon ThumbnailIconProvider::icon(const QFileInfo & info) const {
static QStringList filters = formatsList();
QString fileName = info.fileName();
QString extension = "*" + fileName.right(fileName.length() - fileName.lastIndexOf("."));
if (info.isFile() && filters.contains(extension)) {
QString path = info.absoluteFilePath();
FileDetails details = fileDetailsFromPathForRect(path, m_rect);
QPixmap pixmap = QPixmap::fromImage(details.image);
QIcon icon(pixmap);
return icon;
}
else
return QFileIconProvider::icon(info);
}
Any ideas please?

Related

How to link signal and slot when i change the font in the dialog

This is how i am currently choosing the font in my application.
void FontChange()
{
QString filePath = QFileDialog::getOpenFileName(NULL, tr("Open File"),
QStandardPaths::standardLocations(QStandardPaths::FontsLocation)[0],
tr("Fonts (*.ttf);;Everything (*.*)"), nullptr,
QFileDialog::DontUseNativeDialog);
if (filePath.isEmpty())
return;
QlineEditSetFont->setText(filePath);
stdstrLocation = filePath.toStdString();
this->isChanged = true; // this executes the function
}
I want to execute a function whenever i change my selection in the Dialog , currently i have to choose a font and than click on open to execute the function
////////////////////////////////////////////////////////////////////////////
Now i am using a non modal way but how can i determine if cancel has been pressed.
Further Edit
QFileDialog* dialog = new QFileDialog();
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setNameFilter("TTF (*.ttf)");
dialog->setOption(QFileDialog::DontUseNativeDialog);
dialog->setDirectory("C:\\Windows\\Fonts");
QObject::connect(dialog, &QFileDialog::currentChanged, [=](const QString &path) {
qDebug() << path; stdstrLocation = path.toStdString(); this->isChanged = true;
QStringList fileNames = dialog->selectedFiles();
qDebug() << "Selected FIles" << fileNames.size();
});
dialog->show();
You can't use the static convenience method but need to create the QFileDialog instance manually:
auto dialog = new QFileDialog(someParent);
dialog->setWindowTitle(tr("Open File"));
dialog->setDirectory(QStandardPaths::standardLocations(QStandardPaths::FontsLocation)[0]);
dialog->setNameFilter(tr("Fonts (*.ttf);;Everything (*.*)"));
// more setup...
connect(dialog, &QFileDialog::filesSelected, this, [this](const QStringList &selected) {
// handle selection change here
});
if (dialog->exec() == QDialog::Accepted) { // alternatively use open() to avoid blocking exec()
// do something with dialog->selectedFiles()...
}
delete dialog;
Actually, those are two different questions. The one from the title has been already answered. The answer to the second one, namely How can i determine if cancel has been pressed, lies in the documentation of QFileDialog::getOpenFileName itself:
If the user presses Cancel, it returns a null string.
With this in mind, you can do something like:
void FontChange()
{
QString filePath = QFileDialog::getOpenFileName(NULL, tr("Open File"),
QStandardPaths::standardLocations(QStandardPaths::FontsLocation)[0],
tr("Fonts (*.ttf);;Everything (*.*)"), nullptr,
QFileDialog::DontUseNativeDialog);
if (filePath.isNull()) {
// user pressed Cancel
} else if (filePath.isEmpty()) {
return;
} else {
QlineEditSetFont->setText(filePath);
stdstrLocation = filePath.toStdString();
this->isChanged = true; // this executes the function
}
}
You can of course rewrite the if part to be more suitable to your app's logic.
As a side note, QString::isEmpty also implies that it is NULL.

How to implement clipboard actions for custom mime-types?

I am trying to implement copy/cut/paste in a complex application.
I have a QGraphicsScene that can contain QGraphicsItem subtypes of varied subtypes, fairly complex (with Item as a second parent storing custom properties).
I would copy/cut selected items, and paste them back in place.
I already have implemented it using a local version: a list of items.
void copyItemsActionOld()
{
foreach(QGraphicsItem* qItem, selectedItems())
{
Item* newItem = (dynamic_cast<Item*>(qItem))->createItemCopy();
m_itemClipboard.append(newItem);
}
}
On paste, I make a copy of all items in clipboard and add them to the scene. So simple.....
BUT
I need to implement it using the global system clipboard.
I saw that creating a custom mime type is as simple as calling setData on a QMimeData object, after I make up a format name... (I hope that is true)
static const QString _mimeType("application/myItem");
void copyItemsAction()
{
QMimeData* _mimeData = new QMimeData;
2 QByteArray _itemData = ?????;
_mimeData->setData(_mimeType, _itemData);
QClipboard* _clipboard = QApplication::clipboard();
_clipboard->clear();
_clipboard->setMimeData(_mimeData);
}
void pasteItemsAction()
{
QClipboard* _clipboard = QApplication::clipboard();
const QMimeData* _mimeData = _clipboard->mimeData();
QStringList _formats = _mimeData->formats();
foreach (QString _format, _formats)
{
if (_format == _mimeType)
{
QByteArray _itemData = _mimeData->data(_mimeType);
3 // then do what ? How do I parse it ?
}
}
}
My questions
1) Are the above fragments for copyItemsAction and pasteItemsAction anywhere close to how clipboard actions should work ?
2) How can I put item data in the QByteArray ?
3) How do I parse the data in QByteArray ?
4) Do I need to register the custom mime-type anywhere else ? (other than what I just did in my two functions); and will it be multi-platform ?
I have already implemented save and load functionality for all items. Something like...
void Item::saveItem(QDataStream &outFile)
{
outFile << type;
outFile << width;
outFile << color.name();
}
Can I use this to place the items data in the QByteArray ? (How ?)
I was on the right track, and I kept adding code to my question until I found how to make it work:
static const QString _mimeType("application/myItem");
void copyItemsAction()
{
QByteArray _itemData;
QDataStream outData(&_itemData, QIODevice::WriteOnly);
outData << selectedItems().size();
foreach(QGraphicsItem* qItem, selectedItems())
{
Item* item = dynamic_cast<Item*>(qItem);
item->saveItem(outData);
}
QMimeData* _mimeData = new QMimeData;
_mimeData->setData(_mimeType, _itemData);
_mimeData->setText("My Items");
QClipboard* _clipboard = QApplication::clipboard();
_clipboard->clear();
_clipboard->setMimeData(_mimeData);
}
void pasteItemsAction()
{
QClipboard* _clipboard = QApplication::clipboard();
const QMimeData* _mimeData = _clipboard->mimeData();
QStringList _formats = _mimeData->formats();
foreach (QString _format, _formats)
{
if (_format == _mimeType)
{
QByteArray _itemData = _mimeData->data(_mimeType);
QDataStream inData(&_itemData, QIODevice::ReadOnly);
int itemsSize;
inData >> itemsSize;
for (int i = 0; i < itemsSize; ++i)
{
Item* item = ...
item->loadItem(inData);
}
}
}
}
So, for question 1, yes I was on the right track;
For questions 2 and 3 - I was able to use a QDataStream to serialize info to/from the QByteArray.
If there is a better / more effective / faster way, I would love to know...
For question 4 - it seems that I can use just about any string, if all I want is to copy/paste within a single instance of my application.
It is also true if I want to use it between multiple applications, multiple instances of my application, or for drag-and-drop - on most platforms. (It does not seem to work between multiple applications/instances in the embedded platform I target.)
Caveat - it fails frequently when another clipboard using application is open, in windows.

QLineEdit: automatically append backslash to directory name

I'm trying to automatically add a backslash to valid file paths in a QLineEdit, which is used to show the current path of a QFileSystemModel.
The code looks as follows:
fileSystem = new QFileSystemModel;
fileSystem->setRootPath(QObject::tr("C:\\"));
QCompleter* fileSystemCompleter = new QCompleter(fileSystem);
fileSystemCompleter->setCaseSensitivity(Qt::CaseInsensitive);
fileTree = new QDeselectableTreeView();
fileTree->setModel(fileSystem);
fileTree->setRootIndex(fileSystem->index(fileSystem->rootPath()));
connect(fileTree, &QTreeView::clicked, [&] (QModelIndex index)
{
QString toAppend("");
if (fileSystem->isDir(index))
{
toAppend = '/';
}
fileSystemPathEdit->setText(fileSystem->filePath(index)+toAppend);
});
// path line edit
fileSystemPathEdit = new QLineEdit(fileSystem->rootPath());
fileSystemPathEdit->setPlaceholderText("Path...");
fileSystemPathEdit->setCompleter(fileSystemCompleter);
connect(fileSystemPathEdit, &QLineEdit::editingFinished, [&]()
{
// jump to that location
qDebug() << fileSystemPathEdit->text();
QModelIndex index = fileSystem->index(fileSystemPathEdit->text());
qDebug() << index;
fileTree->setExpanded(index,true);
fileTree->setCurrentIndex(index);
// CLOSE IF EMPTY
if (fileSystemPathEdit->text().isEmpty())
{
fileTree->collapseAll();
fileSystemPathEdit->setText(fileSystem->rootPath());
}
// append slashes to dirs
else if (fileSystem->isDir(index) && index.isValid())
{
qDebug() << "it's a dir";
if (!fileSystemPathEdit->text().endsWith('/',Qt::CaseInsensitive))
{
qDebug() << "added slash";
fileSystemPathEdit->setText(fileSystemPathEdit->text().append('/'));
qDebug() << fileSystemPathEdit->text();
}
}
this->update();
});
I get the following output when running the code:
"C:/export/home"
QModelIndex(0,0,0x3adb840,QFileSystemModel(0x1d9b7c0) )
it's a dir
added slash
"C:/export/home/"
It works ok when I push the Enter key from within the lineEdit, but if the text is set by the QCompleter, I still get the same debug output showing that the text has been changed, but the slash doesn't appear in the lineEdit. Does the QCompleter somehow unset the text?
This is a hack, but adding this connection to the QCompleter gives the desired behavior. I think there is a race condition when using editingFinished() at the same time that the QCompleter is activated, so adding delay allows the slash to be appended without being overridden. On the down side, that function know gets called several times to many per change. I'd still be interested in a better solution.
connect(fileSystemCompleter, activatedOverloadPtr, [&](QModelIndex index)
{
QTimer* timer = new QTimer;
timer->setSingleShot(true);
timer->setInterval(10);
connect(timer, &QTimer::timeout, fileSystemPathEdit, &QLineEdit::editingFinished);
timer->start();
});

QVector: no match for 'operator+'

I am passing a QVector from one window to another, I want to append the value present in QVector from previous window to a QString in present window. I get the error when I perform the addition no match for 'operator+'.
Here is my code:
Window1.cpp
void SelectOS::processNextButton()
{
if(ui->win32->isChecked()){
QString path;
path = qApp->applicationDirPath()+"/WIN/32Bit";
so->osName.push_back(path);
SelectSoftware *ss = new SelectSoftware();
this->hide();
ss->show();
}
}
QVector<QString> SelectOS::getosName(){
so = new SelectOS();
return so->osName;
}
Window2.cpp
void SelectSoftware::getSoftwareDetails()
{
SelectOS *so = new SelectOS();
SelectSoftware *ss = new SelectSoftware();
ss->os = so->getosName();
QString fileName = ss->os + "/" +SOFTWARELIST; // Here I get the error...
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
QString msg = "Could not find the file " + fileName;
errorExit(msg);
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
processLine(line.toLower());
}
}
Help me, thanks ...
Assuming SOFTWARELIST is a simple QString:
QString fileName = QString( "%1/%2" )
.arg( ss->os.last() )
.arg( SOFTWARELIST );
This means you are creating a QString with placeholders %1 and %2 where %1 will be replaced by the result of ss->os.last() which returns the last item in the vector and %2 will be replaced by whatever SOFTWARELIST is.
If SOFTWARELIST is a vector as well, you will need to call e.g. .last() on it as well.
QVector is a container class, which holds set of something. In your example set of QString's. So then you try to form a fileName which is a QString you obviously cannot add to a fileName QString list of other Qstring's. (truly saying you can, but not with '+' operator and slightly different code).
Honestly I didn't got straight away why you actually passing QVector if you only need an application path I would suggest to use just QString.

qfiledialog - Filtering Folders?

1)I want to get the name of the folder for a folder monitoring Application..
Is there a way that i can filter out specific folders from being displayed using QFileDialog (For example i don't want the my documents to be displayed in the file dialog)..
2)I don't want the user to select a drive. By default in this code drives can also be selected..
dirname=QtGui.QFileDialog.getExistingDirectory(self,'Open Directory','c:\\',QtGui.QFileDialog.ShowDirsOnly)
print(dirname)
Is there a way that i can gray out the drives or some specific folders so that it can't be selected or can i set the filters for folder to prevent showing it up..
You can try setting a proxy model for your file dialog: QFileDialog::setProxyModel. In the proxy model class override the filterAcceptsRow method and return false for folders which you don't want to be shown. Below is an example of how proxy model can look like; it'c c++, let me know if there are any problems converting this code to python. This model is supposed to filter out files and show only folders:
class FileFilterProxyModel : public QSortFilterProxyModel
{
protected:
virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
};
bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
if (fileModel!=NULL && fileModel->isDir(index0))
{
qDebug() << fileModel->fileName(index0);
return true;
}
else
return false;
// uncomment to execute default implementation
//return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
here's how I was calling it
QFileDialog dialog;
FileFilterProxyModel* proxyModel = new FileFilterProxyModel;
dialog.setProxyModel(proxyModel);
dialog.setOption(QFileDialog::DontUseNativeDialog);
dialog.exec();
Note that the proxy model is supported by non-native file dialogs only.
You can try using QDir.Dirs filter.
dialog = QtGui.QFileDialog(parentWidget)
dialog.setFilter(QDir.Dirs)
serge_gubenko gave you the right answer. You only had to check the folder names and return "false" for the ones which should not be displayed. For instance, to filter out any folders named "private" you'd write:
bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
if (fileModel!=NULL && fileModel->isDir(index0))
{
qDebug() << fileModel->fileName(index0);
if (QString::compare(fileModel->fileName(index0), tr("private")) == 0)
return false;
return true;
}
else
return false;
// uncomment to execute default implementation
//return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
I already tested this and it works perfectly.
serge_gubenko should receive all due credit.
I know this is not exactly what you were asking, but if you're working with a QFileSystemModel, you can do it with the Name Filters option.
model = QFileSystemModel()
model.setNameFilters(["[abcdefghijklmnopqrstuvwxyz1234567890]*"])
model.setNameFilterDisables(False)
It worked for me, and I couldn't find the answer anywhere else on the internet, so I figured I post it here.
(I know my regex is trash, but using [\\w\\d]* didn't work and I was feeling lazy.)

Resources