QListView с флажками для просмотра файловой системы

Необходимо составить список каталогов по заданному пути в QListView с иконками и флажками, затем передать имена отмеченных папок в программу. Для каталогов списка я использую код:

#include <QtGui/QApplication>
#include <QFileSystemModel>
#include <QListView>


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

        QFileSystemModel model;

        QListView listView;
        listView.setModel(&model);
        listView.setRootIndex(model.setRootPath("C:\\Program Files"));
        listView.show();

        return a.exec();
    }

Как добавить чекбоксы и передать их после нажатия кнопки?

Спасибо.


person Бориска Сосиска    schedule 28.06.2012    source источник
comment
+1 за sscce!   -  person Kuba hasn't forgotten Monica    schedule 28.06.2012


Ответы (1)


Вы не можете сделать ничего такого, что уместилось бы всего в нескольких строчках. Вы можете либо получить от QFileSystemModel и добавить столбец с флажком, либо создать прокси-модель, которая будет делать то же самое.

Обратите внимание, что вы можете использовать встроенный механизм выбора, удерживая нажатой клавишу Ctrl/⌘, чтобы расширить выделение до нескольких элементов:

//main.cpp
#include <QApplication>
#include <QFileSystemModel>
#include <QGridLayout>
#include <QListView>
#include <QPushButton>
#include <QMessageBox>

class Win : public QWidget
{
    Q_OBJECT
    QListView * view;
    QPushButton * button;
public:
    Win(QAbstractItemModel * model, const QModelIndex & idx) :
        view(new QListView(this)), button(new QPushButton("List Selection", this))
    {
        QGridLayout * lay = new QGridLayout;
        lay->addWidget(view, 0, 0, 1, 2);
        lay->addWidget(button, 1, 0);
        setLayout(lay);
        view->setSelectionMode(QAbstractItemView::MultiSelection);
        view->setModel(model);
        view->setRootIndex(idx);
        connect(button, SIGNAL(clicked()), SLOT(showSelection()));
    }
public slots:
    void showSelection() {
        QString str;
        foreach (QModelIndex i, view->selectionModel()->selectedIndexes()) {
            str.append(i.data().toString());
            str.append("\n");
        }
        QMessageBox::information(this, "Selected items", str);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFileSystemModel model;
    Win v(&model, model.setRootPath("/"));
    v.show();
    return a.exec();
}

#include "main.moc"
person Kuba hasn't forgotten Monica    schedule 28.06.2012
comment
Я также покажу код модели прокси. Дай мне несколько минут - cпасибо - person Kuba hasn't forgotten Monica; 28.06.2012
comment
Спасибо за ваш ответ. Это почти то, что мне нужно, но выделение пунктов нужно флажками. - person Бориска Сосиска; 28.06.2012