Qt/ModelView/StringListModelCheckable

From ProgrammingExamples
< Qt
Revision as of 19:22, 17 December 2011 by Daviddoria (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

main.cpp

#include <QApplication>
 
#include "form.h"
 
int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  Form form;
 
  form.show();
  return app.exec();
}

form.h

#ifndef FORM_H
#define FORM_H
 
#include "ui_form.h"
 
class Form : public QWidget, public Ui::Form
{
    Q_OBJECT
 
public:
    Form(QWidget *parent = 0);
 
};
 
#endif

form.cpp

#include <QStringListModel>
#include <QtGui>
 
#include <iostream>
 
#include "form.h"
 
class CheckableStringListModel : public QStringListModel
{
 
public:
  void setMyStringList(const QStringList &strings)
  {
    emit beginResetModel();
    CheckedStatus.resize(strings.size());
    for(unsigned int i = 0; i < CheckedStatus.size(); ++i)
      {
      CheckedStatus[i] = Qt::Unchecked;
      }
    setStringList(strings);
    emit endResetModel();
  }
 
private:
  QVector<Qt::CheckState> CheckedStatus;
 
  Qt::ItemFlags flags(const QModelIndex& index) const
  {
    // If the index is not valid
    if (index.row() < 0 || index.row() >= rowCount())
      {
      return Qt::NoItemFlags;
      }
 
    return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
  }
 
  QVariant data (const QModelIndex  &index , int role ) const
  {
    // From QStringListModel 
    if (index.row() < 0 || index.row() >= rowCount())
      {
      return QVariant();
      }
 
    if (role == Qt::DisplayRole || role == Qt::EditRole)
      {
      return stringList().at(index.row());
      }
 
    if(role == Qt::CheckStateRole)
      {
      return CheckedStatus[index.row()];
      }
 
    return QVariant();
  }
 
  bool setData (const QModelIndex &index, const QVariant &value, int role)
  {
    // From QStringListModel
    if (index.row() < 0 || index.row() >= rowCount())
      {
      return false;
      }
 
    if(role == Qt::EditRole || role == Qt::DisplayRole)
      {
      stringList().replace(index.row(), value.toString());
      emit dataChanged(index, index);
      return true;
      }
    if(role == Qt::CheckStateRole)
      {
      //CheckedStatus[index.row()] = value.value<Qt::CheckState>();
      CheckedStatus[index.row()] = static_cast<Qt::CheckState>(value.toUInt());
 
      emit dataChanged(index, index);
      }
    return true;
 
  }
};
 
Form::Form(QWidget *parent) : QWidget(parent)
{
  setupUi(this);
 
  CheckableStringListModel* model = new CheckableStringListModel;
 
  QStringList list;
  list << "a" << "b" << "c";
  model->setMyStringList(list);
  this->listView->setModel(model);
}

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="0" column="0">
    <widget class="QListView" name="listView"/>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(StringListModelCheckable)
 
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(StringListModelCheckable main.cpp form.cpp ${MOCSrcs} ${UISrcs})
TARGET_LINK_LIBRARIES(StringListModelCheckable ${QT_LIBRARIES})