How to get the selectionchange event in Qt - qt

I have a class inherited from QWidget, now in that class I will be creating aQListView object and filling up the items to view.
When the selection of items in the list view gets changed, I want to get the selectionChange event.
How can I achieve this?. Please tell me in brief.

When you have a view, you will have a model that will be used to select item. It's called a QItemSelectionModel.
For example, with your QListView, you can get the selectionModel this way :
QItemSelectionModel* selectionModel() const;
Now, from that model, you'll be able to connect on many signals :
void currentChanged ( const QModelIndex & current, const QModelIndex & previous )
void currentColumnChanged ( const QModelIndex & current, const QModelIndex & previous )
void currentRowChanged ( const QModelIndex & current, const QModelIndex & previous )
void selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected )
I think it will help you a bit!

https://doc.qt.io/archives/qt-4.8/qlistwidget.html You might want to use QListWidget instead of view, I don't remember specifics why, but this class has these signals you want to use.
https://doc.qt.io/archives/qt-4.8/qlistwidget.html#itemSelectionChanged
This is the signal you have to connect to.
Make a slot in your class declaration:
private slots:
void selChanged();
Fill this slot with what you want to do upon selection change.
Connect the signal to this slot somewhere in your class - perhaps in the constructor of your QWidget derivative.
connect(yourListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(selChanged()));
that's it

Related

itemChanged() on QStandardItemModel triggered when data hasn't changed

I have the following connect line:
connect(my_QStandardItemModel ,SIGNAL(itemChanged(QStandardItem*)),
this,SLOT(cellEditEndedCalled(QStandardItem*)));
For some reason whenever I go into edit mode on a cell inside my table (double click) and click on another cell, cellEditEndedCalled() is being called even though I didn't make any changes to my data.
Any ideas on why this might be happening?
EDIT:
Tried with dataChanged(...) instead of itemChanged(...) but the slot is still being called.
Implementation of my_QStandardItemModel :
class my_QStandardItemModel :public QStandardItemModel
{
typedef QStandardItemModel baseClass;
Q_OBJECT
public:
my_QStandardItemModel ();
virtual ~my_QStandardItemModel ();
...
Not overwriting any signals afterwards.
Because the signal itemChanged is not the correct for your situation. QStandardItemModel inherits another singal form QAbstractItemModel
void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const
QModelIndex &bottomRight, const QVector<int> &roles = QVector<int> ())
that emits with QModelIndex information on the index where changes occured : your cell.
you need to connect that signal to your slot (to be modified to match new signal signature).
Why the itemChanged signal is emitted even you did not modify the data: because that signal emits when you change your item NOT the data in it.

Displaying a tooltip for QTreeWidgetItem when it's hovered without calling setTooltip() for every item

I want to display a tooltip for QTreeWidgetItem that's hovered. However, getting a tooltip is not a very fast process in my case, so I don't want to call setTooltip() for every single item. I want to do it on demand, on some event or signal. What's the easiest way to do it?
The best solution I've found is to subclass QTreeWidgetItem, override virtual QVariant data(int column, int role) const; and return a tooltip for this item when data is called for Qt::ToolTipRole.
I think that it should be easier to achieve what you want if you migrate to a QTreeView/Model pattern.
QAbstractItemModel has a role for tooltips: Qt::ToolTipRole
You could subclass a Model to reimplement the
QVariant QAbstractItemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) const [pure virtual
method.
So, when receives a Qt::TooltipRole, it calculates/recovers from an internal cache.

updating the selection model and proxy model after insertion into a custom model in qt

I have a model in which i will be adding symbols dynamically.
I am using a proxy model and a selection model for linking with the view
Every time i add a symbol to my source model the proxy model gets updated but the selection model i set for the view gets screwed up.
It doesn't recognize any selections anymore
can someone tell me why ??
void SymbolViewer::on_openButton_clicked()
{
this->selModel = ui->symbolListView->selectionModel();
...
}
i set the selection model each time my i click a button to work on the selection.
Im sure you re trying to access your model data with the indexes given by the selection model. But selection model will return indexes relative to your view's model. And It is the proxy. So to access data of your model you must map it:
e.g.:
Consider you have a signal on current item change:
connect( p_selectionModel,
SIGNAL( currentChanged(const QModelIndex &, const QModelIndex &)),
this,
SLOT(viewCurrentChanged(const QModelIndex &, const QModelIndex &)));
On your slot:
viewCurrentChanged(const QItemSelection & selected, const QItemSelection & deselected)
selected will be a QModelIndex of your proxy. You could access to data through
selected.data() ..
But if your accessig this way:
your_model->data( selected, role )
Your accessing your model with a proxy index, that will fail.
You should do it this way:
your_model->data( proxy_model->mapToSource(selected) , role )
( mapToSource(...) )
If your working with a QSelection, ( as in selectionChanged(const QItemSelection & selected, const QItemSelection & deselected) signal ) use
mapSelectionToSource()
To do the reverse path, use:
QItemSelection QAbstractProxyModel::mapFromSource(const QModelIndex & sourceIndex) const
QItemSelection QAbstractProxyModel::mapSelectionFromSource(const QItemSelection & sourceSelection)
And dont set the selection model on that button slot! It makes no sense. It will be always the same.

QTableView cell data disapears when the cell is activated

I'm developing an application using QT5.0 and new to QT. Badly, i have not too much time for a long learning curve.
I have derived my own TableModel and set it to a editable TableView. TableView shows model's data, it works. But when i activate a cell on the tableview, the data disappears. I looked at the documentation and saw that QTableView is derived from QAbstractItemView class which have a signal called 'activated' and a slot called 'edit'. So, i think 'activated' signal is connected to 'edit' slot. But 'edit' is not virtual, so i can not override it. I may connect my child class to parents 'activated' signal but actually i do not know how to handle this signal in order to save the current data of the TableView object.
There is no problem if the code uses SqlTableModel. I think it handles the 'activated' signal but I'm not sure about these, just speculating..
What is the right way to do this?
Check your the data function:
QVariant TableModel::data(const QModelIndex &index, int role) const
if( !index.isValid() )
return QVariant();
if( role == Qt::DisplayRole || role == Qt::EditRole) {
return <your data>
}
return QVariant();
}
Ensure that you process the EditRole role.
Good luck!

Can't use itemFromIndex in clicked signal of QTreeView with QSortFilterProxyModel

I have a QTreeView with a QSortFilterProxyModel between the view and a QStandardItemModel to sort the tree. I then want to act on clicks in the view through the clicked() signal.
The models/view are setup similar to this:
mymodel = new QStandardItemModel(5, 5, this);
mysort = new MySortProxy(this);
mysort->setSourceModel(mymodel);
myview = new QTableView(this);
myview->setSourceModel(mysort);
connect(myview, SIGNAL(clicked(QModelIndex)), this, slot(clickAction(QModelIndex)));
This setup all works and sorts my data in the way I want it. When you click on an item, the clickAction() slot gets called with the index of the item clicked. I then try to get the item from the index in the slot:
void myclass::clickAction(const QModelIndex &index)
{
QStandardItem *item = mymodel->itemFromIndex(index);
}
However, itemFromIndex returns NULL.
If I remove the QSortFilterProxyModel and set the model directly as sourcemodel in the view, it all works perfectly. I.e.
myview->setSourceModel(mymodel); // was setSourceModel(mysort);
mymodel->itemFromIndex(index) now returns the item as expected, but obviously now I can't use my own sort proxy.
Can anyone tell me what I'm doing wrong and how I can get the item in the click slot when I have a sortfilter proxy in place?
I'm using Qt-4.3.1.
Thanks for any help, Giles
I believe you want to do something like:
void myclass::clickAction(const QModelIndex &index)
{
QStandardItem *item = mymodel->itemFromIndex(mysort->mapToSource(index));
}

Resources