Customization of user editable checkboxes implemented via QAbstractItemModel in QTreeView - c++

I have QTreeView with QAbstractItemModel. Some particular columns are supposed to have user defined checkboxes. I have done so by overriding QAbstractItemModel::data() function and by sending check state for Qt::CheckStateRole role as shown in the code.
I am getting checkboxes and am able to check and uncheck them successfully.
But the requirement is to customize some of these checkboxes. Basically I need to differentiate some checkboxes from the others by any method for eg: fill the checkbox with blue, make the boundary of the checkbox blue or any other method. But I am not sure how to change checkbox styling as I am creating checkbox via model.
QVariant MyModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::CheckStateRole && index.column() == COLUMN_WITH_CHECKBOX)
{
//return Qt::Checked or Qt::Unchecked here
}
//...
}
bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
if (role == Qt::CheckStateRole)
{
if ((Qt::CheckState)value.toInt() == Qt::Checked)
{
//user has checked item
return true;
}
else
{
//user has unchecked item
return true;
}
}
return false;
}

First you need is implement your own ItemDelegate
class CheckedDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
CheckedDelegate(QObject *parent = nullptr);
~CheckedDelegate();
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex& index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex& index) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex& index) const;
};
In this delegate you must implement custom editor and custom item painting. To create custom editor:
QWidget *CheckedDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QCheckBox *chBox = new QCheckBox(parent);
//customize editor checkbox
QString strQss = "QCheckBox::indicator:checked { image: url(:/icons/pic/checkboxChecked.png); } ";
strQss.append("QCheckBox::indicator:unchecked { image: url(:/icons/pic/checkboxUnchecked.png); }");
chBox->setStyleSheet(strQss);
return chBox;
}
void CheckedDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QCheckBox *chBox = dynamic_cast<QCheckBox*> (editor);
if (index.data(Qt::CheckStateRole).toInt() == Qt::Checked)
{
chBox->setChecked(true);
}
else
{
chBox->setChecked(false);
}
}
void CheckedDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QCheckBox *chBox = dynamic_cast<QCheckBox*> (editor);
model->setData(index, chBox->isChecked() ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
}
void CheckedDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(GetCheckboxRect(option));
}
To calculate checkbox geometry use
QRect GetCheckboxRect(const QStyleOptionViewItem &option)
{
QStyleOptionButton opt_button;
opt_button.QStyleOption::operator=(option);
QRect sz = QApplication::style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt_button);
QRect r = option.rect;
// center 'sz' within 'r'
double dx = (r.width() - sz.width()) / 2;
double dy = (r.height()- sz.height()) / 2;
r.setTopLeft(r.topLeft() + QPoint(qRound(dx),qRound(dy)));
r.setWidth(sz.width());
r.setHeight(sz.height());
return r;
}
Then implement custom painting. In this example I use pixmaps to customize checkbox so I also paint only pixmaps.
void CheckedDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter);
if (index.data(Qt::CheckStateRole).toInt() == Qt::Checked) {
QApplication::style()->drawItemPixmap(painter, GetCheckboxRect(option), Qt::AlignLeft | Qt::AlignVCenter, QPixmap(":/icons/pic/checkboxChecked.png"));
} else {
QApplication::style()->drawItemPixmap(painter, GetCheckboxRect(option), Qt::AlignLeft | Qt::AlignVCenter, QPixmap(":/icons/pic/checkboxUnchecked.png"));
}
}
And set your delegate (in my example I have TableTiew not TreeView)
CheckedDelegate *chDel = new CheckedDelegate(this);
ui->tableView->setItemDelegateForColumn(1, chDel);

Related

Wrapping a QStringListModel in a QAbstractItemModel renders a blank list

