use QModelIndex to set background of a QTreeWidgetItem - c++

I have MainWindow with a qTreeWidget. To add elements to this widget I've implemented this functions:
QTreeWidgetItem *MainWindow::prepareIt(QTreeWidgetItem *it, const QString &name, const QString &descr)
{
it->setText(0, name);
it->setText(1, descr);
return it;
}
QTreeWidgetItem *MainWindow::addRoot(const QString &name, const QString &descr)
{
QTreeWidgetItem *it = prepareIt(new QTreeWidgetItem(ui->treeWidget), name, descr);
it->setBackground( 0, QColor{112, 77, 75} );
it->setBackground( 1, QColor{78, 90, 110} );
return it;
}
QTreeWidgetItem *MainWindow::addChild(QTreeWidgetItem *parent, const QString &name, const QString &descr)
{
auto child = new QTreeWidgetItem(parent);
child->setBackground( 0, QColor{102, 67, 65} );
child->setBackground( 1, QColor{68, 80, 99} );
parent->addChild(prepareIt(child, name, descr));
return child;
}
...
addRoot(...);
addChild(parent,...);
...
It works as expected. Now I want to highlight some entrys in this qTreeWidget with a right mouse click. In the C-Tor of my MainWindow I implemented:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
...
ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeWidget, SIGNAL (customContextMenuRequested(const QPoint &)), this, SLOT (rightClick(const QPoint &)));
...
}
and the slot is
void MainWindow::rightClick(const QPoint &pt)
{
QModelIndex idx = ui->treeWidget->indexAt(pt);
if (idx.isValid())
{
qDebug() << idx.data().toString();
qDebug() << idx << "index.row()" << idx.row() << " index.column()" << idx.column();
}
}
The slot is called as expected (the qDebug works), but how can I get from the QModelIndex idx to the corresponding QTreeWidgetItem for the highlighting? Or is there another way to make the highligt / change the color of the element?
Thanks in advance!!

Change the color of the cell:
A possible solution is to use the setData() method:
void MainWindow::rightClick(const QPoint &pt)
{
QModelIndex idx = ui->treeWidget->indexAt(pt);
if (idx.isValid())
{
ui->treeWidget->model()->setData(idx, QColor("red"), Qt::BackgroundRole);
}
}
But that will only change one cell, if you want to change the color then the entire row will have to be traversed:
Change the row color:
void MainWindow::rightClick(const QPoint &pt)
{
QModelIndex idx = ui->treeWidget->indexAt(pt);
if (idx.isValid())
{
for(int c=0; c < ui->treeWidget->columnCount(); ++c){
QModelIndex ix = idx.sibling(idx.row(), c);
ui->treeWidget->model()->setData(ix, QColor("red"), Qt::BackgroundRole);
}
}
}
Or:
void MainWindow::rightClick(const QPoint &pt)
{
QTreeWidgetItem *it = ui->treeWidget->itemAt(pt);
if (it)
{
for(int c=0; c < ui->treeWidget->columnCount(); ++c){
it->setBackground(c, QColor("red"));
}
}
}

Related

How to force a model to update QComboBox when data changed?

