QFileSystemModel and Proxy sort differently in QTreeView - qt

I want to show only mounted drives sorted by drive letter in windows using Qt 5.10. Here is my test code:
#include <QtWidgets>
#include <QDebug>
QStringList mountedDrives;
class FSFilter : public QSortFilterProxyModel
{
public:
FSFilter(QObject *parent) : QSortFilterProxyModel(parent) {}
protected:
bool filterAcceptsRow(int row, const QModelIndex &par) const override
{
QModelIndex idx = sourceModel()->index(row, 0, par);
QString path = idx.data(QFileSystemModel::FilePathRole).toString();
QString path2 = idx.data(Qt::DisplayRole).toString();
bool mounted = mountedDrives.contains(path);
if (mounted) {
qDebug() << "QFileSystemModel::FilePathRole =" << path
<< " Qt::DisplayRole =" << path2;
return true;
}
else return false;
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// get mounted drives only
foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
if (storage.isValid() && storage.isReady()) {
if (!storage.isReadOnly()) {
mountedDrives << storage.rootPath();
}
}
}
QFileSystemModel *fsModel = new QFileSystemModel;
fsModel->setRootPath("");
FSFilter *fsFilter = new FSFilter(fsModel);
fsFilter->setSourceModel(fsModel);
fsFilter->setSortRole(QFileSystemModel::FilePathRole); // now it works
fsModel->fetchMore(fsModel->index(0,0));
QTreeView tree;
tree.setModel(fsFilter); // different sort if use
tree.setModel(fsFilter);
tree.setSortingEnabled(true);
tree.sortByColumn(0, Qt::AscendingOrder);
tree.resize(800, 400);
tree.setColumnWidth(0, 300);
tree.setWindowTitle(QObject::tr("File System Test"));
tree.show();
return app.exec();
}
This works fine, producing this output, except it is not sorted by drive letter:
Running the same code, but setting the treeview model as
tree.setModel(fsModel); // use QFileSystemModel, not QSortFilterProxyModel
results in the following, where the items are sorting the way I want:
I thought this might be a role issue. Here is the output from my qDebug statements:
QFileSystemModel::FilePathRole = "C:/" Qt::DisplayRole = "C:"
QFileSystemModel::FilePathRole = "D:/" Qt::DisplayRole = "Data
(D:)"
QFileSystemModel::FilePathRole = "E:/" Qt::DisplayRole = "Photos
(E:)"
QFileSystemModel::FilePathRole = "F:/" Qt::DisplayRole =
"Archive (F:)"
QFileSystemModel::FilePathRole = "H:/" Qt::DisplayRole = "Old D
(H:)"
QFileSystemModel::FilePathRole = "I:/" Qt::DisplayRole = "Old C
(I:)"
So I included this line
fsFilter->setFilterRole(QFileSystemModel::FilePathRole);
but this did not help.
Solution (corrected in code listing above):
fsFilter->setSortRole(QFileSystemModel::FilePathRole);

You should reimplement virtual function lessThan in FSFilter, by default it sort alphabetically, but in this case you need take into account first and last letters in drive name.

Related

Qt QCompleter do not pop out

Problem description
  I want to be able to recognize the corresponding Chinese person name and complete the prompt by entering a short Chinese phonetic alphabet.
  For example, I have a map (("lvbu", "吕布"), ("lvbuwei", "吕不韦")) , then enter "lv" or "bu",The completer should pop up "吕布" and "吕不韦", but it seems that the stringlist must contain the content currently being entered to complete the prompt. The completer cannot recognize the mapping relationship.
  How can I solve it? I can hardly find a solution on the Internet because it involves Chinese.
