Hide QStandardItemModel row in data member function - qt

How can I hide / exclude entire rows from a QStandardItemModel without removing them physically from the model?
Rational: I want to not display entities which are shown in another view. I already have a working highlight logic for that in the data function. Could I turn it(easily) in a "not display logic", so such rows are skipped?
QVariant CMyModel::data(const QModelIndex &index, int role) const
{
if (role != Qt::BackgroundRole) { return CModelBase::data(index, role); }
.......
if (model.hasSomeCondition())
{
static const QBrush b(Qt::green);
return b;
}
return QVariant();
}

Related

How to disable or shorten the tooltip delay in QTableView?

We are using a QTableView with a custom QAbstractTableModel. Some data is too long to display in the cells directly or we would like to show additional information.
In the model we use the following code:
QVariant MyTableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
return "Short Content";
}
if (role == Qt::ToolTipRole) {
return "Super long content which contains line\nbreaks, tabs\t and more.";
}
return QVariant();
}
This works and when hovering the cells the tooltip is displayed. However, this takes a few seconds for the tooltip to appear and sometimes some mouse-wiggling.
Is there a built-in way in Qt 5 to disable the timeout and always display the tooltip?
As they point out in the Qt forum the delay depends on the style and is returned through SH_ToolTip_WakeUpDelay which is 700 ms by default.
Considering the above, a possible solution is to use the QProxyStyle method override:
class ProxyStyle : public QProxyStyle
{
public:
using QProxyStyle::QProxyStyle;
int styleHint(StyleHint hint, const QStyleOption* option = nullptr, const QWidget* widget = nullptr, QStyleHintReturn* returnData = nullptr) const override
{
if (hint == QStyle::SH_ToolTip_WakeUpDelay)
return 0;
return QProxyStyle::styleHint(hint, option, widget, returnData);
}
};
tableview->setStyle(new ProxyStyle(tableview->style()));

Qt does not update several QTableView cells when editing in the code

I have a simple logic, that basically goes like this: a few entries are filtered from source model (my custom QAbstractTableModel) and presented to the user using QSortFilterProxyModel, which does have only modified filterAcceptsRow function. This presentation is done using simple dialog. User selects desired entries from filtered ones and those selected entries from model must be updated (actually two fields have to be modified). So simplified code goes like this:
QModelIndexList selectedRows = myProxyModel->selectionModel()->selectedRows();
for (int i = 0; i < selectedRows.count(); i++) {
myProxyModel->setData(myProxyModel->index(selectedRows.at(i).row(), (int) LoanStatusCol, QModelIndex()), (int) ReturnedLoan, Qt::EditRole);
myProxyModel->setData(myProxyModel->index(selectedRows.at(i).row(), (int) LoanRetEntriesCol, QModelIndex()), (lastEntryNo + 1), Qt::EditRole);
}
However, this does not work. And each time behavour is quite weird. What I noticed is that, when it gets to second selected row in this cycle and when it reaches setData() code in the model:
bool TransactionModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if (!index.isValid()) {
return false;
}
it returns invalid index. However, when I swaped these two setData() code lines, one row was updated, but second row was not - due to invalid index. I do not know, whether I explained that correctly, but probably this should be my silly mistake, because I am new at this.
UPDATE:
Since model consists of QList data, where Transaction is a custom class that defines fields of entry, I created a function, that updated underlying entry by column number (so to say...). I use function setValueByColumnNo. I just could not find a better way to do that, when working with lists of custom classes.
bool TransactionModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if (!index.isValid()) {
return false;
}
if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
transactionData[index.row()].setValueByColumnNo(index.column(), value);
emit dataChanged(index, index);
return true;
}
return false;
}
Any ideas?
Thanks.

using QList<QStringList> to populate a QTableView. When something gets changed in the View, how do I get it back in the data?