I created model for QComboBox:
#ifndef QCOMBOBOXMODEL_H
#define QCOMBOBOXMODEL_H
#include <QModelIndex>
class QComboBoxModel : public QAbstractListModel
{
public:
QComboBoxModel(QObject *parent=nullptr);
int rowCount(const QModelIndex &) const;
QVariant data(const QModelIndex &index, int role) const;
void populate(const QList<QPair<int,QString>> &values);
private:
QList<QPair<int,QString>> values;
};
#endif // QCOMBOBOXMODEL_H
code
#include "qcomboboxmodel.h"
#include <QModelIndex>
QComboBoxModel::QComboBoxModel(QObject *parent)
:QAbstractListModel(parent)
{
}
int QComboBoxModel::rowCount(const QModelIndex &) const
{
return values.count();
}
QVariant QComboBoxModel::data( const QModelIndex &index, int role ) const
{
QVariant value;
switch ( role )
{
case Qt::DisplayRole: //string
{
value = this->values.value(index.row()).second;
}
break;
case Qt::UserRole: //data
{
value = this->values.value(index.row()).first;
}
break;
default:
break;
}
return value;
}
void QComboBoxModel::populate(const QList<QPair<int,QString>> &values)
{
this->values = values;
}
Now i use it
values.append(QPair<int,QString>(-1,"Select item"));
values.append(QPair<int,QString>(10,"item1(0)"));
values.append(QPair<int,QString>(11,"item1(1)"));
values.append(QPair<int,QString>(21,"item1(2)"));
values.append(QPair<int,QString>(32,"item1(3)"));
values.append(QPair<int,QString>(44,"item1(4)"));
newidx = 50;
model = new QComboBoxModel();
model->populate(values);
this->ui->comboBox->setModel(model);
and on button click i add new item to combobox
newidx++;
QString strIdx = QString().number(newidx);
values.append(QPair<int,QString>(newidx,"New item("+strIdx+")"));
model = new QComboBoxModel();
model->populate(values);
this->ui->comboBox->setModel(model);
Its all seems works just fine, but problem here that i need to recreate model every time i add new item to combobox data
model = new QComboBoxModel();
model->populate(values);
this->ui->comboBox->setModel(model);
Is that a proper way to do so? Or there are another way to force model update combobox when data updated?
According to "Model Subclassing Reference" in the documentation you have to do many more things to make an editable model. Is there a reason why you don't use a ready made model like QStandardItemModel?
comboModel = new QStandardItemModel(0, 2, this);
ui->comboBox1->setModel(comboModel);
comboModel->insertRow(0);
comboModel->setData(comboModel->index(0, 0), -1);
comboModel->setData(comboModel->index(0, 1), "Select item");
//and so on
//and the data is available as
int number = comboModel->data(comboModel->index(0, 0)).toInt();
QString itemtext = comboModel->data(comboModel->index(0, 1)).toString();
I find solution!
First i add new method to model
void QComboBoxModel::append(int index, QString value)
{
int newRow = this->values.count();
this->beginInsertRows(QModelIndex(), newRow, newRow);
values.append(QPair<int,QString>(index,value));
endInsertRows();
}
Now on button click method changed to this
void MainWindow::on_pushButton_clicked()
{
qDebug() << "Clicked!";
newidx++;
QString strIdx = QString().number(newidx);
model->append(newidx,"new item " + strIdx );
}
Point is to use beginInsertRows and endInsertRows to notify model that data actually changed!
Now all worked as expected!
Now you also can modify append method to batch add rows to it, but i think if you add many rows it better just recreate model and reassign it to combobox.
Update 1:
Also keep in mind, that you update values QList inside model, so if you add
qDebug() << values;
in on_pushButton_clicked() method, you always see
(QPair(-1,"Select item"), QPair(10,"item1(0)"), QPair(11,"item1(1)"), QPair(21,"item1(2)"), QPair(32,"item1(3)"), QPair(44,"item1(4)"))
Update 2:
Also i update populate method
void QComboBoxModel::populate(const QList<QPair<int,QString>> &newValues)
{
int oldIdx = this->values.count();
int newIdx = newValues.count();
this->beginInsertRows(QModelIndex(), oldIdx, newIdx);
this->values = newValues;
endInsertRows();
}
Now you can just work with values list
void MainWindow::on_pushButton_clicked()
{
qDebug() << "Clicked!";
newidx++;
QString strIdx = QString().number(newidx);
values.append(QPair<int,QString>(newidx,"new item " + strIdx));
model->populate(values);
qDebug() << values;
}
Update 3:
Now, i find one big problem - i did not use pointer inside model, so when i pass QList to model it just create copy instead use already created, so i rewrite model and other code:
Model
#ifndef QCOMBOBOXMODEL_H
#define QCOMBOBOXMODEL_H
#include <QModelIndex>
class QComboBoxModel : public QAbstractListModel
{
public:
QComboBoxModel(QObject *parent=nullptr);
int rowCount(const QModelIndex &) const;
QVariant data(const QModelIndex &index, int role) const;
void populate(QList<QPair<int,QString>> *newValues);
void append(int index, QString value);
private:
QList<QPair<int,QString>> *values;
};
#endif // QCOMBOBOXMODEL_H
#include "qcomboboxmodel.h"
#include <QModelIndex>
#include <QDebug>
QComboBoxModel::QComboBoxModel(QObject *parent)
:QAbstractListModel(parent)
{
values = new QList<QPair<int,QString>>();
}
int QComboBoxModel::rowCount(const QModelIndex &) const
{
return values->count();
}
QVariant QComboBoxModel::data( const QModelIndex &index, int role ) const
{
QVariant value;
switch ( role )
{
case Qt::DisplayRole: //string
{
value = this->values->value(index.row()).second;
}
break;
case Qt::UserRole: //data
{
value = this->values->value(index.row()).first;
}
break;
default:
break;
}
return value;
}
void QComboBoxModel::populate(QList<QPair<int,QString>> *newValues)
{
int oldIdx = this->values->count();
int newIdx = newValues->count();
this->beginInsertRows(QModelIndex(), oldIdx, newIdx);
this->values = newValues;
endInsertRows();
}
void QComboBoxModel::append(int index, QString value)
{
int newRow = this->values->count();
this->beginInsertRows(QModelIndex(), newRow, newRow);
values->append(QPair<int,QString>(index,value));
endInsertRows();
}
Main form
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "qcomboboxmodel.h"
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_comboBox_currentIndexChanged(int index);
void on_comboBox_currentIndexChanged(const QString &arg1);
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
int newidx;
QList<QPair<int,QString>> *values;
QComboBoxModel *model;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "qcomboboxmodel.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
values = new QList<QPair<int,QString>>();
values->append(QPair<int,QString>(-1,"Select item"));
values->append(QPair<int,QString>(10,"item1(0)"));
values->append(QPair<int,QString>(11,"item1(1)"));
values->append(QPair<int,QString>(21,"item1(2)"));
values->append(QPair<int,QString>(32,"item1(3)"));
values->append(QPair<int,QString>(44,"item1(4)"));
newidx = 50;
model = new QComboBoxModel();
model->populate(values);
this->ui->comboBox->setModel(model);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
qDebug() << ui->comboBox->itemData(index).value<int>();
}
void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
qDebug() << arg1;
}
void MainWindow::on_pushButton_clicked()
{
qDebug() << "Clicked!";
newidx++;
QString strIdx = QString().number(newidx);
values->append(QPair<int,QString>(newidx,"new item " + strIdx));
model->populate(values);
qDebug() << values->toStdList();
}
Now all looks just fine and works as intended!
Why don't you request the current model via "QAbstractItemModel * model() const" from the QComboBox; alter it and assign it again (via "void QComboBox::setModel(QAbstractItemModel *model)")?

