I'm trying in QFileSystemModel display just files with extention *.txt and the other types shaded/grayed out:
proxy_ is of type QSortFilterProxyModel
model_ is of type QFileSystemModel
Here's my code:
proxy_->setFilterWildcard("*.txt");
proxy_->setSourceModel(model_);
model_->setNameFilters(QStringList(proxy_->filterRegExp().pattern()));
model_->setNameFilterDisables(true);
sel_model_ = (new QItemSelectionModel(proxy_));
treeView->setModel(proxy_);
treeView->setSelectionModel(sel_model_);
...but by doing so nothing is shown in my view. Anyone knows what I'm doing wrong?
You can set a file name filter with QFileSystemModel::setNameFilters.
In the example program below .txt and folders are displayed normally, and other files are disabled (greyed out).
The nameFilterDisables property allows you to choose between filtered out files being disabled or hidden.
#include <QtGui>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QFileSystemModel model;
model.setRootPath(QDir::rootPath());
QStringList filters;
filters << "*.txt";
model.setNameFilters(filters);
QTreeView view;
view.setModel(&model);
view.show();
return app.exec();
}
Related
I'm trying to generate a simple QTreeView inside another widget (QMainWindow). The following code works as expected and displays the tree-view,
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow w;
w.show();
QString rootPath = "C:/";
QFileSystemModel model;
model.setRootPath("");
QTreeView tree;
tree.setModel(&model);
if (!rootPath.isEmpty()) {
const QModelIndex rootIndex = model.index(QDir::cleanPath(rootPath));
if (rootIndex.isValid())
tree.setRootIndex(rootIndex);
}
tree.setParent(&w);
tree.show();
return app.exec();
}
but if I extract the code that generates the tree-view, nothing seems to happen. The extracted function is as follows:
void create_tree(QMainWindow *w) {
QString rootPath = "C:/";
QFileSystemModel model;
model.setRootPath("");
QTreeView tree;
tree.setModel(&model);
if (!rootPath.isEmpty()) {
const QModelIndex rootIndex = model.index(QDir::cleanPath(rootPath));
if (rootIndex.isValid())
tree.setRootIndex(rootIndex);
}
tree.setParent(w);
tree.show();
}
and the corresponding function call in main function is as follows:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow w;
w.show();
create_tree(&w);
return app.exec();
}
How does the extracted function create_tree work and why is it not showing the tree view?
QFileSystemModel model;
and
QTreeView tree;
Are local stack variables, meaning they will be gone once you exit the create_tree function.
You can solve your issue by creating them on the heap by using new, which will keep them alive. Be careful, that you need to think about how you destroy these created objects. The Qt parenting system is a great help there, because the parent will destroy its children when it is destroyed, so your tree view is fine. You should think about good parent for your model to make sure you create no memory leak.
A working version of your function looks like this - be careful that you still need to handle the models deletion:
void create_tree(QMainWindow *w) {
QString rootPath = "C:/";
QFileSystemModel* model = new QFileSystemModel();
model->setRootPath("");
QTreeView* tree = new QTreeView();
tree->setModel(model);
if (!rootPath.isEmpty()) {
const QModelIndex rootIndex = model->index(QDir::cleanPath(rootPath));
if (rootIndex.isValid())
tree->setRootIndex(rootIndex);
}
tree->setParent(w);
tree->show();
}
Having a QLineEdit with a plain vanilla QStringList QCompleter. I wonder if I can change the appearance of the dropdown (I want to have either a min. size or smaller scrollbar).
Clarification: I want to set it in a stylesheet, not in the code.
Summary of my findings so far:
Pretty good summary here: https://forum.qt.io/topic/26703/solved-stylize-using-css-and-editable-qcombobox-s-completions-list-view/12
I have to use QStyledItemDelegate and
give the popup a name for the qss selector
I have tried that and it does not work for me, but seems to work for others
A simple straight forward solution is to set the stylesheet of the QScrollBar used by the popup of the QCompleter. My knowledge of qss is little, so I don't know if you can set a minimum size that way, but you can always have a look at verticalScrollBar().
Here is some code for the qss way:
#include <QAbstractItemView>
#include <QCompleter>
#include <QLineEdit>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLineEdit edit;
edit.show();
QStringList completionList;
for (int a = 0 ; a < 10 ; ++a) {
completionList << QString("test%1").arg(a);
}
QCompleter completer(completionList);
edit.setCompleter(&completer);
QAbstractItemView *popup = completer.popup();
popup->setStyleSheet("QScrollBar{ width: 50px;}");
return a.exec();
}
I am currently trying to make a QTreeView to display the contents of the folder on the computer. However, I experienced some weird issue where . and .. are displayed in the tree view which I do not want that to happen. How am I suppose to disable showing . and .. in the tree view?
Here is the code for the QTreeView.
model = new QDirModel(this);
model->setReadOnly(true);
model->setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name);
model->setFilter(QDir::Dirs);
ui->treeView->setModel(model);
// expand to D: Directory
QModelIndex index = model->index("D:/");
ui->treeView->expand(index);
ui->treeView->scrollTo(index);
ui->treeView->setCurrentIndex(index);
ui->treeView->resizeColumnToContents(0);
Finally figure out the answer:
model->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
Using the following will not work as the tree view can longer be expanded on each folder:
model->setFilter(QDir::Dirs);
model->setFilter(QDir::NoDotAndDotDot);
You had a look at this?
This is a standard Qt example given out with Qt creator, They are doing exact same what you want to do.
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFileSystemModel model;
model.setFilter( QDir::Dirs | QDir::NoDotAndDotDot );
model.setRootPath("");
QTreeView tree;
tree.setModel(&model);
// Demonstrating look and feel features
tree.setAnimated(false);
tree.setIndentation(20);
tree.setSortingEnabled(true);
tree.setWindowTitle(QObject::tr("Dir View"));
#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5)
tree.showMaximized();
#else
tree.resize(640, 480);
tree.show();
#endif
return app.exec();
}
In my project, I have a QVector full of QStrings containing file paths/names that don't actually exist on any drive:
QVector<QString> fileNames["level.dat", "data/villages.dat", "players/player1.dat"]//etc
I want to create a tree in my QTreeWidget that resembles a sort of file directory like this:
How would I go about creating something like this quickly and efficiently?
Thanks for your time :)
This solution does not avoid duplicate, so if you need that in the future, you could extend this piece of code with adding valiation for that. Anyway, this code produces the exact ame output for me that you have just described to wish to have.
main.cpp
#include <QTreeWidget>
#include <QStringList>
#include <QApplication>
int main(int argc, char **argv)
{
QApplication application(argc, argv);
QStringList fileNames{"level.dat", "data/villages.dat", "players/player1.dat"};
QTreeWidget treeWidget;
treeWidget.setColumnCount(1);
for (const auto& filename : fileNames) {
QTreeWidgetItem *parentTreeItem = new QTreeWidgetItem(&treeWidget);
parentTreeItem->setText(0, filename.split('/').first());
QStringList filenameParts = filename.split('/').mid(1);
for (const auto& filenamePart : filenameParts) {
QTreeWidgetItem *treeItem = new QTreeWidgetItem();
treeItem->setText(0, filenamePart);
parentTreeItem->addChild(treeItem);
parentTreeItem = treeItem;
}
}
treeWidget.show();
return application.exec();
}
main.pro
TEMPLATE = app
TARGET = main
QT += widgets
CONFIG += c++14
SOURCES += main.cpp
Build and Run
qmake && make && ./main
On a site node: you ought to use QStringList rather than QVector. In addition, your current initialization attempt surely results in compiler error. That is invalid initialization.
Pretty simple task but I didn't manage to find anything useful in documentation. I want a QTreeView to contain a single column called "Files" with data from QFileSystemView. Here's what I've got:
QFileSystemModel *projectFiles = new QFileSystemModel();
projectFiles->setRootPath(QDir::currentPath());
ui->filesTree->setModel(projectFiles);
ui->filesTree->setRootIndex(projectFiles->index(QDir::currentPath()));
// hide all but first column
for (int i = 3; i > 0; --i)
{
ui->filesTree->hideColumn(i);
}
That gives me a single column with "Name" header. How do I rename this header?
QAbstractItemModel::setHeaderData() should work. If not, you can always inherit from QFileSystemModel and override headerData().
Quick but a little dirty trick (please note w.hideColumn()):
#include <QApplication>
#include <QFileSystemModel>
#include <QTreeView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView w;
QFileSystemModel m;
m.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
m.setRootPath("C:\\");
w.setModel(&m);
w.setRootIndex(m.index(m.rootPath()));
w.hideColumn(3);
w.hideColumn(2);
w.hideColumn(1);
w.show();
return a.exec();
}
You can subclass QFileSystemModel and overide method headerData(). For example, if you want only to change first header label and leave the rest with their original values, you can do:
QVariant MyFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const {
if ((section == 0) && (role == Qt::DisplayRole)) {
return "Folder";
} else {
return QFileSystemModel::headerData(section,orientation,role);
}
}