How to use pressed() and selectionChanged together?
-
Hi All
I have following codeQTableView * table ; QItemSelectionModel* qsm = table->selectionModel(); if (qsm) { QObject::connect(qsm, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),this, SLOT(itemselectionChanged())); } // this line is require when I click the already selected row QObject::connect(table SIGNAL(pressed(const QModelIndex&)),this, SLOT(itemselectionChanged()));
Now my question is there is some problem in this code itemSelectionChanged is called twice when we change the selection in table .one beacause of signal selectionChanged on selectionmodel and othet becasue of pressed
The solution to avoid the two calls is like
mQtableView table = new QtableView; mQTableView * table ; QItemSelectionModel* qsm = table->selectionModel(); if (qsm) { QObject::connect(qsm, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),this, SLOT(itemselectionChanged())); } // this line is require when I click the already selected row QObject::connect(table SIGNAL(pressed(const QModelIndex&)),this, SLOT(itempresse()));
When we call
mQtableView:: handleSelection() { } mQtableView:: itemselectionChanged() { selectChangeFlag = true handleSelection } mQtableView::itempressed() { if (selectionChanged) return else handleSelection(); }
-
Hi
Am not sure why you need pressed signal also.
First time u click item, selectionChanged is fired and
you call handleSelection.If you then click on the the current selected item again, why
do you then need to call handleSelection again?
Since it was done in selectionChanged on the first click ? -
When we call handleSelection in window W0, we select objects in other window w1 the scenario I need to emit itempressed is
when we deselect the objects in w1 , by clicking on an empty area in W1 , now I go again select in W0 , THE OBJECT IS not selected in W1 , since selectedChanged signal is not emittted that it is why i need to connect a slot for pressed
-
@Qt-Enthusiast Although this is an old post, I dare to reply to it, since it is still marked as unsolved and since I faced the same problem (I wanted to run the row selection handling code even when the user clicks on an already selected row).
You could use a flag, e.g.
m_itemClicked
as follows, using the fact that theclicked()
event is emitted AFTER theselectionChanged()
event. I'll use aQTreeView
here.First, install an event filter on your
QTreeView
:void MainWindow::setup() { ... m_treeView->viewport()->installEventFilter(this); }
In the event filter, set the
m_itemClicked
flag when the viewport is clicked:bool MainWindow::eventFilter(QObject *obj, QEvent *event) { const QEvent::Type type = event->type(); if (type == QEvent::MouseButtonPress) { QWidget* w = qobject_cast<QWidget*>(obj); QMouseEvent* mouseEvent = static_cast <QMouseEvent *> (event); Qt::MouseButton button = mouseEvent->button(); if (button == Qt::LeftButton) { if (w == m_treeView->viewport() { m_itemClicked = true; } } ... QMainWindow::event(event); } }
Then, in the selectionChanged() handler, do something like this:
void MainWindow::onRowSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) { Q_UNUSED(deselected); if (selected.indexes().length()>0) { const QModelIndex index = selected.indexes()[0]; if (index.isValid()) handleRowSelection(index); //Avoid double execution of handleRowSelection() by the click handler if (m_itemClicked) m_itemClicked = false; } //if (selected.indexes() }
The above will thus execute
handleRowSelection()
both upon selection change by key navigation, but also when clicking on a different row (because that also changes the selection). To handle the case where the user clicks on the same row (i.e. the selection does not change), you can do the following in the click handler:void MainWindow::on_treeView_clicked(const QModelIndex &index) { //This means that handleRowSelection() was already //executed by onRowSelectionChanged() if (!m_itemClicked) return; handleRowSelection(index); m_itemClicked = false; }
-
to add my 2cent here, I'd probably just simplify it using a "isScheduled" variable:
private: bool m_isSelectionScheduled = false; Q_SLOT void scheduleItemSelection(){ if(m_isSelectionScheduled) return; m_isSelectionScheduled=true; QTimer::singleShot(0,this,[=]()->void{itemselectionChanged(); m_isSelectionScheduled=false;} };
then in the constructor
// assuming this is of type MyClass* QObject::connect(qsm,&QItemSelectionModel::selectionChanged,this,&MyClass::scheduleItemSelection); QObject::connect(table,&QTableView::pressed,this, &MyClass::scheduleItemSelection);