What is the right way to color-code rows in a QTableView?
I'm developing a spreadsheet application that should color-code its rows based on a specific value set in one of the columns. I use QSqlRelationalTableModel and QSqlRelationalDelegate; because, the value that should determine the color is a foreign key.
Why can't it be as simple as the following? Any ideas?
model->setData( model->index( index.row(), index.column() ),
QBrush(Qt::red),
Qt::BackgroundRole );
You should overwrite the data function of QSqlRelationalTableModel and when you get the Qt :: BackgroundRole role to filter according to your case and return the appropriate QBrush, in the following example filter by the foreign field and check that it is equal to Lima:
Example:
sqlrelationaltablemodel.h
#ifndef SQLRELATIONALTABLEMODEL_H
#define SQLRELATIONALTABLEMODEL_H
#include <QSqlRelationalTableModel>
class SqlRelationalTableModel : public QSqlRelationalTableModel
{
Q_OBJECT
public:
SqlRelationalTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase());
QVariant data(const QModelIndex & item, int role = Qt::DisplayRole) const;
};
#endif // SQLRELATIONALTABLEMODEL_H
sqlrelationaltablemodel.cpp
#include "sqlrelationaltablemodel.h"
#include <QBrush>
SqlRelationalTableModel::SqlRelationalTableModel(QObject *parent, QSqlDatabase db)
:QSqlRelationalTableModel(parent, db)
{
}
QVariant SqlRelationalTableModel::data(const QModelIndex &item, int role) const
{
if(role == Qt::BackgroundRole)
if(QSqlRelationalTableModel::data(index(item.row(), 2), Qt::DisplayRole).toString().trimmed() == "Lima")
return QVariant(QBrush(Qt::red));
return QSqlRelationalTableModel::data(item, role);
}
Output:
The complete example can be found here.
Related
Situation
I have a single Qt model, in particular, a StringModel which derives from QStringListModel called "model".
I have three views, in particular, three QListViews which are called "listView_1", "listView_2" and "listView_3".
All three of these QListViews are set to the one model, that is:
(ui->listView_1)->setModel(model);
(ui->listView_2)->setModel(model);
(ui->listView_3)->setModel(model);
Complication
Whilst I would like all three views to refer to the same model, the data which they show from that model should be slightly different. The function in the model which dictates what data is shown in the view is the "data" member function inherited from QStringListModel and is defined as:
QVariant StringModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()){
return QVariant();
}
if (role == Qt::DisplayRole)
{
int col = index.column();
int row = index.row();
if (col == 0){
QList<Contact*> list = contactList.findChildren<Contact*>();
return list.at(row)->toString();//<<THIS STATEMENT MUST BE VARIABLE
}
}
QVariant v;
return v;
}
Line 12 in the code above returns the data to show on the view and thats the return statement I would like to vary depending on the view.
Question
Model View Controller best practice states that we should be able to keep the model the same and change the views using minor tweaks. Therefore, without defining 3 models for 3 views, how would I tweak my data function to return a varying statement depending on the view that it is set to? Or, summarising in once sentence, how do I adjust a model depending on the view it is set to?
For simple use cases I suggest, that you use QIdentityProxyModel. Below is a small example with two views, where one view displays the strings in a reversed order.
You have to consider careful the two roles Qt::ItemDataRole::EditRole and Qt::ItemDataRole::DisplayRole to make it work seemless.
main.cpp
#include <cmath>
#include <QApplication>
#include <QHBoxLayout>
#include <QStandardItemModel>
#include "ReverseModel.h"
#include <QListView>
#include <QFrame>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
auto frame = new QFrame;
auto model = new QStandardItemModel;
auto view1 = new QListView;
auto view2 = new QListView;
view1->setModel(model);
auto reverseModel = new ReverseModel;
reverseModel->setSourceModel(model);
view2->setModel(reverseModel);
frame->setLayout(new QHBoxLayout);
frame->layout()->addWidget(view1);
frame->layout()->addWidget(view2);
model->appendRow(new QStandardItem("Test"));
frame->show();
return a.exec();
}
ReverseModel.h
#pragma once
#include <QIdentityProxyModel>
#include <algorithm>
class ReverseModel : public QIdentityProxyModel {
Q_OBJECT
public:
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override {
if (role == Qt::DisplayRole || role==Qt::EditRole) {
auto data = QIdentityProxyModel::data(index);
auto string = data.toString();
std::reverse(string.begin(), string.end());
return string;
}
else {
return QIdentityProxyModel::data(index, role);
}
}
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override {
if (role == Qt::EditRole ) {
auto string = value.toString();
std::reverse(string.begin(), string.end());
QVariant reversedValue =string;
return QIdentityProxyModel::setData(index, reversedValue, role);
}
else {
return QIdentityProxyModel::setData(index, value, role);
}
}
};
I am trying to write a QML Gui for a large dynamic C/Fortran simulation. The data I want to display is stored in Fortran Common blocks and updated on fixed time steps. My problem is that QML ListView does not refresh when the dataChanged signal is emitted after each time step, although the signal is received by the Gui (test is in the code below).
I am probably missing out something really obvious because when I flick my ListView down and up again, the displayed data is updated and correct (I guess because the QML engine re-renders elements when they get "out of sight" and back in again). So the only thing that does not work is that the ListView gets updated every time the dataChanged signal is received and not only when it is re-rendered. Below is a more detailed description of my approach and the relevant code parts.
Each simulation entity has several attributes (alive, position...), so I decided to create a ListModel containing a DataObject for each entity. This is the corresponding header file (the actual simulation data is declared as extern structs in "interface.h", so I can access it via pointer):
"acdata.h"
#include <QtCore>
#include <QObject>
#include <QtGui>
extern "C" {
#include "interface.h"
}
class AcDataObject : public QObject
{
Q_OBJECT
public:
explicit AcDataObject(int id_, int *pac_live, double *pac_pos_x, QObject *parent = 0) :
QObject(parent)
{
entity_id = id_;
ac_live = pac_live;
ac_pos_x = pac_pos_x;
}
int entity_id;
int *ac_live;
double *ac_pos_x;
};
class AcDataModel : public QAbstractListModel
{
Q_OBJECT
public:
enum RoleNames {
IdRole = Qt::UserRole,
LiveRole = Qt::UserRole + 1,
PosXRole = Qt::UserRole + 2
};
explicit AcDataModel(QObject *parent = 0);
virtual int rowCount(const QModelIndex &parent) const;
virtual QVariant data(const QModelIndex &index, int role) const;
Q_INVOKABLE Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
void do_update();
protected:
virtual QHash<int, QByteArray> roleNames() const;
private:
QList<AcDataObject*> data_list;
QHash<int, QByteArray> m_roleNames;
QModelIndex start_index;
QModelIndex end_index;
signals:
void dataChanged(const QModelIndex &start_index, const QModelIndex &end_index);
};
Like the header, the .cpp file is also adapted from what you can find in the Qt5 Cadaques Book here, except that my constructor iterates over all simulation entities to set the pointers. Additionally, there is the do_update function that emits the dataChanged signal for the whole list.
"acdata.cpp"
#include "acdata.h"
AcDataModel::AcDataModel(QObject *parent) :
QAbstractListModel(parent)
{
m_roleNames[IdRole] = "entity_id";
m_roleNames[LiveRole] = "ac_live";
m_roleNames[PosXRole] = "ac_pos_x";
for (int i = 0; i < MAX_ENTITIES; i++) // MAX_ENTITIES is defined in interface.h
{
AcDataObject *data_object = new AcDataObject( i,
&fdata_ac_.ac_live[i], // fdata_ac_ is the C struct/Fortran common block defined in interface.h
&fdata_ac_.ac_pos_x[i] );
data_list.append(data_object);
}
}
int AcDataModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return data_list.count();
}
QVariant AcDataModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
if(row < 0 || row >= data_list.count()) {
return QVariant();
}
const AcDataObject *data_object = data_list.at(row);
switch(role) {
case IdRole: return data_object->entity_id;
case LiveRole: return *(data_object->ac_live);
case PosXRole: return *(data_object->ac_pos_x);
}
return QVariant();
}
QHash<int, QByteArray> AcDataModel::roleNames() const
{
return m_roleNames;
}
void AcDataModel::do_update() {
start_index = createIndex(0, 0);
end_index = createIndex((data_list.count() - 1), 0);
dataChanged(start_index, end_index);
}
Qt::ItemFlags AcDataModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) {return 0;}
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
When the simulation is running, do_update() is called every second. I have created a test Gui with a ListView and exposed my model to it with:
Excerpt from "threadcontrol.cpp"
acdata = new AcDataModel();
viewer = new QtQuick2ApplicationViewer();
viewer->rootContext()->setContextProperty("acdata", acdata);
viewer->setMainQmlFile(QStringLiteral("../lib/qml_gui/main.qml"));
viewer->showExpanded();
(This code is part of a larger file that controls the different threads. I am quite sure the rest is not relevant to the actual problem and this question is getting really long...)
So finally there is main.qml. It contains a list with MAX_ENTITIES elements and each elements holds text fields to display my data. I have also added a Connections element to check if the dataChanged signal is received by the Gui.
"main.qml"
ListView {
id: listviewer
model: acdata
delegate: Rectangle {
/* ... some formatting stuff like height etc ... */
Row {
anchors.fill: parent
Text {
/* ... formatting stuff ... */
text: model.entity_id
}
Text {
/* ... formatting stuff ... */
text: model.ac_live
}
Text {
/* ... formatting stuff ... */
text: model.ac_pos_x
}
}
}
Connections {
target: listviewer.model // EDIT: I drew the wrong conclusions here, see text below!
onDataChanged: {
console.log("DataChanged received")
}
}
}
When running the simulation, the "DataChanged received" message is printed every second.
Edit: I was connecting to the ListModel and not to the ListView here, although the ListView has to receive the dataChanged signal. As the console log does not work when connecting to listviewer, I am probably missing the connection between listView and dataChanged signal. However, I think this should work automatically when implementing the dataChanged signal?
Additional information: I have found a similar problem here with Qt Map and it actually seemed to be a bug that was fixed in Qt 5.6. However, running qmake with Qt 5.7 did not fix my problem.
You mustn't declare the dataChanged() signal in your class, because you want to emit the signal AbstractItemModel::dataChanged(). If you re-declare it you add a comleptely new and different Signal that is not connected anywhere. If you remove the declaration in acdata.h everything should work fine.
I'm using this code to query sqlite and put the results in a QTableView.
//MainWindow.cpp
void MainWindow::on_pushButton_clicked()
{
QSqlQueryModel * modal=new QSqlQueryModel();
connOpen();
QSqlQuery* qry=new QSqlQuery(mydb);
qry->prepare("select * from database");
qry->exec();
modal->setQuery(*qry);
//from stack
modal->insertColumn(0);
ui->tableView->setModel(modal);
//from stack
ui->tableView->resizeColumnsToContents();
int p;
for(p=0; p<modal->rowCount(); p++)
{
ui->tableView->setIndexWidget(modal->index(p,0),new QCheckBox());
}
connClose();
qDebug() <<(modal->rowCount());
}
I've seen several examples of the web for adding checkboxes to a column, but I'm not quite sure what to use for my simple example.
This answer suggests a few lines that doesn't seem standard.
There are more examples like this and this one that appear to outline what I need, but it's unclear to where you place the code.
What I intend to do is to have column 1 checkable. On next btn press, If checked those rows of data get written to a file.
I still need to understand how to loop thru the selected data, or perhaps I need to get the ids of the checked rows and do another query.
Questions:
How do you add 1 column of editable checkboxes to QTableView?
How do you loop through values in the QTableView data, so values of the checked rows can be accessed?
How do you check all/none?
I think the best way to have a column of checkable cells is to create your item model, e.g. by subclassing the QSqlQueryModel.
You must reimplement the flags() method to make checkable the cells.
Also you need to reimplement the data() method to return the check state and the setData() method and to set the check state. You must implement your own logic to keep track of the check state of every rows (e.g. using an array of Qt::CheckState that you must initialize and resize when the model data changes).
Yuo can start with something like this:
class MyModel : public QSqlQueryModel
{
public:
Qt::ItemFlags flags(const QModelIndex & index) const
{
if(index.column() == 0)
return QSqlQueryModel::flags(index) | Qt::ItemIsUserCheckable;
return QSqlQueryModel::flags(index);
}
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
{
if(index.column() == 0 && role == Qt::CheckStateRole)
{
//implement your logic to return the check state
//....
}
else
return QSqlQueryModel::data(index, role);
}
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)
{
if(index.column() == 0 && role == Qt::CheckStateRole)
{
//implement your logic to set the check state
//....
}
else
QSqlQueryModel::setData(index, value, role);
}
};
Se also:
Model Subclassing
QAbstractItemModel documentation
To speed-up QComboBox work with very big data set want to try to use QSqlQueryModel instead of QStandardItemModel. However text data in QComboBox I need to map to an ID, which is stored and accessible currently by itemData(rowIndex, Qt::UserRole). In QSqlQueryModel query there will be 2 columns: ID and Text; and QComboBox setModelColumn(1) is defined, i.e. Text.
How to correctly subclass or redefine QSqlQueryModel, if combobox->itemData(rowIndex, Qt::UserRole) have to contain ID? Who had implemented such things or know link to a source? If I define QVariant QSqlQueryModel::data(const QModelIndex & item, int role = Qt::DisplayRole) in a such way:
QVariant MySqlModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::UserRole && index.column() == 1)
return QSqlQueryModel::data(this->index(index.row(), 0), Qt::DisplayRole);
return QSqlQueryModel::data(index, role);
}
will it work, i.e. whether combobox->itemData(rowIndex, Qt::UserRole) will contain ID in this case? Or need to investigate Qt sources?
Yeah, according to QComboBox code should work:
QVariant QComboBox::itemData(int index, int role) const
{
Q_D(const QComboBox);
QModelIndex mi = d->model->index(index, d->modelColumn, d->root);
return d->model->data(mi, role);
}
Will implement this.
I've been working on a simple QTreeView of a local directory. The goal is allow the user to browse to his/her directory and select the correct csv file.
I've created a QFileSystemModel and displayed it with a QTreeView. I'm confused how to get the filename from the currently selected node.
I've read through the documentation and I've found the following signal/slot pairing:
connect(tree, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTreeWidgetEvent(QModelIndex)));
But I'm not sure what to do with the QModelIndex once activated. I know you're supossed to index the QTreeView with this index, but I'm not sure how.
Any help is greatly appreciated.
EDIT: Adding code so people can see what I'm doing.
QFileSystemModel *model = new QFileSystemModel;
model->setRootPath("/");
tree = new QTreeView;
tree->setModel(model);
tree->setRootIndex(model->index("/home/Missions/"));
tree->setColumnWidth(0, 350);
connect(tree, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTreeWidgetEvent(QModelIndex)));
WhatEverClassInheritingQObject::handleTreeWidgetEvent(const QModelIndex& index)
{
const QString valuablePathAskedFor(fileSystemModel->fileName(index));
...
}
you can retrieve the path as a QString in your setData method via filePath() method based just on the QModelIndex, so it will be called each time user has checked (or unchecked) some directory or file in your model being displayed, and then you need to store all these paths in some conatainer and implement method to return this:
bool MyQFileSystemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.column() != 0 || role != Qt::CheckStateRole)
return QFileSystemModel::setData(index, value, role);
int newCheckState = value.toInt();
QString filePath = filePath(index);
if (newCheckState == Qt::Checked || newCheckState == Qt::PartiallyChecked )
checkedPaths.insert(filePath);
else
checkedPaths.remove(filePath);
emit dataChanged(index, index.child(index.row(),0));
return true;
}
class MyQFileSystemModel : public QFileSystemModel
{
Q_OBJECT
public:
//...
QSet<QString> getChecked() const { return checkedPaths; }
private:
QSet<QString> chackedPaths;
//...
};