Drag and drop of QTreeWidgetItem does not work properly

I subclassed a QTreeWidget in order to reimplement the protected functionalities of drag and drop in order to drag drop parents and children of a QTreeWidget.
It almost works and the problem is that as soon as I try to drag and drop either children or parents, they are erased as soon as I drop them.
Source code can be found here in case you need to see what is going on.
Also if I try to drag-drop the parent, it unfortunately does not move and nothing happens.
See below the strange effect for the children:
I drag the child "Original" to drop it right under "Sample" and this procedure seems to work correctly:
After dropping "Original" as you can see the index seems to be there but it seems to be partially erased:
I am not sure exactly of what is going on and why the children seems to not completely be dropped correctly and why the parents are literally not moving.
Below the code of the minimal verifiable example:
mainwindow.h
#include <QMainWindow>
class QTreeWidget;
class QTreeWidgetItem;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QTreeWidgetItem *createItem(const QString &name, const QString &iconPath);
private:
Ui::MainWindow *ui;
QTreeWidget *widget;
QString m_Name;
QString m_Path;
};
#endif // MAINWINDOW_H
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->treeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui->treeWidget->setDragEnabled(true);
ui->treeWidget->viewport()->setAcceptDrops(true);
ui->treeWidget->setDropIndicatorShown(true);
ui->treeWidget->setDragDropMode(QAbstractItemView::InternalMove);
auto *top1 = createItem("Images", "/home/ui/qrc/laserscan.png");
auto *top2 = createItem("Path", "/home/ui/qrc/laserscan.png");
top1->addChild(createItem("Original", "/home/ui/qrc/laserscan.png"));
top1->addChild(createItem("Sample", "/home/ui/qrc/laserscan.png"));
top2->addChild(createItem("Left Side", "/home/ui/qrc/laserscan.png"));
top2->addChild(createItem("Right Side", "/home/ui/qrc/laserscan.png"));
ui->treeWidget->addTopLevelItems({ top1, top2 });
// Below I am assigning to each parent/children a checkbox
const int n_tops = ui->treeWidget->topLevelItemCount();
for(int a = 0; a<n_tops; ++a) {
const int n_children = ui->treeWidget->topLevelItem(a)->childCount();
qtreewidgetitem_assign_qcheckbox(ui->treeWidget, ui->treeWidget->topLevelItem(a));
for(int b = 0; b<n_children; ++b) {
qtreewidgetitem_assign_qcheckbox(ui->treeWidget, ui->treeWidget->topLevelItem(a)->child(b));
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
QTreeWidgetItem *MainWindow::createItem(const QString &name, const QString &iconPath)
{
auto *item = new QTreeWidgetItem(QStringList{name});
item->setIcon(0, QIcon(iconPath));
return item;
}
maphelpers.h
#include <QTreeWidget>
#include <QTreeWidgetItem>
void qtreewidgetitem_assign_qcheckbox(QTreeWidget *tree_widget, QTreeWidgetItem *tree_item);
#endif // MAPHELPERS_H
maphelpers.cpp
#include <QTreeWidget>
void qtreewidgetitem_assign_qcheckbox(QTreeWidget *tree_widget, QTreeWidgetItem *tree_item)
{
MyCheckBoxMap *myCheckBoxMap = new MyCheckBoxMap(tree_item);
tree_widget->setItemWidget(tree_item, MAP_COLUMN, myCheckBoxMap);
}
Below how I subclassed QTreeWidget:
customtreewidget.h
#include <QTreeWidget>
class CustomTreeWidget : public QTreeWidget
{
public:
CustomTreeWidget(QWidget *parent = Q_NULLPTR);
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;
private:
QTreeWidgetItem *draggedItem;
};
#endif // CUSTOMTREEWIDGET_H
customtreewidget.cpp
CustomTreeWidget::CustomTreeWidget(QWidget *parent)
{
QTreeWidgetItem* parentItem = new QTreeWidgetItem();
parentItem->setText(0, "Test");
parentItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled);
int num_rows = topLevelItemCount();
for(int i = 0; i < num_rows; ++i)
{
int nChildren = topLevelItem(i)->childCount();
QTreeWidgetItem* pItem = new QTreeWidgetItem(parentItem);
for(int j = 0; j < nChildren; ++j) {
pItem->setText(0, QString("Number %1").arg(i) );
pItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
pItem->addChild(pItem);
topLevelItem(i)->child(j);
}
}
}
void CustomTreeWidget::dragEnterEvent(QDragEnterEvent *event)
{
draggedItem = currentItem();
QTreeWidget::dragEnterEvent(event);
}
void CustomTreeWidget::dropEvent(QDropEvent *event)
{
QModelIndex droppedIndex = indexAt(event->pos());
if(!droppedIndex.isValid()) {
return;
}
if(draggedItem) {
QTreeWidgetItem *mParent = draggedItem->parent();
if(mParent) {
if(itemFromIndex(droppedIndex.parent()) != mParent)
return;
mParent->removeChild(draggedItem);
mParent->insertChild(droppedIndex.row(), draggedItem);
}
}
}
What I have done so far and what I have tried:
1) According to official documentation it is possible to use QTreeWidgetItemIterator Class and i tried to go that way and in fact you can see below my initial implementation idea, but this didn't really bring me anywhere:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto *top1 = createItem("Images", "/home/ui/qrc/laserscan.png");
auto *top2 = createItem("Path", "/home/ui/qrc/laserscan.png");
top1->addChild(createItem("Original", "/home/ui/qrc/laserscan.png"));
top1->addChild(createItem("Sample", "/home/ui/qrc/laserscan.png"));
top2->addChild(createItem("Left Side", "/home/ui/qrc/laserscan.png"));
top2->addChild(createItem("Right Side", "/home/ui/qrc/laserscan.png"));
ui->treeWidget->addTopLevelItems({ top1, top2 });
const int n_tops = ui->treeWidget->topLevelItemCount();
// Below I am assigning to each parent/children a checkbox
for(int a = 0; a<n_tops; ++a) {
const int n_children = ui->treeWidget->topLevelItem(a)->childCount();
qtreewidgetitem_assign_qcheckbox(ui->treeWidget, ui->treeWidget->topLevelItem(a));
for(int b = 0; b<n_children; ++b) {
qtreewidgetitem_assign_qcheckbox(ui->treeWidget, ui->treeWidget->topLevelItem(a)->child(b));
}
}
QTreeWidgetItem *parentItem = Q_NULLPTR;
QTreeWidgetItem *childItem = Q_NULLPTR;
QTreeWidgetItemIterator it(ui->treeWidget);
while (*it) {
parentItem = new QTreeWidgetItem(ui->treeWidget);
//parentItem->setText(0, it.key());
foreach (const auto &str, it) {
childItem = new QTreeWidgetItem;
childItem->setText(0, str);
parentItem->addChild(childItem);
}
}
}
2) After further research in the official documentation I decided to approach the problem applying the QMapIterator Class I wanted to keep the constructor as I structured initially and figured that passing through a QMap meant to rewrite the constructor in a different way, basically the implementation idea below, but didn't want to pass that way:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto *top1 = createItem("Images", "/home/ui/qrc/laserscan.png");
auto *top2 = createItem("Path", "/home/ui/qrc/laserscan.png");
top1->addChild(createItem("Original", "/home/ui/qrc/laserscan.png"));
top1->addChild(createItem("Sample", "/home/ui/qrc/laserscan.png"));
top2->addChild(createItem("Left Side", "/home/ui/qrc/laserscan.png"));
top2->addChild(createItem("Right Side", "/home/ui/qrc/laserscan.png"));
ui->treeWidget->addTopLevelItems({ top1, top2 });
ui->treeWidget->addTopLevelItems({ top1, top2 });
const int n_tops = ui->treeWidget->topLevelItemCount();
// Below I am assigning to each parent/children a checkbox
for(int a = 0; a<n_tops; ++a) {
const int n_children = ui->treeWidget->topLevelItem(a)->childCount();
qtreewidgetitem_assign_qcheckbox(ui->treeWidget, ui->treeWidget->topLevelItem(a));
for(int b = 0; b<n_children; ++b) {
qtreewidgetitem_assign_qcheckbox(ui->treeWidget, ui->treeWidget->topLevelItem(a)->child(b));
}
}
QTreeWidgetItem *parentItem = Q_NULLPTR;
QTreeWidgetItem *childItem = Q_NULLPTR;
QMapIterator<QString, QStringList> i(treeMap);
while (i.hasNext()) {
i.next();
parentItem = new QTreeWidgetItem(widget);
parentItem->setText(0, i.key());
foreach (const auto &str, i.value()) {
childItem = new QTreeWidgetItem;
childItem->setText(0, str);
parentItem->addChild(childItem);
}
}
}
3) After more research I cam across this source, and this other source which was useful (in particular the last post) as guidance to the implementation of the subclassing of the QTreeWidget.
Now I am stuck as I am trying to figure out why, despite all the things I tried, I achieved a 90% working application and, therefore, what is missing about it?
Thanks for providing guidance on how to solve this problem.
I think the checkboxes are handling the mouse events and don't let the events go through to the item. To have the desired behavior, you should remove this part:
const int n_tops = ui->treeWidget->topLevelItemCount();
for(int a = 0; a<n_tops; ++a) {
const int n_children = ui->treeWidget->topLevelItem(a)->childCount();
qtreewidgetitem_assign_qcheckbox(ui->treeWidget, ui->treeWidget->topLevelItem(a));
for(int b = 0; b<n_children; ++b) {
qtreewidgetitem_assign_qcheckbox(ui->treeWidget, ui->treeWidget->topLevelItem(a)->child(b));
}
}
And in the createItem() function:
QTreeWidgetItem *MainWindow::createItem(const QString &name, const QString &iconPath)
{
auto *item = new QTreeWidgetItem(QStringList{name});
item->setCheckState(0, Qt::Unchecked);
item->setIcon(0, QIcon(iconPath));
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
return item;
}