I want to start off making my own models for Qt list views, and I thought that I would start by wrapping a QStringListModel in my own QAbstractItemModel, then render it in a list view. However, it only renders a blank white square, instead of the list I expect. I don't really know what could be happening, given that all I'm doing is delegating all calls to the QStringListModel. Perhaps there's some aspects of the QStringListModel that are called by the QListView that are not mandated by the QAbstractItemModel pure virtual methods? Or maybe it's related to storage of the QStringList somehow?
My attempt is below. The header:
class DelegatingItemModel: public QAbstractItemModel {
public:
DelegatingItemModel();
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
private:
QAbstractItemModel* innerModel;
};
This is the implementation:
#include "delegating_item_model.hh"
DelegatingItemModel::DelegatingItemModel() {
QStringList available = {"foo", "bar", "baz"};
this->innerModel = new QStringListModel(available);
}
QVariant DelegatingItemModel::data(const QModelIndex &index, int role) const {
return innerModel->data(index, role);
}
int DelegatingItemModel::columnCount(const QModelIndex &parent) const {
return innerModel->columnCount(parent);
}
int DelegatingItemModel::rowCount(const QModelIndex &parent) const {
return innerModel->rowCount(parent);
}
QModelIndex DelegatingItemModel::parent(const QModelIndex &index) const {
return innerModel->parent(index);
}
QModelIndex DelegatingItemModel::index(int row, int column, const QModelIndex &parent) const {
return innerModel->index(row, column, parent);
}
And this is the entry point:
int main(int argc, char** argv) {
qDebug() << "Starting up";
QApplication app(argc, argv);
QMainWindow mainWindow;
QListView* listView = new QListView;
DelegatingItemModel* theModel = new DelegatingItemModel;
listView->setModel(theModel);
mainWindow.setCentralWidget(listView);
mainWindow.show();
return app.exec();
}
Your view will get data from the model only if the given index is linked to its model. If you print a trace in the data() method, you will see that it's never called.
So, you cannot return a new index created by your inner list model because it will be linked to the list and not your own model. For example:
QModelIndex DelegatingItemModel::index(int row, int column, const QModelIndex &parent) const {
//return innerModel->index(row, column, parent);
if (parent.isValid()) // It's a list. Not a tree
return QModelIndex();
return createIndex(row, column); // Create a index for your own model.
}
To be full compliant, you should convert the index in data():
QVariant DelegatingItemModel::data(const QModelIndex &index, int role) const {
QModelIndex const innerIndex(innerModel->index(index.row(), index.column()));
return innerModel->data(innerIndex, role);
}

Cannot check item of QListView

I'm working on a dialog class with a QListView and a customized model inheriting QAbstractListModel. Items on my list are custom widgets with several labels.
I managed to make a checkbox displayed for each item by reimplementing data(), setData() and flags() methods of my model, but when I run my code and click on a checkbox associated to one of the item, the checkbox doesn't appear as checked (remains unchecked).
Here's my code:
mymodel.h
class MyModel : public QAbstractListModel
{
Q_OBJECT
public:
MyModel(QObject *parent);
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE ;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
Qt::ItemFlags flags(const QModelIndex & index) const Q_DECL_OVERRIDE ;
QSet<QPersistentModelIndex> checkedItems;
};
mymodel.cpp
QVariant MyModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::CheckStateRole)
{
if(checkedItems.contains(index))
return Qt::Checked;
else
return Qt::Unchecked;
}
return QVariant();
}
bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
if(role == Qt::CheckStateRole)
{
if(value == Qt::Checked)
checkedItems.insert(index);
else
checkedItems.remove(index);
emit dataChanged(index, index);
}
return true;
}
Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
{
return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable;
}
mydelegate.h
class MyDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
MyDelegate(QObject* parent = 0) : QStyledItemDelegate(parent) {}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
mydelegate.cpp
QSize MyDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(400, 120);
}
In the constructor of mydialog.cpp
model = new MyModel(this);
ui->list->setModel(model);
ui->list->setItemDelegate(new MyDelegate(this));
I've tried adding flags Qt::ItemIsEnabled and Qt::ItemIsEditable but it didn't change anything.
I'm not very familiar with view/model implementation so far, although I've read Qt docs.
Thx for the help !
To be more precise about my comment, here is what I had in mind
bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
if(role == Qt::CheckStateRole)
{
if(value == Qt::Checked)
checkedItems.insert(index);
self.checkBoxList[index.row()][index.column()].setChecked(True)
else
checkedItems.remove(index);
self.checkBoxList[index.row()][index.column()].setChecked(False)
emit dataChanged(index, index);
}
return true;
}
I just do not have all of your code so I do not know how your self.checkBoxList is done. Are the checkBox child of the table?

Size of editor in QItemDelegate

