Header File
#ifndef GRUPOMODEL_H
#define GRUPOMODEL_H
#include <QAbstractTableModel>
#include <QList>
#include <grupoclass.h>
#include <QMessageBox>
#include <QWidget>
{
public:
void setGrupoList(const QList<GrupoClass> &list);
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
private:
QString grupoAt
(int row,
int col
) const;
bool checkAt(int row) const;
QList<GrupoClass> grupoList;
};
#endif // GRUPOMODEL_H
#ifndef GRUPOMODEL_H
#define GRUPOMODEL_H
#include <QAbstractTableModel>
#include <QList>
#include <grupoclass.h>
#include <QMessageBox>
#include <QWidget>
class GrupoModel : public QAbstractTableModel
{
public:
GrupoModel(QObject *parent = 0);
void setGrupoList(const QList<GrupoClass> &list);
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
bool setData(const QModelIndex &index, const QVariant &value,int role);
void setParenti(QWidget *parenti);
private:
QString grupoAt(int row, int col) const;
bool checkAt(int row) const;
QList<GrupoClass> grupoList;
QString temp;
QWidget *parentias;
};
#endif // GRUPOMODEL_H
To copy to clipboard, switch view to plain text mode
CPP FILE
#include "grupomodel.h"
#include <QMessageBox>
#include <QAbstractTableModel>
{
}
int GrupoModel
::rowCount(const QModelIndex &parent
) const {
return grupoList.count();
}
int GrupoModel
::columnCount(const QModelIndex &parent
) const {
return 3;
}
QString GrupoModel
::grupoAt(int row,
int col
)const {
switch(col)
{
case 0: retorno="";break;
case 1: retorno=grupoList[row].Designacao();break;
case 2: retorno=grupoList[row].Informacao();break;
}
return retorno;
}
void GrupoModel::setGrupoList(const QList<GrupoClass> &list)
{
grupoList=list;
reset();
}
{
if (!index.isValid())
if (role == Qt::TextAlignmentRole) {
return int(Qt::AlignLeft | Qt::AlignVCenter);
}
else if (role == Qt::DisplayRole) {
return grupoAt(index.row(),index.column());
}
else if (role == Qt::EditRole)
{
return grupoAt(index.row(),index.column());
}
else if ((!index.column()) &&(role==Qt::CheckStateRole))
{
if (grupoList[index.row()].Check())
return Qt::Checked;
else
return Qt::Unchecked;
}
}
Qt
::ItemFlags GrupoModel
::flags(const QModelIndex &index
) const{
Qt::ItemFlags flags ;
if (index.column()>0)
else
flags = Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
return flags;
}
QVariant GrupoModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if (role != Qt::DisplayRole)
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("");
case 1:
return tr("Grupo");
case 2:
return tr("Informação");
default:
}
}
}
{
if (index.isValid() && role == Qt::EditRole)
a.setText(value.toString());
a.exec();
if (index.column()==1)
{
GrupoClass a(value.toString(),"",false);
if(grupoList.contains(a))
{
msgErro.setText("O grupo introduzido já existe.");
msgErro.exec();
return false;
}
else
{grupoList[index.row()].setDesignacao(value.toString());
}
}
emit dataChanged(index,index);
return true;
}
if (index.isValid() && role == Qt::CheckStateRole)
{
grupoList[index.row()].setCheck(!grupoList[index.row()].Check());
emit dataChanged(index,index);
return true;
}
return false;
}
void GrupoModel
::setParenti(QWidget *parenti
) {
parentias=parenti;
}
bool GrupoModel::checkAt(int row)const
{
return grupoList[row].Check();
}
#include "grupomodel.h"
#include <QMessageBox>
#include <QAbstractTableModel>
GrupoModel::GrupoModel(QObject *parent) : QAbstractTableModel(parent)
{
}
int GrupoModel::rowCount(const QModelIndex &parent) const
{
return grupoList.count();
}
int GrupoModel::columnCount(const QModelIndex &parent) const
{
return 3;
}
QString GrupoModel::grupoAt(int row, int col)const
{
QString retorno;
switch(col)
{
case 0: retorno="";break;
case 1: retorno=grupoList[row].Designacao();break;
case 2: retorno=grupoList[row].Informacao();break;
}
return retorno;
}
void GrupoModel::setGrupoList(const QList<GrupoClass> &list)
{
grupoList=list;
reset();
}
QVariant GrupoModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::TextAlignmentRole) {
return int(Qt::AlignLeft | Qt::AlignVCenter);
}
else if (role == Qt::DisplayRole) {
return grupoAt(index.row(),index.column());
}
else if (role == Qt::EditRole)
{
return grupoAt(index.row(),index.column());
}
else if ((!index.column()) &&(role==Qt::CheckStateRole))
{
if (grupoList[index.row()].Check())
return Qt::Checked;
else
return Qt::Unchecked;
}
return QVariant();
}
Qt::ItemFlags GrupoModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags ;
if (index.column()>0)
flags =QAbstractTableModel::flags(index)| Qt::ItemIsEditable | Qt::ItemIsEnabled;
else
flags = Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
return flags;
}
QVariant GrupoModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("");
case 1:
return tr("Grupo");
case 2:
return tr("Informação");
default:
return QVariant();
}
}
return QVariant();
}
bool GrupoModel::setData ( const QModelIndex & index, const QVariant & value, int role )
{
QMessageBox msgErro;
if (index.isValid() && role == Qt::EditRole)
{QMessageBox a;
a.setText(value.toString());
a.exec();
QMessageBox::warning(parentias,"","teste");
if (index.column()==1)
{
GrupoClass a(value.toString(),"",false);
if(grupoList.contains(a))
{
msgErro.setText("O grupo introduzido já existe.");
msgErro.exec();
return false;
}
else
{grupoList[index.row()].setDesignacao(value.toString());
}
}
emit dataChanged(index,index);
return true;
}
if (index.isValid() && role == Qt::CheckStateRole)
{
grupoList[index.row()].setCheck(!grupoList[index.row()].Check());
emit dataChanged(index,index);
return true;
}
return false;
}
void GrupoModel::setParenti(QWidget *parenti)
{
parentias=parenti;
}
bool GrupoModel::checkAt(int row)const
{
return grupoList[row].Check();
}
To copy to clipboard, switch view to plain text mode
I Have Problems here because emit CloseEditor
Comipler error QObject::installEventFilter(): Cannot filter events for objects in a different thread..
I base in the Qt demo code Spreadsheet, and some other codes like spinboxdelegate..
You can give-me a ideia what i make wrong for i can search.
#include <QtGui>
#include "tabledelegate.h"
#include <QMessageBox>
#include <QLineEdit>
TableDelegate
::TableDelegate(QObject *parent
)
{
connect(editor, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
return editor;
}
void TableDelegate::commitAndCloseEditor()
{
QLineEdit *editor
= qobject_cast<QLineEdit
*>
(sender
());
emit commitData(editor);
emit closeEditor(editor);
}
{
QLineEdit *edit
= qobject_cast<QLineEdit
*>
(editor
);
edit->setText(index.model()->data(index, Qt::EditRole).toString());
}
{
QLineEdit *edit
= qobject_cast<QLineEdit
*>
(editor
);
if (edit)
model->setData(index, edit->text());
}
#include <QtGui>
#include "tabledelegate.h"
#include <QMessageBox>
#include <QLineEdit>
TableDelegate::TableDelegate(QObject *parent)
: QItemDelegate(parent) {}
QWidget *TableDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex &index) const
{
QLineEdit *editor = new QLineEdit(parent);
connect(editor, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
return editor;
}
void TableDelegate::commitAndCloseEditor()
{
QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
edit->setText(index.model()->data(index, Qt::EditRole).toString());
}
void TableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
if (edit)
model->setData(index, edit->text());
}
To copy to clipboard, switch view to plain text mode
PS: The code can be a little confused because I the measure that I understand I added, not bothering to logically is very confusing
Added after 22 minutes:
I do not very well explained in the previous post. Finally I was QTableModel implement successfully. Now I'm trying to implement the delegate to carry out further validation before sending to the model.setData. So what I did was take the examples, and create a delegate without basic validations to see if it works well. Only it is giving me error.
Bookmarks