how to use pressed() and selectionchanged together



  • Hi All
    I have following code

    QTableView * 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();
    }


  • Qt Champions 2016

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.