Get the coordinates of the QListWidgetItem x y, I want to achieve the effect shown in the figure

My code is as follows. After clicking the item with the mouse, I want a checkbox to be selected and show the background of the specified position and setting the checkbox
Hope someone give a good suggestion and thank you
jobDialog::jobDialog(QWidget *parent) : QDialog(parent), ui(new Ui::jobDialog)
{
ui->setupUi(this);
ui->listWidget->setViewMode(QListView::IconMode);//
ui->listWidget->setIconSize(QSize(300, 300));
ui->listWidget->setSpacing(10);
ui->listWidget->setResizeMode(QListWidget::Adjust);
ui->listWidget->setMovement(QListWidget::Static);
connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this,
SLOT(getItems(QListWidgetItem *)));
int n = 10;
for (int i = 0; i < n; i++) {
QListWidgetItem *newItem = new QListWidgetItem(
QIcon(":/res/images/video.png"), " iamges");
newItem->setSizeHint(QSize(140, 130));
newItem->setTextAlignment(Qt::AlignCenter);;
ui->listWidget->addItem(newItem);
}
}
void jobDialog::getItems(QListWidgetItem *item)
{
for (int i = 0; i < 10; i++) {
if (ui->listWidget->item(i) == item) {
}
}
}
To paint a checkbox on the items you can use a QProxyStyle by enabling the checked property of the item, the status of the checkbox is changed through a delegate:
proxystyle.h
#ifndef PROXYSTYLE_H
#define PROXYSTYLE_H
#include <QPainter>
#include <QProxyStyle>
#include <QStyleOptionViewItem>
class ProxyStyle: public QProxyStyle{
public:
using QProxyStyle::QProxyStyle;
void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const{
QProxyStyle::drawControl(element, opt, p, w);
if(element == QStyle::CE_ItemViewItem){
if(const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)){
if(vopt->checkState == Qt::Checked)
drawCheckBox(*vopt, p);
}
}
}
private:
void drawCheckBox(const QStyleOptionViewItem & option, QPainter *painter) const{
QIcon::Mode mode = QIcon::Normal;
if (!(option.state & QStyle::State_Enabled))
mode = QIcon::Disabled;
else if (option.state & QStyle::State_Selected)
mode = QIcon::Selected;
QIcon::State state = option.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
// load icon
QIcon icon = standardIcon(QStyle::SP_DialogApplyButton);
// or QIcon icon("/path/of/icon");
const int size = 16;
const int margin = 10;
painter->save();
QRect iconRect = QRect(option.rect.bottomRight() - (size + margin) * QPoint(1, 1), size * QSize(1, 1));
icon.paint(painter, iconRect, Qt::AlignCenter, mode, state);
painter->restore();
}
};
#endif // PROXYSTYLE_H
styleditemdelegate.h
#ifndef STYLEDITEMDELEGATE_H
#define STYLEDITEMDELEGATE_H
#include <QEvent>
#include <QMouseEvent>
#include <QStyledItemDelegate>
class StyledItemDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index){
Qt::ItemFlags flags = model->flags(index);
if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
|| !(flags & Qt::ItemIsEnabled))
return false;
QVariant value = index.data(Qt::CheckStateRole);
if (!value.isValid())
return false;
if ((event->type() == QEvent::MouseButtonRelease)
|| (event->type() == QEvent::MouseButtonDblClick)
|| (event->type() == QEvent::MouseButtonPress)) {
if ((event->type() == QEvent::MouseButtonPress)
|| (event->type() == QEvent::MouseButtonDblClick))
return true;
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
if (flags & Qt::ItemIsUserTristate)
state = ((Qt::CheckState)((state + 1) % 3));
else
state = (state == Qt::Checked) ? Qt::Unchecked : Qt::Checked;
return model->setData(index, state, Qt::CheckStateRole);
}
return false; //QStyledItemDelegate::editorEvent(event, model, option, index);
}
protected:
void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const{
QStyledItemDelegate::initStyleOption(option, index);
// disable draw checkbox
option->features &= ~QStyleOptionViewItem::HasCheckIndicator;
}
};
#endif // STYLEDITEMDELEGATE_H
// ...
#include "proxystyle.h"
#include "styleditemdelegate.h"
// ...
jobDialog::jobDialog(QWidget *parent) : QDialog(parent), ui(new Ui::jobDialog)
{
ui->setupUi(this);
ui->listWidget->setViewMode(QListView::IconMode);//
ui->listWidget->setIconSize(QSize(300, 300));
ui->listWidget->setSpacing(10);
ui->listWidget->setResizeMode(QListWidget::Adjust);
ui->listWidget->setMovement(QListWidget::Static);
ui->listWidget->setStyle(new ProxyStyle(ui->listWidget->style()));
ui->listWidget->setItemDelegate(new StyledItemDelegate(ui->listWidget));
int n = 10;
for (int i = 0; i < n; i++) {
QListWidgetItem *newItem = new QListWidgetItem(QIcon(":/res/images/video.png"), "");
newItem->setSizeHint(QSize(140, 130));
newItem->setTextAlignment(Qt::AlignCenter);
newItem->setFlags(item->flags() | Qt::ItemIsUserCheckable);
newItem->setCheckState(Qt::Unchecked);
ui->listWidget->addItem(newItem);
}
}

