Using QTreeView, how to highlight only specific row/colum with calling function? - c++

I am using C++ Qt5. Currently I have a QStandardItemModel being displayed as a QTreeView with multiple rows and columns. I am aware of using setStyleSheet(), but the issue there is that every row and column that the mouse hovers is highlighted.
I would only like specific rows of the first column to be highlighted, and then have a function called for each cell highlighted that I would then use to manipulate my game.

The solution for a personalized painting is to use a custom delegate, and to indicate which item should change the color a role should be used, in the following code I show an example:
#include <QApplication>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QTreeView>
class StyledItemDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
protected:
void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const{
QStyledItemDelegate::initStyleOption(option, index);
if(index.data(Qt::UserRole +1).toBool())
option->backgroundBrush = QBrush(Qt::red);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeView w;
StyledItemDelegate delegate(&w);
w.setItemDelegate(&delegate);
QStandardItemModel model;
model.setColumnCount(4);
w.setModel(&model);
for(int i=0; i<4; i++){
auto it = new QStandardItem(QString::number(i));
model.appendRow(it);
for(int j=0; j<3; j++){
it->appendRow(new QStandardItem(QString("%1-%2").arg(i).arg(j)));
}
}
QObject::connect(&w, &QTreeView::clicked, [&](const QModelIndex & index){
bool last_state = model.data(index, Qt::UserRole +1).toBool();
model.setData(index, !last_state, Qt::UserRole +1);
});
w.expandAll();
w.show();
return a.exec();
}

Related

How to keep row selected after adding new one in the begining of table?

My model designed to add row in the begining of table. Problem is that if I select second row (for example) selection still will be on this row (second) after adding new row, but original row moved down.
I figured out that QStandardItemModel provides propely behaviour. How to provide same one in my inherited QAbstractTableModel?
Now there is exapmle code for QStandardItemModel. You can select row and see what's going on.
#include <QApplication>
#include <QMainWindow>
#include <QStandardItemModel>
#include <QTableView>
#include <QTimer>
void timeout(QTimer &timer, QStandardItemModel &tblModel)
{
static int step = 0;
tblModel.insertRow(0);
QStandardItem *pItem = new QStandardItem(QString("row %0").arg(step));
tblModel.setItem(0, 1, pItem);
step++;
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow win;
QStandardItemModel tblModel(0, 1);
QTableView tblView;
tblView.setModel(&tblModel);
win.setCentralWidget(&tblView);
win.show();
QTimer timer;
timer.setInterval(1000);
QObject::connect(&timer, &QTimer::timeout,
[&timer, &tblModel]() { timeout(timer, tblModel); });
timer.start();
return app.exec();
}
And simplified version of my model. You can see that in my version selected row doesen't move.
#include <QApplication>
#include <QMainWindow>
#include <QStandardItemModel>
#include <QTableView>
#include <QTimer>
#include <QString>
class MyModel : public QAbstractTableModel
{
public:
MyModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
void insertRow(int data);
private:
QVector<int> table;
};
MyModel::MyModel(QObject *parent)
: QAbstractTableModel(parent) {}
int MyModel::rowCount(const QModelIndex & /*parent*/) const
{
return table.count();
}
int MyModel::columnCount(const QModelIndex & /*parent*/) const
{
return 1;
}
QVariant MyModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if (role == Qt::DisplayRole) {
return table[table.count() - 1 - index.row()];
}
return QVariant();
}
void MyModel::insertRow(int data) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
table.push_back(data);
endInsertRows();
}
void timeout(QTimer &timer, MyModel * tblModel)
{
static int step = 0;
tblModel->insertRow(step);
step++;
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow win;
MyModel * model = new MyModel();
QTableView tblView;
tblView.setModel(model);
win.setCentralWidget(&tblView);
win.show();
QTimer timer;
timer.setInterval(1000);
QObject::connect(&timer, &QTimer::timeout,
[&timer, model]() { timeout(timer, model); });
timer.start();
return app.exec();
}
I didn't find any information in QAbstractTableModel documentation. After that I tried to read QStandardItemModel source code but it's really difficult for understanding.

Drag and drop QStandardItem holding file paths to another application in C++?