My code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Pinyin2Hanzi/myPinyin.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString N = "吕布 吕不韦 南宫问天";
QStringList Names = N.split(" ");
// qDebug() << Names;
ui->comboBox->addItem("");
ui->comboBox->addItems(Names);
ui->comboBox->lineEdit()->setClearButtonEnabled(true);
CreateCompleter(Names, ui->comboBox->lineEdit());
ui->comboBox->setMaxCount(ui->comboBox->count()); //这行代码可以防止按回车键自动往combobox里addItem
}
MainWindow::~MainWindow()
{
delete ui;
}
std::map<std::string, std::vector<QString>> pinyin2NameMap; // This is a private variable
// 把所有人名按照拼音分类并存入map中
void MainWindow::PreparePinyinData(const QStringList &names)
{
for (auto & name : names)
{
QString outFristPy,outFullPy;
getComPingyinForStr(name, outFristPy, outFullPy);
// QString pinyin = GetPinyin(name);
QString pinyin = outFullPy;
// qDebug() <<"FristPy:" << outFristPy << "FullPy:" << outFullPy;
// qDebug() <<"pinyin:" << pinyin;
// QString fist, last;
// myNameSplit(name, last, fist); // 自动切分 [姓、名]
// last = getNamePingyin(last, true); // 获取 [姓] 的拼音
// fist = getNamePingyin(fist, false);// 获取 [名] 的拼音
// qDebug() << name + " : " + last + " " + fist << endl;
pinyin2NameMap[pinyin.toStdString()].push_back(name);
}
}
// 根据输入的拼音进行匹配并获取提示列表
QStringList MainWindow::GetMatchByPinyin(const QString &pinyin)
{
QStringList result;
if("" == pinyin.trimmed()){
return QStringList();
}
for (const auto & iter : pinyin2NameMap){
if (iter.first.find(pinyin.toStdString()) != std::string::npos){
auto vec = iter.second;
for(const auto & name : vec){
result.append(name);
}
}
}
return result;
}
// 创建QCompleter并设置自动补全模型
void MainWindow::CreateCompleter(const QStringList &names, QLineEdit *lineEdit)
{
PreparePinyinData(names);
QStringListModel *model = new QStringListModel(names);
QCompleter *completer = new QCompleter(model, lineEdit);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setFilterMode(Qt::MatchContains);
completer->setCompletionMode(QCompleter::PopupCompletion);
lineEdit->setCompleter(completer);
connect(lineEdit, &QLineEdit::textEdited, [this,completer,lineEdit, model](const QString &text)
{
QStringList matchList = GetMatchByPinyin(text);
qDebug() << "listmodel:" << matchList;
// QString N = "lv不bu 可lvbuwei nan够gongwentian";
// QStringList matchList = N.split(" ");
model->setStringList(matchList);
completer->setModel(model);
completer->complete();
});
}

QListView max number of items in view

