Solved model populated signal
-
i have a
QTableVIew
subclass andQAbstractTableModel
subclass.i need to know when data is available in order to enable gui actions. the widget doesn't have any such signal but the model doesn't seem to have one either.
i suppose that should be possible?
-
@user4592357 said in model populated signal:
i need to know when data is available
Could you provide a small example of what you mean?
-
table = new TableView(); table->setModel(new TableModel); // ... // here i need some way to be able to know when data is put into model connect(table, /* some signal */, [this] { ( /* update gui */) });
-
like
QAbstractItemModel::dataChanged
? -
@VRonin
is that the correct one to use?it didn't activate the slot
-
You were really vague on what your needs are but that is the one closer to your description.
QTableView
already uses that signal to know when to repaint so you could do the same -
@user4592357 said in model populated signal:
it didn't activate the slot
That means you didn't implement the model correctly, see http://doc.qt.io/qt-5/qabstractitemmodel.html#subclassing it's not easy :)
-
@VRonin
do you mean emittingdataChanged()
? my model isn't editable so i don't need it -
The fact the is editable or not is meaningless, if any change in your model should trigger a repaint in the view you should emit that signal
-
@VRonin
still no luck :( -
Can you post your model implementation?
-
@VRonin
sorry it's python. nothing specific to it, though.class TableModel(QAbstractTableModel): headers = [/* items */] def __init__(self, parent=None): super(TableModel, self).__init__(parent) self.__data = [] def rowCount(self, parent=QModelIndex()): del parent return len(self.__data) def columnCount(self, parent=QModelIndex()): del parent return len(self.headers) def data(self, index, role=Qt.DisplayRole): if not index.isValid() or \ not (0 <= index.row() < self.rowCount()) or \ not (0 <= index.column() < self.columnCount()): return QVariant() if role == Qt.DisplayRole: return self.__data[index.row()][index.column()] if role == Qt.UserRole: return self.__data[index.row()][0] return QVariant() def headerData(self, section, orientation, role): if role != Qt.DisplayRole: return QVariant() if orientation == Qt.Horizontal: return self.headers[section] return section + 1 def set_data(self, data): self.beginResetModel() del self.__data[:] # populate self.__data self.endResetModel() self.dataChanged.emit(self.index(0, 0), self.index(self.rowCount() - 1, self.columnCount() - 1))
-
You are using reset model so you can connect to
QAbstractItemModel::modelReset
-
@VRonin
that's what i tried firstactually, the first time model is populated, the slot isn't invoked, but when it's populated 2nd time, the slot is invoked (with both signals)
-
@user4592357 said in model populated signal:
the slot isn't invoked
void QAbstractItemModel::endResetModel() { Q_D(QAbstractItemModel); d->invalidatePersistentIndexes(); QMetaObject::invokeMethod(this, "resetInternalData"); emit modelReset(QPrivateSignal()); }
-
@VRonin
yeahanother weird thing: the 2nd time it's invoked, 3rd time it's invoked twice, 4th time - thrice, etc...
-
Where are you connecting the signal? looks like you are connecting every time it's invoked
-
@VRonin
my bad, right...how didn't i realize? thanks