##// END OF EJS Templates
more intelligent legend. fixed compiler warning in bar.cpp
more intelligent legend. fixed compiler warning in bar.cpp

File last commit:

r546:3faf122e2566
r565:b4a66ce9ee2b
Show More
customtablemodel.cpp
146 lines | 3.5 KiB | text/x-c | CppLexer
Marek Rosa
Table model data example
r519 #include "customtablemodel.h"
CustomTableModel::CustomTableModel(QObject *parent) :
QAbstractTableModel(parent)
{
m_points.append(QPointF(10, 50));
m_labels.append("Apples");
m_points.append(QPointF(60, 70));
m_labels.append("Oranges");
m_points.append(QPointF(110, 50));
m_labels.append("Bananas");
m_points.append(QPointF(140, 40));
m_labels.append("Lemons");
m_points.append(QPointF(200, 150));
m_labels.append("Plums");
Marek Rosa
QXYSeries: model data orientation added. QBarSeries: some model data function placeholders
r527 m_points.append(QPointF(225, 75));
m_labels.append("Pearls");
Marek Rosa
Table model data example
r519 }
int CustomTableModel::rowCount(const QModelIndex & parent) const
{
return m_points.count();
}
int CustomTableModel::columnCount(const QModelIndex & parent) const
{
Marek Rosa
Added support for adding and removing data with model. Updated the example
r545 return 3;
Marek Rosa
Table model data example
r519 }
QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
{
switch(section)
{
case 0:
return "x";
case 1:
return "y";
case 2:
return "Fruit";
Marek Rosa
QXYSeries: model data orientation added. QBarSeries: some model data function placeholders
r527 default:
return "What?";
Marek Rosa
Table model data example
r519 }
}
else
return QString("%1").arg(section + 1);
}
QVariant CustomTableModel::data(const QModelIndex & index, int role) const
{
if (role == Qt::DisplayRole)
{
switch(index.column())
{
case 0:
return m_points[index.row()].x();
case 1:
return m_points[index.row()].y();
case 2:
return m_labels[index.row()];
default:
return QVariant();
}
}
else if (role == Qt::EditRole)
{
switch(index.column())
{
case 0:
return m_points[index.row()].x();
case 1:
return m_points[index.row()].y();
case 2:
return m_labels[index.row()];
default:
return QVariant();
}
}
}
bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
{
if (index.isValid() && role == Qt::EditRole)
{
switch(index.column())
{
case 0:
m_points[index.row()].setX(value.toDouble());
break;
case 1:
m_points[index.row()].setY(value.toDouble());
break;
case 2:
m_labels.replace(index.row(), value.toString());
break;
default:
return false;
}
emit dataChanged(index, index);
return true;
}
return false;
}
Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
{
Marek Rosa
QXYSeries: model data orientation added. QBarSeries: some model data function placeholders
r527 // if (!index.isValid())
// return Qt::ItemIsEnabled;
Marek Rosa
Table model data example
r519 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
{
Marek Rosa
Model data example updated
r546 if (row < 0)
row = 0;
Marek Rosa
Table model data example
r519 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
Marek Rosa
Added support for adding and removing data with model. Updated the example
r545 for (int i = row; i < row + count; i++)
Marek Rosa
Table model data example
r519 {
Marek Rosa
Added support for adding and removing data with model. Updated the example
r545 m_points.insert(row, QPointF());
m_labels.insert(row,(""));
Marek Rosa
Table model data example
r519 }
endInsertRows();
return true;
}
Marek Rosa
Added support for adding and removing data with model. Updated the example
r545
bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
{
if (row > this->rowCount() - 1)
return false;
if (row < 0)
row = 0;
if (row + count > rowCount())
return false;
beginRemoveRows(parent, row, row + count - 1);
for (int i = row; i < row + count; i++)
{
m_points.removeAt(row);
m_labels.removeAt(row);
}
endRemoveRows();
return true;
}