Qt/ModelView/AbstractListModelFileDialog
From ProgrammingExamples
< Qt
Contents
main.cpp
#include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Form form; form.show(); return app.exec(); }
AbstractListModelFileDialog.h
#ifndef AbstractListModel_H #define AbstractListModel_H #include <QAbstractListModel> #include <QVector> class Item { public: Item(const QString& name = QString()) : Name(name){} QString Name; }; class AbstractListModel : public QAbstractListModel { public: AbstractListModel(); QVariant data(const QModelIndex& index, int role) const; bool setData (const QModelIndex &index, const QVariant &value, int role); int rowCount(const QModelIndex &parent=QModelIndex() ) const; int setItems(const QVector<Item>& items); Qt::ItemFlags flags (const QModelIndex &index ) const; protected: QVector<Item> Items; }; #endif
AbstractListModelFileDialog.cpp
#include "AbstractListModelFileDialog.h" #include <iostream> int AbstractListModel::setItems(const QVector<Item>& items) { emit beginResetModel(); this->Items = items; emit endResetModel(); } Qt::ItemFlags AbstractListModel::flags (const QModelIndex &index ) const { if (index.row() < 0 || index.row() >= rowCount() || !index.isValid()) { return Qt::NoItemFlags; } return Qt::ItemIsEditable | Qt::ItemIsEnabled; } AbstractListModel::AbstractListModel() : QAbstractListModel() { } int AbstractListModel::rowCount(const QModelIndex& parent) const { return this->Items.size(); } QVariant AbstractListModel::data (const QModelIndex &index , int role ) const { if (index.row() < 0 || index.row() >= rowCount() || !index.isValid()) { return QVariant(); } if (role == Qt::DisplayRole || role == Qt::EditRole) { return this->Items.at(index.row()).Name; } return QVariant(); } bool AbstractListModel::setData (const QModelIndex &index, const QVariant &value, int role) { if (index.row() < 0 || index.row() >= rowCount() || !index.isValid()) { return false; } if (role == Qt::DisplayRole || role == Qt::EditRole) { this->Items[index.row()].Name = value.toString(); } emit dataChanged(index, index); return true; }
form.h
#ifndef FORM_H #define FORM_H #include "ui_form.h" class AbstractListModel; class Form : public QWidget, public Ui::Form { Q_OBJECT public: Form(QWidget *parent = 0); public slots: void editSlot(QModelIndex index); protected: AbstractListModel* Model; }; #endif
form.cpp
#include <QAbstractListModel> #include <QtGui> #include <iostream> #include "form.h" #include "AbstractListModelFileDialog.h" Form::Form(QWidget *parent) : QWidget(parent) { setupUi(this); this->Model = new AbstractListModel; connect (this->listView, SIGNAL(clicked(QModelIndex)), this, SLOT(editSlot(QModelIndex))); QVector<Item> items; items.push_back(Item("Test0")); items.push_back(Item("Test1")); items.push_back(Item("Test2")); this->Model->setItems(items); this->listView->setModel(this->Model); } void Form::editSlot(QModelIndex index) { if (index.row() == 0) { QString fileName = QFileDialog::getOpenFileName(this, "Open", "", "Files (*.*)"); this->Model->setData(index, fileName, Qt::EditRole); } }
form.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<widget class="QPushButton" name="btnUpdate">
<property name="text">
<string>Update</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QListView" name="listView"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(AbstractListModelFileDialog) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(UISrcs form.ui) QT4_WRAP_CPP(MOCSrcs form.h) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(AbstractListModelFileDialog main.cpp AbstractListModelFileDialog.cpp form.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(AbstractListModelFileDialog ${QT_LIBRARIES})