Stop a signal from firing from code!
-
this
void MainWindow::on_tableWidgetDeviceList_cellChanged(int row, int column) { }
gets fired when i populate the
tableWidget
within code! i ONLY want to fire this off when a USER changes a cell text! how?i googled and found this
ui->tableWidgetDeviceList->blockSignals(true);
but did not work
-
@mrjj ty :) got it working like this
void cellwasChanged(int row, int column); connect(ui->tableWidgetDeviceList, &QTableWidget::cellChanged, this, &MainWindow::cellwasChanged); void MainWindow::cellwasChanged(int row, int column) { qDebug() << "Row : " << row << "Column : " << column; }
but why is it when using the auto
goto slot -> cellChanged
code triggers that slot but doing it manu it doe not :S -
Hi
Best way to fix this is not to use the auto connect feature.
(GoOto Slot, and name on_tableWidgetDeviceList_cellChanged)and simply use manual connect staement.
Then you first fill the table.
then you connect the slots.No issues and better desgin. :)
-
@mrjj ty :) got it working like this
void cellwasChanged(int row, int column); connect(ui->tableWidgetDeviceList, &QTableWidget::cellChanged, this, &MainWindow::cellwasChanged); void MainWindow::cellwasChanged(int row, int column) { qDebug() << "Row : " << row << "Column : " << column; }
but why is it when using the auto
goto slot -> cellChanged
code triggers that slot but doing it manu it doe not :S -
- but why is it when using the auto goto slot -> cellChanged code triggers that slot but doing it manu it doe not :S
That is simply the order.
Auto connected is run in setupUI() and you fill afterwith manual connect you fill first, then connect.
So as non magical as it gets . :)
-
@Kris-Revi
Yep! More powerful, less gotchas. That's just how it is. See how we are having a similar conversation at https://forum.qt.io/topic/128677/no-documents-matching-ui_form-h-could-be-found/9, with same recommendation though for specific gotcha-reason there. -
For people ending up here from Google:
If you want a signal to be sent only when the user edits the cell and not when it's done by internal processes the correct way is a simple custom delegate:class EditSignalDelegate : public QStyledItemDelegate { Q_OBJECT public: using QStyledItemDelegate::QStyledItemDelegate ; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override { QStyledItemDelegate::setModelData(editor,model,index); indexEdited(index); } signals: void indexEdited(const QModelIndex &index); };
Now you can use
EditSignalDelegate* delegate = new EditSignalDelegate(this); connect(delegate,&EditSignalDelegate::indexEdited,this,[](const QModelIndex &index){qDebug() << "cell edited " << index.row() << "," <<index.column();}); ui->tableWidgetDeviceList->setItemDelegate(delegate);