I need to calculate max number of items in current view of QListView.
I wrote code like this:
void MyListView::resizeEvent(QResizeEvent *event)
{
QListView::resizeEvent(event);
QFontMetrics fm (this->font());
int fontHeight = fm.lineSpacing();
QRect cr = contentsRect();
int windowHeight = cr.bottom() - cr.top();
int maxItemsCount = windowHeight / fontHeight;
qDebug()<<"max items in view: "<< maxItemsCount;
}
but calculated max number of items is is incorrect.
E.g. in case of my window height and font height I get 32 max items in view when in fact current view has 28 items. Perhaps someone can suggest something, how to calculate it properly?
My idea is to use QListView::indexAt() (inherited from QAbstractView) to obtain the row index for
the top-left corner
the bottom-left corner
of the list view viewport and determining the number of visible items by difference of them.
To check this out, I made an MCVE testQListViewNumVisibleItems.cc:
// Qt header:
#include <QtWidgets>
class ListWidget: public QListWidget {
public:
ListWidget(QWidget *pQParent = nullptr): QListWidget(pQParent) { }
virtual ~ListWidget() = default;
ListWidget(const ListWidget&) = delete;
ListWidget& operator=(const ListWidget&) = delete;
int getNumVisibleItems() const
{
const QSize size = viewport()->size();
const QModelIndex qMIdx0 = indexAt(QPoint(0, 0));
const QModelIndex qMIdx1 = indexAt(QPoint(0, size.height() - 1));
//qDebug() << "qMIdx0:" << qMIdx0 << "qMIdx1:" << qMIdx1;
return qMIdx0.isValid()
? (qMIdx1.isValid() ? qMIdx1.row() : count()) - qMIdx0.row()
: 0;
}
};
const int MaxItems = 20;
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
ListWidget qLst;
qLst.resize(200, 200);
qLst.show();
// timer to populate list view
using namespace std::chrono_literals;
QTimer qTimer;
qTimer.setInterval(1000ms);
// install signal handlers
int n = 0;
QObject::connect(&qTimer, &QTimer::timeout,
[&]() {
qLst.addItem(QString("item %0").arg(++n));
qDebug() << "Visible items:" << qLst.getNumVisibleItems();
if (n >= MaxItems) qTimer.stop();
});
// runtime loop
qTimer.start();
return app.exec();
}
and a CMakeLists.txt:
project(QListViewNumVisibleItems)
cmake_minimum_required(VERSION 3.10.0)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Qt5Widgets CONFIG REQUIRED)
include_directories("${CMAKE_SOURCE_DIR}")
add_executable(testQListViewNumVisibleItems testQListViewNumVisibleItems.cc)
target_link_libraries(testQListViewNumVisibleItems Qt5::Widgets)
built and tested in VS2017 on Windows 10:
After having implemented what came in my mind, I googled a bit to possibly see other approaches. (I admit I should've done before.)
Thereby I found the following possible duplicate:
SO: Simple way to get all visible items in the QListView
The accepted answer doesn't contain much more than the hint for indexAt and a link to a Qt-FAQ article:
How can i get hold of all of the visible items in my QListView?
In order to get hold of the visible items in a QListView http://doc.qt.io/qt-5/qlistview.html, then you can iterate over them using indexAt() http://doc.qt.io/qt-5/qlistview.html#indexAt. You can get hold of the first visible item using indexAt(QPoint(0, 0)), then in order to get the index at the next position then use visualRect() http://doc.qt.io/qt-5/qlistview.html#visualRect to find out what your next call to itemAt() should be. This position would be:
visualRect.y() + visualRect.height() + 1 effectively.
See the following example for an illustration:
#include <QtGui>
QList <QModelIndex>myList;
class ListView : public QListView
{
Q_OBJECT
public:
ListView()
{
QStringListModel *myModel = new QStringListModel(this);
QStringList list;
list << "a" << "b" <<"c" <<"d" <<"e" <<"f" <<"g" <<"h" <<"i" <<"j" <<"k";
myModel->setStringList(list);
setModel(myModel);
QTimer::singleShot(3000, this, SLOT(test1()));
}
public slots:
void test1()
{
QModelIndex firstIndex = indexAt(QPoint(0, 0));
if (firstIndex.isValid()) {
myList << firstIndex;
} while (viewport()->rect().contains(QPoint(0, visualRect(firstIndex).y() + visualRect(firstIndex).height() + 1 ))) {
firstIndex = indexAt(QPoint(0, visualRect(firstIndex).y() + visualRect(firstIndex).height() + 1 ));
myList << firstIndex;
}
qDebug() << myList.count() << "are visible";
}
};
#include "main.moc"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
ListView window;
window.resize(100, 50);
window.show();
return app.exec();
}

Make tree folder from QTreeView or QTreeWidget