How to make a custom QItemDelegate for a particular row in a QTreeView?

How to make a custom QItemDelegate like on the attached picture. This is a QTreeView. The last element I want to customize and add a QItemDelegate
For now I have only green separator line and would like to add a QCheckBox below the separator. How to implement this type of behavior?
void SeparatorItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.data(Qt::UserRole + 1).toString() == tr("Unsorted"))
{
QPen pen(Qt::green, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
painter->setPen(pen);
painter->drawLine(option.rect.left(), option.rect.center().y(), option.rect.right(), option.rect.center().y());
}
else
{
QItemDelegate::paint(painter, option, index);
}
}
QSize SeparatorItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.data(Qt::UserRole + 1).toString() == tr("Unsorted"))
{
return QSize(200, 25);
}
return QItemDelegate::sizeHint(option, index);
}
void SeparatorItemDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
editor->setGeometry(option.rect);
}
THE PROBLEM IS:
How to unite a SeparatorLine and a QChekBox in one custom item?
The idea of union in this case is to paint over as I show below:
#include <QApplication>
#include <QItemDelegate>
#include <QPainter>
#include <QStandardItemModel>
#include <QTreeView>
class SeparatorItemDelegate: public QItemDelegate
{
public:
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QItemDelegate::paint(painter, option, index);
if (index.data(Qt::UserRole + 1).toString() == tr("Unsorted"))
{
QPen pen(Qt::green, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin);
painter->setPen(pen);
QLine line(option.rect.topLeft(), option.rect.topRight());
painter->drawLine(line);
}
}
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
if (index.data(Qt::UserRole + 1).toString() == tr("Unsorted"))
return QSize(200, 25);
return QItemDelegate::sizeHint(option, index);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView w;
SeparatorItemDelegate delegate;
w.setItemDelegate(&delegate);
QStandardItemModel model;
for(const QString & root_name: {"Symbols", "Studies", "Drawings", "Unsorted"}){
QStandardItem *root_item = new QStandardItem(root_name);
root_item->setData(root_name);
root_item->setCheckState(Qt::Checked);
model.appendRow(root_item);
for(int i=0; i < 3; i++){
QStandardItem *child_item = new QStandardItem(root_name+QString::number(i));
root_item->appendRow(child_item);
child_item->setCheckState(Qt::Checked);
}
}
w.setModel(&model);
w.expandAll();
w.resize(240, 480);
w.show();
return a.exec();
}

