Frage

It is necessary to list of directories by given path in QListView whith icons and checkboxes, then transmit names of marked folders to the program. For list directories i use code:

#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();
    }

How to add checkboxes and transmit it after pushing button?

Thanks.

War es hilfreich?

Lösung

There's nothing you can do that would fit in just a few lines. You can either derive from QFileSystemModel and add a checkbox column, or create a proxy model that will do the same.

Note that you can use built-in selection mechanism to Ctrl-click/⌘-click to expand the selection to multiple items:

//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"
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top