Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created September 2, 2017 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/de685e311c1ad18dad40b1bef12e1aea to your computer and use it in GitHub Desktop.
Save eyllanesc/de685e311c1ad18dad40b1bef12e1aea to your computer and use it in GitHub Desktop.
#include <QApplication>
#include <QStandardItemModel>
#include <QTreeView>
#include <QDebug>
class TreeView : public QTreeView
{
Q_OBJECT
protected slots:
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected){
Q_UNUSED(deselected)
const auto *m = qobject_cast<QStandardItemModel *>(model());
if(m){
for(const auto index: selected.indexes()){
m->itemFromIndex(index)->setCheckState(Qt::Checked);
}
}
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TreeView w;
QStandardItemModel *model = new QStandardItemModel;
for (int i = 0; i < 4; ++i){
QStandardItem *parent =new QStandardItem(QString("Family %1").arg(i));
for (int j = 0; j < 4; ++j){
QStandardItem *child = new QStandardItem(QString("item %1").arg(j));
parent->appendRow(child);
}
model->appendRow(parent);
}
w.setModel(model);
w.show();
return a.exec();
}
#-------------------------------------------------
#
# Project created by QtCreator 2017-09-01T21:56:52
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QTreeView-CheckBoxOnSelectedItems
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
HEADERS +=
FORMS +=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment