QSortFilterProxyModel with QStandardItemModel after appendRow are not working - qt

Here what I have got:
a QTreeView widget (*);
Source model MainModel inherits from QStandardItemModel. No virtual data() const method reimplemented;
Proxy MainFilterProxyModel inherits from QSortFilterProxyModel;
The tree:
[PERIOD 1]
[CHILD 1]
[CHILD 2]
[SUBCHILD 2.1]
...
[CHILD N]
[PERIOD 2]
...
[PERIOD N]
So the main problem comes when I'm trying to add a CHILD-row like (**) code do. Filter proxy model, after document was added to source model, does not know about new row and didn't show it on the tree.
I'm sure that proxy didn't get signal from QStandardItemModel when appendRow method do his job, so proxy can't filter new row, and didn't make it visible.
Any help?
Thanks.
PS: If I turn off proxy, everything appended just fine. But the problem is not in proxy. Proxy just doesn't get the signal about new row appended to the main source model...
(*) Here is QTreeView:
MainView::MainView( QWidget* parent /* = 0 */ ) : QTreeView( parent )
{
if( !model_ )
{
model_ = new MainModel( this );
}
if( !proxy_ )
{
proxy_ = new MainFilterProxyModel( this );
proxy_->setDynamicSortFilter( true );
proxy_->setSourceModel( model_ );
setModel( proxy_ );
}
}
(**) Here is my append function:
void MainModel::addRow( const DocumentPtr& document, QStandardItem* parentItem )
{
assert( document );
QList< QStandardItem* > items;
items << ( new QStandardItem );
items << ( new QStandardItem );
items << ( new QStandardItem );
items << ( new QStandardItem );
items << ( new QStandardItem );
items << ( new QStandardItem );
items << ( new QStandardItem );
updateRow( document, items );
if( !parentItem )
{
BOOST_FOREACH( const TimePeriod& period, TimePeriod::all() )
{
if( period.contains( QDateTime::fromTime_t( document->creationDate() ) ) )
{
QStandardItem* periodItem = itemByPeriod( period );
Q_ASSERT( periodItem );
periodItem->appendRow( items );
break;
}
}
}
else
{
parentItem->appendRow( items );
}
}

The base class for modeling is QAbstractItemModel. It is better to use the methods of the abstract class to do what you want. QStandardItemModel is a simple implementation of the abstract methods of QAbstractItemModel and most of the QStandardItemModel new methods are used by the reimplemented abstract functions. Here is a code to add item and subitem using the abstract class methods:
QAbstractItemModel * pModel = new QStandardItemModel(parent);
int nRows = pModel->rowCount();
pModel->insertRow(nRows); // this will emit rowsAboutToBeInserted() and rowsInserted() signals
pModel->insertColumn(0); // this will emit columnsAboutToBeInserted() and columnsInserted() signals
const QModelIndex indexFirstItem = pModel->index(nRows, 0);
pModel->setData(indexFirstItem, "Item text"); // this will emit dataChanged() signal
int nChildRows = pModel->rowCount(indexFirstItem);
pModel->insertRow(nChildRows, indexFirstItem); // this will emit rowsInserted()
pModel->insertColumn(0, indexFirstItem); // we also need to do this for the item's children
const QModelIndex indexChild = pModel->index(nChildRows, 0, indexFirstItem);
pModel->setData(indexChild, "Child item text");
If we try to do the same using QStandardItemModel methods it will look like:
QStandardItemModel *pModel = new QStandardItemModel(parent);
QStandardItem *pItem = new QStandardItem("Item text");
pItem->appendRow(new QStandardItem); // pItem is not yet added to pModel and rowsInserted won't be emitted
pModel->appendRow(pItem); // this will probably emit rowsInserted() signal but since we set tha text of the item when creating the pItem the dataChanged() signal won't be emitted.
So if you do pItem->appendRow() to add sub items and if the pItem is not yet added to the model, you will probably not get the rowsInserted() signal and therefore the proxy model won't be notified. From my experience the first method works better and is more robust though you need to write a couple of extra lines. Working directly with QStandardItemModel methods will often end with missing signals or other headaches. All you need to look is the QAbstractItemModel and QModelIndex documentations.