I have a custom Delegate, subclassed from QItemDelegate, that provides a QComboBox in the very first column and a QLineEdit in all other columns.
SensorDisplayDelegate::SensorDisplayDelegate(QObject *parent) :
QItemDelegate(parent)
{}
QWidget *SensorDisplayDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
int col = index.column();
if(col == 0)
{
QComboBox *comboBox = new QComboBox(parent);
connect(comboBox, SIGNAL(activated(int)), this, SLOT(setData(int)));
comboBox->setEditable(false);
//comboBox->setMaximumSize(editorSize);
comboBox->setInsertPolicy(QComboBox::NoInsert);
currentComboBox = comboBox;
return comboBox;
}
else
{
QLineEdit *lineEdit = new QLineEdit(parent);
return lineEdit;
}
return NULL;
}
void SensorDisplayDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int col = index.column();
if(col == 0)
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QStringList comboItems = index.data(Qt::EditRole).toStringList();
comboBox->addItem("Add New Sensor");
comboBox->addItems(comboItems);
QCompleter *completer = new QCompleter(comboItems);
completer->setCaseSensitivity(Qt::CaseInsensitive);
comboBox->setCompleter(completer);
comboBox->showPopup();
}
else
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(index.data(Qt::EditRole).toString());
lineEdit->show();
}
}
void SensorDisplayDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const
{
int col = index.column();
if(col == 0)
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
if(comboBox->currentIndex() == 0)
emit addNewSensor();
else
emit populateSensorView(comboBox->currentText());
}
else
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
model->setData(index, QVariant(lineEdit->text()));
}
}
void SensorDisplayDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
QSize SensorDisplayDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
return editorSize;
}
void SensorDisplayDelegate::setData(int option)
{
emit commitData(currentComboBox);
emit closeEditor(currentComboBox);
}
The editTrigger has been set to selectClicked. I want the combo box to cover the entire cell in the QTableView. However, now it just appears as blip on the left hand corner. I tried setting the minimum size by passing the cell size through an event filter that listens for mousePressed on QTableView. However, the corresponding slot in the delegate is never called. Here's the code:
MultiEventFilter.cpp :
bool MultiEventFilter::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
if(obj->objectName() == "sensorlocationTableView")
{
QTableView *sensorView = static_cast<QTableView*>(obj);
QModelIndexList idxs = sensorView->selectionModel()->selectedIndexes();
if(!idxs.empty())
{
QModelIndex idx = idxs.at(0);
emit passCellSize(QSize(sensorView->columnWidth(idx.column()),
sensorView->rowHeight(idx.row())));
}
}
}
return false;
}
installed on qApp.
MainWindow.cpp:
eFilter = new MultiEventFilter();
connect(eFilter, SIGNAL(passCellSize(QSize)),
sensor_display_delegate, SLOT(setEditorSize(QSize)));
SensorDisplayDelegate.cpp slot:
void SensorDisplayDelegate::setEditorSize(const QSize &size)
{
editorSize = size;
}
where QSize editorSize is a private member.
How can I set the size of the editor correctly? I need something general that can be applied to the QLineEdit editors as well.
Also, is it necessary to explicitly emit commitData() when the editor is closed? I have not seen this done in any example codes involving QComboBox.
I suspect your eventFilter is intercepting the click events before the selection indexes have been set. So, you're effectively always hitting an empty idxs IndexList?
Try replacing that loop with something like:
bool MultiEventFilter::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
if(obj->objectName() == "sensorlocationTableView")
{
emit locationTableViewClicked();
}
}
return false;
}
....
connect(eFilter, SIGNAL(locationTableViewClicked()),
sensor_display_delegate, SLOT(setEditorSize()));
...
void SensorDisplayDelegate::setEditorSize()
{
QModelIndexList idxs = sensorView->selectionModel()->selectedIndexes();
if(!idxs.empty())
{
QModelIndex idx = idxs.at(0);
editorSize = QSize(sensorView->columnWidth(idx.column()),
sensorView->rowHeight(idx.row()));
}
}

Qtreeview colored border

I have a QTreeView that is filled using an AbstractItemModel. I want to highlight some entries by showing a red border based on an internal state.
Currently my code looks like this:
QVariant MyAbstractItemModel::data(QModelIndex const& index, int role)
...
else if (Qt::BackgroundRole == role)
{
if (someMethod(index))
return QColor(255,0,0);
return QVariant();
} ...
Obviously this codes sets the background color to red and not the border color.
How can I set the border color of the item?
So thanks to SaZ the solution is the QStyledItemDelegate.
My code looks now like this:
QVariant MyAbstractItemModel::data(QmodelIndex const&, int role)
{
...
else if (Qt::UserRole == role)
{
return someMethod(index);
}
...
}
and I have a delegate:
class MyDelegate
: public QStyledItemDelegate
{
virtual void paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const override
{
QStyledItemDelegate::paint(painter, option, index);
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
if (index.data(Qt::UserRole))
{
QRect rect(opt.rect.x(), opt.rect.y(), opt.rect.width(), opt.rect.height());
painter.save();
painter.setPen(Qt::red);
painter.drawRect(rect);
painter->restore();
}
}
}
...
MyDelegate _delegate;
_ui.myTreeView->setItemDelegate(&_delegate);

C++ Qt: set a active widget in QStyledItemDelegate::paint method

I would like to set a widget in a treeviews child row using a QStyledItemDelegate.
The widget is shown as intended but not clickable. It seems like it is not "active".
This is my paint method:
ProjectSpecificDelegate::ProjectSpecificDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void ProjectSpecificDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);
if(index.data(SpecialTreatmentsArchiv::ParentRowRol).toString() != "parent")
{
if (option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.highlight());
}
QPaintDevice* original_pdev_ptr = painter->device();
QList<ProjectSpecificArchivItem> item_list = index.data(SpecialTreatmentsArchiv::ChildRowRole).value<QList<ProjectSpecificArchivItem> >();
ProjectSpecificArchiv expand_widget(item_list);
painter->end();
expand_widget.render(painter->device(), QPoint(option.rect.x(), option.rect.y()), QRegion(0, 0, option.rect.width(), option.rect.height()), QWidget::DrawChildren);
painter->begin(original_pdev_ptr);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
My question is: What do I have to change so that it is possible to to interact with the widget?