Spanning horizontal header in Qt

I want to merge(span) horizontal headers in QTableWidget. I tried googling for the same, but no luck and hence posting it. Please guide me.
You can subclass QHeaderView and create one section for each of the groups of columns/rows you would like to span and connect signals and slots to have them react to the different columns/rows.
The following example is for spanning the horizontal header:
#include <QtGui>
class MyHeaderModel : public QAbstractItemModel
{
public:
MyHeaderModel(QObject *parent = 0) : QAbstractItemModel(parent) {}
int columnCount(const QModelIndex &parent = QModelIndex()) const { return 2; }
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const { return QVariant(); }
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const { return QModelIndex(); }
QModelIndex parent(const QModelIndex &index) const { return QModelIndex(); }
int rowCount(const QModelIndex &parent = QModelIndex()) const { return 0; }
};
class MyHeader : public QHeaderView
{
Q_OBJECT
public:
MyHeader(QHeaderView *header, QWidget *parent = 0) : QHeaderView(Qt::Horizontal, header), mainHeader(header)
{
setModel(new MyHeaderModel(this));
// This example uses hardcoded groups, you can extend
// this yourself to save the groups
// Group 1 is 0-2 and Group 2 is 3-4
resizeSection(0, getSectionSizes(0, 2));
resizeSection(1, getSectionSizes(3, 4));
connect(this, SIGNAL(sectionResized(int,int,int)), this, SLOT(updateSizes()));
connect(((QTableWidget *)(mainHeader->parentWidget()))->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(updateOffset()));
setGeometry(0, 0, header->width(), header->height());
updateOffset();
mainHeader->installEventFilter(this);
}
public slots:
void updateSizes()
{
setOffset(mainHeader->offset());
mainHeader->resizeSection(2, mainHeader->sectionSize(2) + (sectionSize(0) - getSectionSizes(0, 2)));
mainHeader->resizeSection(4, mainHeader->sectionSize(4) + (sectionSize(1) - getSectionSizes(3, 4)));
}
void updateOffset()
{
setOffset(mainHeader->offset());
}
protected:
bool eventFilter(QObject *o, QEvent *e)
{
if (o == mainHeader) {
if (e->type() == QEvent::Resize) {
setOffset(mainHeader->offset());
setGeometry(0, 0, mainHeader->width(), mainHeader->height());
}
return false;
}
return QHeaderView::eventFilter(o, e);
}
private:
int getSectionSizes(int first, int second)
{
int size = 0;
for (int a=first;a<=second;++a)
size += mainHeader->sectionSize(a);
return size;
}
QHeaderView *mainHeader;
};
#include "main.moc"
int main(int argc, char **argv)
{
QApplication a(argc, argv);
QWidget w;
QVBoxLayout *vbox = new QVBoxLayout;
QTableWidget *tw = new QTableWidget(5, 5);
MyHeader *h = new MyHeader(tw->horizontalHeader());
vbox->addWidget(tw);
w.setLayout(vbox);
w.show();
return a.exec();
}