I have a matrix of data, I simply stored it as a QList of QStringLists, all containing an equal number of QStrings. In this way, the data looks almost like a spreadsheet.
I use a QTableView to present this data to the user:
void DialogwitQTableView::setData(QList<QStringList> s)
{
/* Create the data model */
// 1. give it some headers
QStandardItemModel model = new QStandardItemModel(s.count(),25,this); //x Rows and 25 Columns
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Column 1")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Column 2")));
// ...
model->setHorizontalHeaderItem(24, new QStandardItem(QString("Column 25")));
// 2. populate the model with the data
for(int i = 0; i < s.count() ; i++)
{
for(int j = 0; j < s[i].count() ; j++)
model->setItem(i,j,new QStandardItem(QString(s[i][j])));
}
ui->NameOfTheTableView->setModel(model);
}
Now, if the user wants to change some of the data, he will just doubleclick in the QTableView in the Dialogbox and edits what he wants.
How do I get that edit also in the data? How can I adapt the QStringList with that new information?
If I search for documentation, I mostly find QTableViews linked to databases, but I don't see how this will work with a simple datastructure in memory. If I go to QtDesigner and click on "go to slots" for the TableView, I also do not see a slot called "datachanged" or anything similar.
Any thoughts? I feel pretty stuck and I am probably overviewing something, any tip is very welcome.
Looking at the doco, a QTableView inherits 6 signals from QAbstractItemView
http://doc.qt.digia.com/qt/qabstractitemview.html#signals
This class has all sorts of functionality for capturing edits, and edit triggers.
Once you can catch when the data is changed you can recommit it back to your model if you are using an MVC view. I am sure there are a lot of examples.
Hope that helps.
I think that for more complicated cases it's always best to use the abstract classes, more specifically QAbstractTableModel in this case.
Looking at this file, I just replaced Contact with StringList and changed the getters and setters. Check it out:
https://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-tablemodel-cpp.html
TableModel::TableModel(QObject *parent) :
QAbstractTableModel(parent)
{
}
TableModel::TableModel(QList<QStringList> stringLists, QObject *parent) :
QAbstractTableModel(parent),
stringLists(stringLists)
{
}
int TableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return stringLists.size();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2; // put the amount of columns here
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
if (index.row() >= stringLists.size() || index.row() < 0) return QVariant();
if (role == Qt::DisplayRole) {
const auto &strings = stringLists.at(index.row());
if (index.column() == 0)
return strings[0];
else if (index.column() == 1)
return strings[1];
}
return QVariant();
}
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("String 1");
case 1:
return tr("String 2");
default:
return QVariant();
}
}
return QVariant();
}
bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
stringLists.insert(position, { QString(), QString() });
endInsertRows();
return true;
}
bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
stringLists.removeAt(position);
endRemoveRows();
return true;
}
bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
int row = index.row();
auto strings = stringLists.at(row);
if (index.column() == 0)
strings[0] = value.toString();
else if (index.column() == 1)
contact[1] = value.toString();
else
return false;
stringLists.replace(row, contact);
emit dataChanged(index, index, {role});
return true;
}
return false;
}
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) return Qt::ItemIsEnabled;
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
QList<QStringList> TableModel::getStringLists() const
{
return stringLists;
}
I also highly recommend that you read this:
https://doc.qt.io/qt-5/modelview.html
Hope it helps.
If I search for documentation, I mostly find QTableViews linked to
databases, but I don't see how this will work with a simple
datastructure in memory.
QTableView is a part of Qt's Model/View framework. There are bunch of examples of model-views.
How do I get that edit also in the data? How can I adapt the
QStringList with that new information?
At least these solutions exists:
You can grab all data from QStandardItemModel via item method.
Connect to QStandardItemModel::itemChanged signal.
You can make your own model via subclassing (and I suggest to base on QAbstractTableModel) and implement several methods (data, setData + several utility methods).

QTreeview : How to change the defalut position of the text item

Could you please let me know how to chage the position of the item in QTreeView.By default the item displayed at Left most and in the center of the item box.But how should i change it so that it will display in top
Using a Qt built-in item model
If you are using e.g. QFileSystemModel you have to inherit from it and override the data() behaviour:
class MyFileSystemModel : public QFileSystemModel {
public:
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const {
if (role == Qt::TextAlignmentRole)
return Qt::AlignTop; //maybe different result depending on column/row
else
return QFileSystemModel::data(index, role);
}
and then use that class instead.
Using own item model
If you implemented your own item model all you have to do is handle Qt::TextAlignmentRole in data():
QVariant MyTreeModel::data (const QModelIndex &index, int role) const {
if (role == Qt::TextAlignmentRole)
return Qt::AlignTop; //maybe different result depending on column/row
//handle other roles
return QVariant();
}
The tree view should now automatically align the items to the top.
If you want to customize the appearance even further, here are the roles that are used by QTreeView. For more customization I think you have to implement your own QTreeView subclass.
Using QStandardItemModel
If you did not implement your own model but used QStandardItemModel you have to call
setTextAlignment(Qt::Alignment alignment) with Qt::AlignTop on your standard items before adding them to the model.

How to add custom row in QFileSystemModel?

I am using QFileSystemModel to represent file structure through the QTreView. Everything works fine, but I need to add an additional row at some level of the tree. For example for now is:
-root
--row1
--row2
--row3
All these rows mapping folders/files from file system.
I need:
-root
--row1
--row2
--row3
--custom row
So custom row is not representing any data from file system. I just need to add here my own data.
I have read a lot of stuff from the internet and people advice to use proxy model and reimplement rowCount(), data() and flags() functions. I tried to do that(used class derived from QSortFilterProxyModel), but I never get my row in data() and flags() functions. Seems like it takes count from source model.
QVariant AddonFilterModel::data (const QModelIndex & index, int role) const
{
if(role == Qt::DisplayRole && index.row() == FilterModel::rowCount(index))
{
return QString("Add-Ons");
}
return FilterModel::data(index, role);
}
Qt::ItemFlags AddonFilterModel::flags(const QModelIndex & index) const
{
if (!index.isValid())
return 0;
if (index.row() == FilterModel::rowCount(index))
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
return FilterModel::flags(index);
}
int AddonFilterModel::rowCount(const QModelIndex &parent) const
{
int count = FilterModel::rowCount(parent);
if(parent == this->getRootIndex())
{
return count+1;
}
return count;
}
Using class derived from QAbstractProxyModel is not acceptable because I need filtering functions of QSortFilterProxyModel().
Also I have tried to reimplement rowCount() of QFileSystemModel to make changes directly in model but I am getting "array out of range" error from QT code.
I have tried insertRow() method but it is not working. I think because QFileSystemModel is read only.
Did anyone face this problem? Any ideas?
Late answer. You have to subclass Qabstractitemmodel.

Resources