Related

How to apply Filter for Tree which is developed using QStandardItemModel

Implemented Tree using QStandardItemModel.. like below
QStandardItem *americaItem = new QStandardItem("America");
QStandardItem *mexicoItem = new QStandardItem("Canada");
QStandardItem *usaItem = new QStandardItem("USA");
QStandardItem *bostonItem = new QStandardItem("Boston");
QStandardItem *europeItem = new QStandardItem("Europe");
QStandardItem *italyItem = new QStandardItem("Italy");
QStandardItem *romeItem = new QStandardItem("Rome");
QStandardItem *veronaItem = new QStandardItem("Verona");
//building up the hierarchy
rootNode-> appendRow(americaItem);
rootNode-> appendRow(europeItem);
americaItem-> appendRow(mexicoItem);
americaItem-> appendRow(usaItem);
usaItem-> appendRow(bostonItem);
europeItem-> appendRow(italyItem);
italyItem-> appendRow(romeItem);
italyItem-> appendRow(veronaItem);
//register the model
treeView->setModel(standardModel);
So now Im unable to do search operation, using that QFilterProxyModel im able to search only parent data.. Any suggestion to search parent and child rows too..(using filterAcceptsRow or any other)
Implemented search filter like this:
Derived a class from QSortFilterProxyModel
Overrided the filterAcceptsRow method
bool FilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
// check the current item
bool result = QSortFilterProxyModel::filterAcceptsRow(source_row,source_parent);
QModelIndex currntIndex = sourceModel()->index(source_row, 0, source_parent);
if (sourceModel()->hasChildren(currntIndex))
{
// if it has sub items
for (int i = 0; i < sourceModel()->rowCount(currntIndex) && !result; ++i)
{
// keep the parent if a children is shown
result = result || filterAcceptsRow(i, currntIndex);
}
}
return result;
}
mFilterProxyModel->setFilterRegExp(ui->mLeSearchFilter->text());

How to hide action button when QLineEdit is disabled?

I have a class which is inherited from QLineEdit, and I set an icon as an action button for this.
MyEdit::MyEdit( QWidget *p_parent ) : QLineEdit( p_parent )
{
m_buttonAction = addAction( QIcon( "search.png" ), QLineEdit::TrailingPosition );
QAbstractButton *button = qobject_cast<QAbstractButton *>( m_buttonAction->associatedWidgets().last() );
m_buttonAction->setVisible( false );
connect( m_buttonAction, &QAction::triggered, this, &MyEdit::openCompleter );
m_completer = new QCompleter( this );
m_sourceModel = new CompleterSourceModel( m_completer );
m_view = new CompleterView();
m_view->setStyle( &m_style );
m_delegate = new CompleterDelegate( m_view );
m_completer->setPopup( m_view );
m_completer->setModel( m_sourceModel );
m_view->setItemDelegate( m_delegate );
setCompleter( m_completer );
}
void MyEdit::setDataForCompleter( const CompleterData &p_data )
{
m_sourceModel->setCompleterData( p_data );
m_buttonAction->setVisible( p_data.data().size() > 0 );
}
When I import data for completer, the icon is always shown. Now I need to hide this icon in case MyEdit is disabled or as ReadOnly.
I am thinking about override setDisabled and setReadOnly for my class, and in there setVisible for the icon. But these functions are not virtual, so can not be overridden.
I am thinking also about a signal like stateChanged of my class, so I can do it in a slot. But I can not find any signal like that for QLineEdit. Do you have any idea how to do it?
You can handle events QEvent::ReadOnlyChange or QEvent::EnabledChange by overriding the QLineEdit::event method
UPDATE:
Here is an example implementation:
bool MyEdit::event(QEvent *e) override {
const auto type = e->type();
if (type == QEvent::ReadOnlyChange || type == QEvent::EnabledChange) {
m_buttonAction->setVisible(m_sourceModel->rowCount() > 0 ? isEnabled() && isReadOnly() : false);
}
return QLineEdit::event(e);
}

QTreeWidget takeChild() - Qt

I have a treeWidget in which I need to remove the QTreeWidgetItem's childelement.
QTreeWidgetItem *listing = new (ui->treeWidget);
AddChild(listing,id);
AddChild(listing,id);`// this is what i need to delete
AddChild(listing,id);
listing->takeChild(1);
I tried func takeChild(int index) but nothing happend
void AddChild(QTreeWidget *s,int id)
{
QTreeWidgetItem *_s = new QTreeWidgetItem(ui->treeWidget);
_s->setText(0,QString::number(id));
s->addChild(_s);
}
There are errors in your code,
QTreeWidgetItem *listing = new (ui->treeWidget);
should be:
QTreeWidgetItem *listing = new QTreeWidgetItem(ui->treeWidget);
and the signature of AddChild() functions is wrong:
void AddChild(QTreeWidget *s,int id)
should be:
AddChild(QTreeWidgetItem *s,int id)
Now the main issue why takeChild() is not working for you, is that you are not adding the items as children to listing , but you are adding them to the treeWidget, thus in your AddChild(), you should set the parent to S which is listing, So your code should be:
QTreeWidgetItem *listing = new QTreeWidgetItem(ui->treeWidget);
listing->setText(0, tr("Listing"));
AddChild(listing,0);
AddChild(listing,1); // this is what i need to delete
AddChild(listing,2);
listing->takeChild(1);
and the function AddChild()
void AddChild(QTreeWidgetItem *s,int id)
{
QTreeWidgetItem *_s = new QTreeWidgetItem(s);
_s->setText(0,QString::number(id));
s->addChild(_s);
}

Specializing a QAbstractProxyModel for adding a column: the table cells becomes empty

I have created a mixin-like proxy model (Qt5) which just adds an extra first column to another proxy model, for adding a QToolBar of actions to each row of the table view (for example, a "delete" button). The model just provides a way of populating a QList<QVariant> for the first column. The delegate must know what is the meaning of each QVariant (usually ints/enums identifying actions), and populate the QToolBar accordingly. As last feature, if there's no actions, no extra column is added (it behaves like a QIdentityProxyModel in that case). Once added, actions cannot be removed. That's a feature for another day.
The problem of today is that, when I insert actions (which I do before setting the model to the view), the cells are all blanks. So, I'm doing something wrong with the signals or who knows with what (I think the mistake is in the add_action function, at the end of the snippet):
template<class proxy_model>
class action_model : public proxy_model
{
QList<QVariant> l_actions;
public:
using base_t = proxy_model;
using base_t::base_t; // Inheriting constructors.
QModelIndex mapFromSource(const QModelIndex& source_idx) const override
{
if (!l_actions.empty() and source_idx.isValid())
return this->createIndex(source_idx.row(),
source_idx.column() + 1);
else // identity proxy case
return base_t::mapFromSource(source_idx);
} // same for mapToSource but with - 1 instead of + 1.
int columnCount(const QModelIndex& parent = QModelIndex()) const override
{ return this->base_t::columnCount() + !l_actions.empty(); }
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
{
if (!l_actions.empty()) {
if (orientation == Qt::Horizontal and section == 0
and role == Qt::DisplayRole)
return "Actions"; // Testing.
else
return base_t::headerData(section - 1, orientation, role);
} else // identity proxy case
return base_t::headerData(section, orientation, role);
}
QVariant data(const QModelIndex& idx, int role) const override
{
if (!l_actions.empty()) {
if (idx.column() == 0 and role = Qt::DisplayRole)
return l_actions; // All the actions for drawing.
else
return QVariant();
} else // identity proxy case
return base_t::data(idx, role);
}
Qt::ItemFlags flags(QModelIndex const& idx) const
{
if (!l_actions.empty() and idx.column() == 0)
return Qt::NoItemFlags; // No editable or selectable
else
return base_t::flags(idx);
}
// And here, I think, is where the fun starts:
// The action could be added before or after the sourceModel
// is set or this model is connected to a view, but I don't
// how that cases are supposed to be managed.
void add_action(QVariant const& action)
{
bool was_empty = l_actions.empty();
l_actions << action;
if (was_empty and !this->insertColumns(0, 1))
throw std::logic_error("Something went wrong");
Q_EMIT this->dataChanged
(this->createIndex(0, 0),
this->createIndex(this->rowCount(), 0),
{ Qt::DisplayRole });
}
};
Without setting actions, the model works fine, both with QAbstractIdentityProxyModel and QSortFilterProxyModel as proxy_model. But, when setting actions, the view shows every cell blank, both with QSortFilterProxyModel and QAbstractIdentityProxyModel.
Here is a user-land code:
enum sql_action { DELETE };
auto* table_model = /* My QSqlTableModel */;
auto* view_model = new action_model<QIdentityProxyModel>(my_parent);
auto* table_view = new QTableView;
view_model->add_action(static_cast<int>(sql_action::DELETE));
view_model->setSourceModel(table_model);
table_view->setModel(view_model);
table_view->setSortingEnabled(true);
table_view->setAlternatingRowColors(true);
// The last column is printed in white, not with alternate colors.
table_view->show();
table_model->select();
The delegates are not a problem because I have set no one. I expect a first column with white cells, but I get an entirely white table. The column names are shown fine, except the last one, which prints just 0 as column name.
What am I doing wrong?
The problem is in your data() method.
You do not compare role to Qt::DisplayRole you assign to role
If you have actions, you either return the action entry or QVariant(), never any data

HowTo find Subitem in QAbstractItemModel and QTreeView class?

Question: how to find sub item, in a QTreeView loaded QAbstractItemModel model with model->match() method?
Problem: model->match() can't find sub items, wtf?!
Here is the example:
As you can see from the picture, I'm trying to expand Layouts sub item with this code:
void Dialog::restoreState(void)
{
// get list
QSettings settings("settings.ini", QSettings::IniFormat);
settings.beginGroup("MainWindow");
QStringList List = settings.value("ExpandedItems").toStringList();
settings.endGroup();
foreach (QString item, List)
{
if (item.contains('|'))
item = item.split('|').last();
// search `item` text in model
QModelIndexList Items = model->match(model->index(0, 0), Qt::DisplayRole, QVariant::fromValue(item));
if (!Items.isEmpty())
{
// Information: with this code, expands ONLY first level in QTreeView
view->setExpanded(Items.first(), true);
}
}
}
Where settings.ini file contains:
[MainWindow]
ExpandedItems=Using Containers, Connection Editing Mode, Form Editing Mode, Form Editing Mode|Layouts
PS: root items successfully expands on start!
Here is the solution:
QModelIndexList Items = model->match(
model->index(0, 0),
Qt::DisplayRole,
QVariant::fromValue(item),
2, // look *
Qt::MatchRecursive); // look *
* Without that argument match() function searches only 1 level
My working example on QTreeView.
QModelIndexList Indexes = this->ui->treeView->selectionModel()->selectedIndexes();
if(Indexes.count() > 0)
{
QStandardItemModel *am = (QStandardItemModel*)this->ui->treeView->model();
QStack<QModelIndex> mis;
QModelIndex mi = Indexes.at(0);
while(mi.isValid())
{
mis.push(mi);
mi = mi.parent();
}
QStandardItem *si;
bool FirstTime = true;
while (!mis.isEmpty())
{
mi = mis.pop();
if(FirstTime)
{
FirstTime = false;
si = am->item(mi.row());
}
else
{
si = si->child(mi.row());
}
}
// "si" - is selected item
}
Wanted to add to the answer that #mosg gave
The forth parameter is actually the hits parameters.
It decides ho many matches one wants to return.
For all matches specify -1 as can be seen
here:
QModelIndexList Items = model->match(
model->index(0, 0),
Qt::DisplayRole,
QVariant::fromValue(item),
-1, // any number of hits
Qt::MatchRecursive); // look *

Resources