read folder tree from a Rest API, then show them to user
Example json response after call API:
[
{"name":"/folder1/file1.txt";"size":"1KB"},
{"name":"/folder1/file2.txt";"size":"1KB"},
{"name":"/folder1/sub/file3.txt";"size":"1KB"},
{"name":"/folder2/file4.txt";"size":"1KB"},
{"name":"/folder2/file5.txt";"size":"1KB"}
]
I only want to make GUI like below image:
There are 2 options:
QTreeView
QTreeWidget
In this photo, I used QTreeWidget (with static data).
Currently, I don't know make data model for this.
I made TreeModel to set data for QtreeView.
But When them shown in GUI, All of the data only show in column Name.
I copied the code from http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html
But now I can't resolve this problem. I need an example source code for this.
Plus, I also only want show a tree view simply.
don't use QFileSystem because data get from Rest API.
What you have to do is parsing the json by getting the name of the file and the size, then you separate the name using the "/", and add it to the model in an appropriate way.
#include <QApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QStandardItemModel>
#include <QTreeView>
#include <QFileIconProvider>
QStandardItem * findChilItem(QStandardItem *it, const QString & text){
if(!it->hasChildren())
return nullptr;
for(int i=0; i< it->rowCount(); i++){
if(it->child(i)->text() == text)
return it->child(i);
}
return nullptr;
}
static void appendToModel(QStandardItemModel *model, const QStringList & list, const QString & size){
QStandardItem *parent = model->invisibleRootItem();
QFileIconProvider provider;
for(QStringList::const_iterator it = list.begin(); it != list.end(); ++it)
{
QStandardItem *item = findChilItem(parent, *it);
if(item){
parent = item;
continue;
}
item = new QStandardItem(*it);
if(std::next(it) == list.end()){
item->setIcon(provider.icon(QFileIconProvider::File));
parent->appendRow({item, new QStandardItem(size)});
}
else{
item->setIcon(provider.icon(QFileIconProvider::Folder));
parent->appendRow(item);
}
parent = item;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStandardItemModel model;
model.setHorizontalHeaderLabels({"Name", "Size"});
const std::string json = R"([
{"name":"/folder1/file1.txt";"size":"1KB"},
{"name":"/folder1/file2.txt";"size":"1KB"},
{"name":"/folder1/sub/file3.txt";"size":"1KB"},
{"name":"/folder2/file4.txt";"size":"1KB"},
{"name":"/folder2/file5.txt";"size":"1KB"}
])";
QJsonParseError parse;
// The string is not a valid json, the separator must be a comma
// and not a semicolon, which is why it is being replaced
QByteArray data = QByteArray::fromStdString(json).replace(";", ",");
QJsonDocument const& jdoc = QJsonDocument::fromJson(data, &parse);
Q_ASSERT(parse.error == QJsonParseError::NoError);
if(jdoc.isArray()){
for(const QJsonValue &element : jdoc.array() ){
QJsonObject obj = element.toObject();
QString name = obj["name"].toString();
QString size = obj["size"].toString();
appendToModel(&model, name.split("/", QString::SkipEmptyParts), size);
}
}
QTreeView view;
view.setModel(&model);
view.show();
return a.exec();
}
Note: the semicolon is not a valid separator for the json, so I had to change it.

Qt5 Subclassing QStyledItemDelegate Formatting