I have a treeview which is modeling a file tree. Each item in the TreeView is a QstandardItem that holds a file path. I would like to be able to get the file it referres to and drag the item into another application. The files are all video files so I would like to add the ability to drag and drop into VLC, Adobe Premier etc.
minialistic code :
main.cpp
drag d:/1.txt drop to notepad
#include <QtWidgets/QApplication>
#include <QMimeData>
#include <QTreeView>
#include <QDrag>
#include <QStandardItemModel>
#include<QUrl>
class Myodel :public QStandardItemModel
{
Q_OBJECT
public:
QStringList mimeTypes() const override
{
return QStringList(QLatin1String("text/uri-list"));
}
QMimeData* mimeData(const QModelIndexList& indexes) const override
{
QList<QUrl> urls;
QList<QModelIndex>::const_iterator it = indexes.begin();
for (; it != indexes.end(); ++it)
if ((*it).column() == 0)
urls << QUrl::fromLocalFile("d:/1.txt");
QMimeData* data = new QMimeData();
data->setUrls(urls);
return data;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Myodel* model = new Myodel;
QStandardItem* parentItem = model->invisibleRootItem();
for (int i = 0; i < 4; ++i) {
QStandardItem* item = new QStandardItem(QString("item %0").arg(i));
parentItem->appendRow(item);
parentItem = item;
}
QTreeView* tree = new QTreeView();
tree->setModel(model);
tree->setDragEnabled(true);
tree->show();
return a.exec();
}
#include"main.moc"

Show gridLine only for columns in QTableView

In a QTableView, irrespective if there are entries or not. I want to show grid to it (considering its fixed size QTableView), but only for columns, not for rows.
You have to overwrite the paintEvent() method, in addition to setting the setShowGrid() to false.
#include <QApplication>
#include <QPainter>
#include <QStandardItemModel>
#include <QTableView>
#include <QHeaderView>
#include <QPaintEvent>
class TableView: public QTableView{
public:
using QTableView::QTableView;
protected:
void paintEvent(QPaintEvent *event){
QTableView::paintEvent(event);
if (horizontalHeader()->count() == 0 || verticalHeader()->count() == 0)
return;
QPainter painter(viewport());
QStyleOptionViewItem option;
option.init(this);
const int gridHint = style()->styleHint(QStyle::SH_Table_GridLineColor, &option, this);
const QColor gridColor = static_cast<QRgb>(gridHint);
const QPen gridPen = QPen(gridColor, 0, gridStyle());
painter.setPen(gridPen);
int w = horizontalHeader()->offset();
for(int i=0; i<horizontalHeader()->count(); ++i){
w += horizontalHeader()->sectionSize(i);
painter.drawLine(w-1, 0, w-1,viewport()->height());
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TableView view;
QStandardItemModel model(5, 5);
for(int i=0; i< model.rowCount(); ++i)
for(int j=0; j < model.columnCount(); ++j)
model.setItem(i, j, new QStandardItem(QString("%1-%2").arg(i).arg(j)));
view.setModel(&model);
view.setShowGrid(false);
view.show();
return a.exec();
}

QWidget in QTreeWidgetItem disappearing after reordering the QTreeWidgetItem

I have subclassed QTreeWidget (called it ToolsSelectorWidget) and enabled reordering in it by overriding QTreeWidget::dropEvent()
void ToolsSelectorWidget::dropEvent(QDropEvent *event) {
QModelIndex droppedIndex = indexAt(event->pos());
if( !droppedIndex.isValid() || droppedIndex.parent().isValid()) {
return;
}
QTreeWidget::dropEvent(event);
}
Also, I am adding QWidgets (QPushButton, QLineEdit) to top level items of QTreeWidget:
ToolsSelectorWidget::ToolsSelectorWidget(QWidget *parent) : QTreeWidget(parent) {
header()->hide();
setSelectionMode(QAbstractItemView::SingleSelection);
setDragEnabled(true);
viewport()->setAcceptDrops(true);
setDropIndicatorShown(true);
setDragDropMode(QAbstractItemView::InternalMove);
for(int i=0; i<4; ++i) {
QTreeWidgetItem *part = new QTreeWidgetItem(this);
part->setFlags(part->flags() & Qt::ItemFlag((~Qt::ItemIsDropEnabled)));
setItemWidget(part, 0, new QLabel("Part" + QString::number(i) + " Preview", this));
setItemWidget(part, 1, new QLineEdit("Part" + QString::number(i) + " Name", this));
setItemWidget(part, 2, new QCheckBox("Part" + QString::number(i) + " Visible", this));
setItemWidget(part, 3, new QCheckBox("Part" + QString::number(i) + " Locked", this));
}
}
So now I have 4 top level items each containing 4 QWidgets. It's populating them fine, but when I rearrange them by drag and drop the QWidgets disappear and I end up having an empty row. What should I do to preserve them?
Before:
After Part2 has been moved and is under Part4, it's children have been preserved, but it's conents, which are QWidgets, are gone:
Why are widgets deleted?
When the drag and drop is performed, the data of the selected items is coded (roles and associated values) and saved in a QMimeData. When the drop is accepted, the source items are deleted and new items are created with the information stored in the QMimeData, inside the saved information there is no widgets information since this does not have relation with the model. And since the items are deleted, their widgets are also deleted.
To check it we can use the following example
#include <QApplication>
#include <QLabel>
#include <QTreeWidget>
#include <QDebug>
static void on_destroyed(){
qDebug()<<"destroyed";
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTreeWidget w;
w.setSelectionMode(QAbstractItemView::SingleSelection);
w.setDragEnabled(true);
w.viewport()->setAcceptDrops(true);
w.setDropIndicatorShown(true);
w.setDragDropMode(QAbstractItemView::InternalMove);
for(int i=0; i< 5; i++){
QTreeWidgetItem *it = new QTreeWidgetItem(&w);
QLabel *lbl = new QLabel(QString::number(i));
QObject::connect(lbl, &QObject::destroyed, on_destroyed);
w.setItemWidget(it, 0, lbl);
}
w.show();
return a.exec();
}
It shows that the widgets will emit the signal they destroy when you drag and drop the items.
Possible workaround:
One possible solution is to remove the widgets before accepting the drop and set them in the new items which I have not implemented.
I have explored another solution, it is to change the QTreeWidget for a QTreeView + QStandardItemModel. In the case of the QCheckBox, the checkboxes with the Qt::ItemIsUserCheckable flag are enabled, in the case of the QLineEdit a delegate will be used and to always be shown, the openPersistentEditor() method must be used.
#include <QApplication>
#include <QStandardItemModel>
#include <QTreeView>
#include <QHeaderView>
#include <QDropEvent>
#include <QStyledItemDelegate>
#include <QLineEdit>
class ToolsSelectorDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const{
QLineEdit *le = new QLineEdit(parent);
return le;
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const{
QRect r(option.rect);
r.adjust(2, 2, -2, -2);
editor->setGeometry(r);
}
};
class ToolsSelectorWidget: public QTreeView{
QStandardItemModel model;
public:
ToolsSelectorWidget(QWidget *parent=nullptr): QTreeView(parent){
setItemDelegate(new ToolsSelectorDelegate(this));
setModel(&model);
header()->hide();
setSelectionMode(QAbstractItemView::SingleSelection);
setDragEnabled(true);
viewport()->setAcceptDrops(true);
setDropIndicatorShown(true);
setDragDropMode(QAbstractItemView::InternalMove);
for(int i=0; i<4; ++i) {
QList<QStandardItem *> items;
for(const QString & text: {"Preview", "Name", "Visible", "Locked"}){
QStandardItem *it = new QStandardItem(QString("Part%1 %2").arg(i).arg(text));
it->setFlags(it->flags() & ~Qt::ItemIsDropEnabled & ~Qt::ItemIsEditable);
items.append(it);
if(text == "Visible" || text == "Locked"){
it->setFlags(it->flags() | Qt::ItemIsUserCheckable);
it->setCheckState(Qt::Unchecked);
}
else if (text == "Name") {
it->setFlags(it->flags() | Qt::ItemIsEditable);
}
}
for(const QString & children: {"The", "quick", "Brown", "fox", "jump...", "over", "the", "lazy", "dog"})
items.first()->appendRow(new QStandardItem(children));
model.invisibleRootItem()->appendRow(items);
for( int i = 0; i < model.rowCount(); ++i )
openPersistentEditor(model.index(i, 1));
}
}
protected:
void dropEvent(QDropEvent *event) {
QModelIndex droppedIndex = indexAt(event->pos());
if( !droppedIndex.isValid() || droppedIndex.parent().isValid())
return;
QTreeView::dropEvent(event);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ToolsSelectorWidget w;
w.show();
return a.exec();
}

QT QStandardItemModel segfaulting

I'm pretty new to QT, and I'm trying to take a list from a text file and output it into QT with nice formatting.
I managed to get the list printed on the window, but it has to be able to be sorted.
I have the radio buttons set up right now so that one of them displays the list and the other clears the list.
The problem is that when I switch from the list to the cleared list back to the list the program segfaults and I don't understand why.
The files are here.
winelist.cpp
#include "winelist.h"
#include "ui_winelist.h"
#include <QFile>
#include <QString>
#include <QStandardItemModel>
wineList::wineList(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wineList)
{
ui->setupUi(this);
ui->ratingButton->setChecked(true);
fillList();
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Wine Name")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Vintage")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Rating")));
model->setHorizontalHeaderItem(3, new QStandardItem(QString("Price")));
ui->listOutput->setModel(model);
}
wineList::~wineList()
{
delete ui;
}
void wineList::on_sortButton_clicked()
{
if( ui->ratingButton->isChecked())
{
for (int i = 0; i < 100; i++) {
model->setItem(i,0,wList[i].wineName);
model->setItem(i,1,wList[i].vintage);
model->setItem(i,2,wList[i].rating);
model->setItem(i,3,wList[i].price);
}
}
else
{
for(int i = 0; i < 100; i++) {
for(int j = 0; j < 4; j++) {
model->setItem(i, j, new QStandardItem(QString("")));
}
}
}
ui->listOutput->resizeColumnsToContents();
ui->listOutput->resizeRowsToContents();
}
void wineList::fillList()
{
Wine wine;
QString line;
QStringList lineElements;
QFile wineText(":/winelist.txt");
if (wineText.open(QIODevice::ReadOnly))
{
while ((line = line.fromUtf8(wineText.readLine())) != "")
{
lineElements = line.split(";");
lineElements[0].replace("\t", "");
lineElements[1].replace("\t", "");
wine.wineName = new QStandardItem(QString(lineElements.at(0)));
wine.vintage = new QStandardItem(QString(lineElements.at(1)));
wine.rating = new QStandardItem(QString::number(lineElements.at(2).toInt()));
wine.price = new QStandardItem(QString::number(lineElements.at(3).toInt()));
wList.append(wine);
}
}
wineText.close();
}
winelist.h
#ifndef WINELIST_H
#define WINELIST_H
#include <QMainWindow>
#include <QStandardItem>
#include <QStandardItemModel>
namespace Ui {
class wineList;
}
struct Wine {
QStandardItem* wineName;
QStandardItem* vintage;
QStandardItem* rating;
QStandardItem* price;
};
class wineList : public QMainWindow
{
Q_OBJECT
public:
explicit wineList(QWidget *parent = 0);
~wineList();
private slots:
void on_sortButton_clicked();
private:
Ui::wineList *ui;
QVarLengthArray<Wine> wList;
QStandardItemModel *model = new QStandardItemModel(100, 4, this);
void fillList();
void printList(QStandardItemModel *model);
};
#endif // WINELIST_H
main.cpp
#include "winelist.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
wineList w;
w.show();
return a.exec();
}
Clicking sort the first time
Switching Radio Button and clicking sort again
Switching Radio Button back and clicking sort again
Any Help is appreciated, I am completely lost here.
In the on_sortButton_clicked you're trying to read data from a list, but not doing any range checks. Instead, you've hardcoded 100 there.
You should rewrite this:
for (int i = 0; i < 100; i++) {
model->setItem(i,0,wList[i].wineName);
to this:
for (int i = 0; i < wList.size(); i++) {
model->setItem(i,0,wList[i].wineName);
--upd---
When you initially populate your model, it takes ownership over items from wList. When you replace model items with empty ones, it deletes initial items from wList. After this your wList is no move valid, because it contains Wine structs with dangling pointers. That's why when you try to populate your model second time, it crashes.