2023-04-19 13:51:23 +00:00
|
|
|
|
|
|
|
#include "tablewidget.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tableWidget::tableWidget(QWidget *parent): QTableWidget(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void tableWidget::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if(event->button() == Qt::RightButton)
|
|
|
|
{
|
2023-04-23 20:29:44 +00:00
|
|
|
QMenu menu;
|
|
|
|
QAction *insert= menu.addAction("Insert Item");
|
|
|
|
QAction *add= menu.addAction("Add Item");
|
|
|
|
QAction *del = menu.addAction("Delete Item");
|
2023-04-19 13:51:23 +00:00
|
|
|
QAction *selectedAction = menu.exec(event->globalPosition().toPoint());
|
|
|
|
|
2023-04-23 20:29:44 +00:00
|
|
|
if(selectedAction == insert)
|
|
|
|
{
|
|
|
|
this->insertRow(this->currentRow());
|
|
|
|
}
|
|
|
|
else if(selectedAction == add)
|
2023-04-19 13:51:23 +00:00
|
|
|
{
|
|
|
|
this->insertRow(this->rowCount());
|
|
|
|
}
|
|
|
|
else if( selectedAction == del )
|
|
|
|
{
|
|
|
|
this->removeRow(this->currentRow());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 20:29:44 +00:00
|
|
|
tableCombobox::tableCombobox(QAbstractItemModel* model, bool sort, QObject *parent)
|
2023-04-19 13:51:23 +00:00
|
|
|
: QItemDelegate(parent), modelData(model)
|
|
|
|
{
|
2023-04-23 20:29:44 +00:00
|
|
|
if (sort)
|
|
|
|
modelData->sort(0);
|
2023-04-19 13:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* tableCombobox::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
2023-04-23 20:29:44 +00:00
|
|
|
Q_UNUSED(index)
|
|
|
|
Q_UNUSED(option)
|
2023-04-19 13:51:23 +00:00
|
|
|
QComboBox* comboBox = new QComboBox(parent);
|
2023-04-23 20:29:44 +00:00
|
|
|
comboBox->setModel(modelData);
|
2023-04-19 13:51:23 +00:00
|
|
|
return comboBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tableCombobox::setEditorData(QWidget *editor, const QModelIndex &index) const {
|
|
|
|
// update model widget
|
|
|
|
QString value = index.model()->data(index, Qt::EditRole).toString();
|
|
|
|
qDebug() << "Value:" << value;
|
|
|
|
QComboBox* comboBox = static_cast<QComboBox*>(editor);
|
|
|
|
comboBox->setCurrentIndex(comboBox->findText(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tableCombobox::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
|
|
|
|
// store edited model data to model
|
|
|
|
QComboBox* comboBox = static_cast<QComboBox*>(editor);
|
|
|
|
QString value = comboBox->currentText();
|
|
|
|
model->setData(index, value, Qt::EditRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tableCombobox::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
2023-04-23 20:29:44 +00:00
|
|
|
Q_UNUSED(index)
|
2023-04-19 13:51:23 +00:00
|
|
|
editor->setGeometry(option.rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tableCheckbox::tableCheckbox(QObject *parent)
|
|
|
|
: QItemDelegate(parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* tableCheckbox::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
2023-04-23 20:29:44 +00:00
|
|
|
Q_UNUSED(index)
|
|
|
|
Q_UNUSED(option)
|
2023-04-19 13:51:23 +00:00
|
|
|
QCheckBox* checkBox = new QCheckBox(parent);
|
|
|
|
return checkBox;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tableCheckbox::setEditorData(QWidget *editor, const QModelIndex &index) const {
|
|
|
|
// update model widget
|
|
|
|
bool value = index.model()->data(index, Qt::EditRole).toBool();
|
|
|
|
qDebug() << "Value:" << value;
|
|
|
|
QCheckBox* checkBox = static_cast<QCheckBox*>(editor);
|
|
|
|
checkBox->setEnabled(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tableCheckbox::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
|
|
|
|
// store edited model data to model
|
|
|
|
QCheckBox* checkBox = static_cast<QCheckBox*>(editor);
|
|
|
|
bool value = checkBox->isChecked();
|
|
|
|
model->setData(index, value, Qt::EditRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tableCheckbox::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
2023-04-23 20:29:44 +00:00
|
|
|
Q_UNUSED(index)
|
2023-04-19 13:51:23 +00:00
|
|
|
editor->setGeometry(option.rect);
|
|
|
|
}
|