if(viewInit)
{
model->submitAll();
delete model;
}
int rowCount = 0;
model->setTable(tableName);
model->select();
model->removeColumn(8);
rowCount = model->rowCount();
ui.tableTV->setModel(model);
checkboxDelegate delegate;
ui.tableTV->setItemDelegate(&delegate);
ui.tableTV->resizeColumnToContents(2);
ui.tableTV->hideColumn(0);
ui.tableTV->setAlternatingRowColors(true);
ui.tableTV->scrollToBottom();
if(viewInit)
{
model->submitAll();
delete model;
}
int rowCount = 0;
model = new QSqlTableModel();
model->setTable(tableName);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->removeColumn(8);
rowCount = model->rowCount();
ui.tableTV->setModel(model);
checkboxDelegate delegate;
ui.tableTV->setItemDelegate(&delegate);
ui.tableTV->resizeColumnToContents(2);
ui.tableTV->hideColumn(0);
ui.tableTV->setEditTriggers(QAbstractItemView::CurrentChanged);
ui.tableTV->setAlternatingRowColors(true);
ui.tableTV->scrollToBottom();
To copy to clipboard, switch view to plain text mode
checkboxDelegate
::checkboxDelegate(QObject *parent
){
}
{
if(index.isValid() && index.column() == checkboxCol)
{
editor->installEventFilter(const_cast<checkboxDelegate*>(this));
return editor;
}
else
{
}
}
void checkboxDelegate
::setEditorData(QWidget *editor,
{
if(index.isValid() && index.column() == checkboxCol)
{
QString value
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
QCheckBox *checkBox
= static_cast<QCheckBox
*>
(editor
);
if(value == "Y")
checkBox->setCheckState(Qt::Checked);
else
checkBox->setCheckState(Qt::Unchecked);
}
else
{
}
}
{
if(index.isValid() && index.column() == checkboxCol)
{
QCheckBox *checkBox
= static_cast<QCheckBox
*>
(editor
);
if(checkBox->checkState() == Qt::Checked)
value = "Y";
else
value = "N";
model->setData(index, value);
}
else
{
}
}
void checkboxDelegate
::updateEditorGeometry(QWidget *editor,
{
if(index.isValid() && index.column() == checkboxCol)
editor->setGeometry(option.rect);
else
}
checkboxDelegate::checkboxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *checkboxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.isValid() && index.column() == checkboxCol)
{
QCheckBox *editor = new QCheckBox(parent);
editor->installEventFilter(const_cast<checkboxDelegate*>(this));
return editor;
}
else
{
return QItemDelegate::createEditor(parent, option, index);
}
}
void checkboxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
if(index.isValid() && index.column() == checkboxCol)
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
if(value == "Y")
checkBox->setCheckState(Qt::Checked);
else
checkBox->setCheckState(Qt::Unchecked);
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
void checkboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if(index.isValid() && index.column() == checkboxCol)
{
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
QString value;
if(checkBox->checkState() == Qt::Checked)
value = "Y";
else
value = "N";
model->setData(index, value);
}
else
{
QItemDelegate::setModelData(editor, model, index);
}
}
void checkboxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.isValid() && index.column() == checkboxCol)
editor->setGeometry(option.rect);
else
QItemDelegate::updateEditorGeometry(editor, option, index);
}
To copy to clipboard, switch view to plain text mode
Bookmarks