Difference between revisions of "Qt/ModelView/AbstractListModelCheckable"

From ProgrammingExamples
< Qt
Jump to: navigation, search
(AbstractListModelCheckable.cpp)
(AbstractListModelCheckable.cpp)
Line 61: Line 61:
 
#include "AbstractListModelCheckable.h"
 
#include "AbstractListModelCheckable.h"
  
#include <sstream>
+
#include <iostream>
  
 
int AbstractListModelCheckable::setItems(const QVector<Item>& items)
 
int AbstractListModelCheckable::setItems(const QVector<Item>& items)
 
{
 
{
 +
  emit beginResetModel();
 
   this->Items = items;
 
   this->Items = items;
 +
  emit endResetModel();
 
}
 
}
  
 
Qt::ItemFlags AbstractListModelCheckable::flags (const QModelIndex  &index ) const
 
Qt::ItemFlags AbstractListModelCheckable::flags (const QModelIndex  &index ) const
 
{
 
{
   return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
+
  if (index.row() < 0 || index.row() >= rowCount())
 +
    {
 +
    return Qt::NoItemFlags;
 +
    }
 +
   return Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
 
}
 
}
  
Line 110: Line 116:
 
     }
 
     }
  
  if(role == Qt::EditRole || role == Qt::DisplayRole)
 
    {
 
    this->Items[index.row()].Name = value.toString();
 
    emit dataChanged(index, index);
 
    return true;
 
    }
 
 
   if(role == Qt::CheckStateRole)
 
   if(role == Qt::CheckStateRole)
 
     {
 
     {
 
     this->Items[index.row()].Displayed = static_cast<Qt::CheckState>(value.toUInt());
 
     this->Items[index.row()].Displayed = static_cast<Qt::CheckState>(value.toUInt());
 +
    }
  
    emit dataChanged(index, index);
+
  emit dataChanged(index, index);
    }
+
 
   return true;
 
   return true;
 
 
}
 
}
  

Revision as of 13:15, 18 December 2011

main.cpp

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

AbstractListModelCheckable.h

#ifndef AbstractListModelCheckable_H
#define AbstractListModelCheckable_H
 
#include <QAbstractListModel>
 
#include <QVector>
 
class Item
{
public:
  Item(const QString& name = QString(), const bool displayed = false) :
  Name(name), Displayed(displayed){}
  QString Name;
  bool Displayed;
};
 
class AbstractListModelCheckable : public QAbstractListModel
{
public:
  AbstractListModelCheckable();
 
  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

AbstractListModelCheckable.cpp

#include "AbstractListModelCheckable.h"
 
#include <iostream>
 
int AbstractListModelCheckable::setItems(const QVector<Item>& items)
{
  emit beginResetModel();
  this->Items = items;
  emit endResetModel();
}
 
Qt::ItemFlags AbstractListModelCheckable::flags (const QModelIndex  &index ) const
{
  if (index.row() < 0 || index.row() >= rowCount())
    {
    return Qt::NoItemFlags;
    }
  return Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
}
 
AbstractListModelCheckable::AbstractListModelCheckable() : QAbstractListModel()
{
 
}
 
int AbstractListModelCheckable::rowCount(const QModelIndex& parent) const
{
  return this->Items.size();
}
 
QVariant AbstractListModelCheckable::data (const QModelIndex  &index , int role ) const
{
  if (index.row() < 0 || index.row() >= rowCount())
    {
    return QVariant();
    }
 
  if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
    return this->Items.at(index.row()).Name;
    }
 
  if(role == Qt::CheckStateRole)
    {
    return this->Items[index.row()].Displayed;
    }
 
  return QVariant();
}
 
bool AbstractListModelCheckable::setData (const QModelIndex &index, const QVariant &value, int role)
{
  if (index.row() < 0 || index.row() >= rowCount())
    {
    return false;
    }
 
  if(role == Qt::CheckStateRole)
    {
    this->Items[index.row()].Displayed = static_cast<Qt::CheckState>(value.toUInt());
    }
 
  emit dataChanged(index, index);
  return true;
}

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);
 
  public slots:
 
 
};
 
#endif

form.cpp

#include <QAbstractTableModel>
#include <QtGui>
 
#include <iostream>
#include <sstream>
 
#include "form.h"
#include "AbstractListModelCheckable.h"
 
Form::Form(QWidget *parent)
    : QWidget(parent)
{
  setupUi(this);
 
  AbstractListModelCheckable* model = new AbstractListModelCheckable;
 
  QVector<Item> items;
  items.push_back(Item("Test0", true));
  items.push_back(Item("Test1", false));
  items.push_back(Item("Test2", true));
  model->setItems(items);
  this->tableView->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="QTableView" name="tableView">
     <attribute name="horizontalHeaderStretchLastSection">
      <bool>true</bool>
     </attribute>
    </widget>
   </item>
   <item row="1" column="0">
    <widget class="QPushButton" name="btnUpdate">
     <property name="text">
      <string>Update</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

CMakeLists.txt

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