I am developing a GUI for a SQLite database in Qt5. I use QSqlQueryModel and QTableView for storing and displaying the data.
I then created a custom delegate to replace the numeric values of certain columns with their literals in the table view (e.g. 1 = "Hello", 2 = "World") using a switch statement.
The delegate displays the data as it should and works functionally. However, the columns that the custom delegate paints over have a different format compared to the default paint method of QStyledItemDelegate. The values are up in the top left rather than centre left, the altered column no longer automatically expands the column to display the full values, and the cells in column do not turn blue or have the dotted outline when selected.
I created this example program:
#include <QApplication>
#include <QModelIndex>
#include <QPainter>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QTableView>
class TestDelegate: public QStyledItemDelegate {
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)
const Q_DECL_OVERRIDE
{
if (index.column() == 0) {
int value = index.model()->data(index, Qt::DisplayRole).toInt();
QString str;
switch (value) {
case 1:
str = "Hello0000";
break;
case 2:
str = "World0000";
break;
}
if (option.state.testFlag (QStyle::State_Selected)) {
painter->fillRect(option.rect, option.palette.highlight());
qApp->style()->drawItemText(painter, option.rect, option.displayAlignment, option.palette, true, str, QPalette::HighlightedText);
} else {
painter->drawText(option.rect, option.displayAlignment, str);
}
} else {
return QStyledItemDelegate::paint(painter, option, index);
}
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
QStandardItemModel model(2, 2);
model.setHorizontalHeaderItem(0, new QStandardItem(QString("A")));
model.setHorizontalHeaderItem(1, new QStandardItem(QString("B")));
model.setData(model.index(0, 0, QModelIndex()), 1);
model.setData(model.index(1, 0, QModelIndex()), 2);
model.setItem(0, 1, new QStandardItem(QString("Hello")));
model.setItem(1, 1, new QStandardItem(QString("World0000")));
QTableView view;
view.setItemDelegate(new TestDelegate);
view.setModel(&model);
view.resizeColumnsToContents();
view.show();
app.exec();
}
This fixes the text alignment by adding options.displayAlignment to painter->drawText(); I have also added additional code in the if(option.state & QStyle::State_Selected) statement that paints the cell according to its selection state. So if it isn't selected the text is black, if it is the text turns white and the background blue. However I still cannot get the columns to expand to fit the cells' content or add a dotted line around the outside of the cell as it does with the standard delegate.
Is there a simple way to maintain the default style of the table view when using my custom paint method?
The delegate is a rather circuitous and unnecessary way of going about it. We already have a view that paints the elements perfectly fine, no need to redo that. We only need to pass modified data to the view. Thus we insert a QIdentityProxyModel viewmodel between the source and the view.
// https://github.com/KubaO/stackoverflown/tree/master/questions/proxy-reformat-39244309
#include <QtWidgets>
class RewriteProxy : public QIdentityProxyModel {
QMap<QVariant, QVariant> m_read, m_write;
int m_column;
public:
RewriteProxy(int column, QObject * parent = nullptr) :
QIdentityProxyModel{parent}, m_column{column} {}
void addReadMapping(const QVariant & from, const QVariant & to) {
m_read.insert(from, to);
m_write.insert(to, from);
}
QVariant data(const QModelIndex & index, int role) const override {
auto val = QIdentityProxyModel::data(index, role);
if (index.column() != m_column) return val;
auto it = m_read.find(val);
return it != m_read.end() ? it.value() : val;
}
bool setData(const QModelIndex & index, const QVariant & value, int role) override {
auto val = value;
if (index.column() == m_column) {
auto it = m_write.find(value);
if (it != m_write.end()) val = it.value();
}
return QIdentityProxyModel::setData(index, val, role);
}
};
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QStandardItemModel model{2,2};
model.setData(model.index(0, 0), 1);
model.setData(model.index(1, 0), 2);
model.setData(model.index(0, 1), "Zaphod");
model.setData(model.index(1, 1), "Beeblebrox");
RewriteProxy proxy{0};
proxy.setSourceModel(&model);
proxy.addReadMapping(1, "Hello");
proxy.addReadMapping(2, "World");
QTableView ui;
ui.setModel(&proxy);
ui.show();
return app.exec();
}

QListView with QAbstractListModel shows an empty list

I have created a very simple example of QListView with a custom QAbstractListModel. The QListView is displayed but it is empty.
What am I doing wrong?
Code:
#include <QListView>
#include <QAbstractListModel>
#include <QApplication>
class DataModel: public QAbstractListModel
{
public:
DataModel() : QAbstractListModel() {}
int rowCount( const QModelIndex & parent = QModelIndex() ) const { return 2; }
QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const
{
return "a";
}
};
int main( int argc, char **argv)
{
QApplication app(argc, argv, true);
QListView *lv = new QListView();
DataModel d;
lv->setModel( &d );
lv->show();
app.setMainWidget(lv);
app.exec();
}
Thanks!
The fix to the previous code is to set the parent of the model to the QListView:
DataModel d(lv);
But this raises a question, where is the model/view independence if the model has to have a reference to the view?
What if I want to use this model in two different views?
Your methods data should return "a" only if role = Qt::DisplayRole. Otherwise, it returns "a" for every role.
So, add a simple test and it will work :
QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const
{
if ( role == Qt::DisplayRole ) {
return "a";
}
return QVariant();
}

Resources