Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved model populated signal

    General and Desktop
    2
    18
    1839
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • U
      user4592357 last edited by

      i have a QTableVIew subclass and QAbstractTableModel 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?

      V 1 Reply Last reply Reply Quote 0
      • V
        VRonin @user4592357 last edited by

        @user4592357 said in model populated signal:

        i need to know when data is available

        Could you provide a small example of what you mean?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        U 1 Reply Last reply Reply Quote 0
        • U
          user4592357 @VRonin last edited by

          @VRonin

          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 */) });
          
          1 Reply Last reply Reply Quote 0
          • V
            VRonin last edited by

            like QAbstractItemModel::dataChanged?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            U 1 Reply Last reply Reply Quote 0
            • U
              user4592357 @VRonin last edited by user4592357

              @VRonin
              is that the correct one to use?

              it didn't activate the slot

              V 1 Reply Last reply Reply Quote 0
              • V
                VRonin last edited by

                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

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply Reply Quote 0
                • V
                  VRonin @user4592357 last edited by

                  @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 :)

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  U 1 Reply Last reply Reply Quote 0
                  • U
                    user4592357 @VRonin last edited by

                    @VRonin
                    do you mean emitting dataChanged()? my model isn't editable so i don't need it

                    1 Reply Last reply Reply Quote 0
                    • V
                      VRonin last edited by

                      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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      U 1 Reply Last reply Reply Quote 0
                      • U
                        user4592357 @VRonin last edited by

                        @VRonin
                        still no luck :(

                        1 Reply Last reply Reply Quote 0
                        • V
                          VRonin last edited by

                          Can you post your model implementation?

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          U 1 Reply Last reply Reply Quote 0
                          • U
                            user4592357 @VRonin last edited by user4592357

                            @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))
                            
                            
                            1 Reply Last reply Reply Quote 0
                            • V
                              VRonin last edited by

                              You are using reset model so you can connect to QAbstractItemModel::modelReset

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              U 1 Reply Last reply Reply Quote 0
                              • U
                                user4592357 @VRonin last edited by user4592357

                                @VRonin
                                that's what i tried first

                                actually, 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)

                                V 1 Reply Last reply Reply Quote 0
                                • V
                                  VRonin @user4592357 last edited by

                                  @user4592357 said in model populated signal:

                                  the slot isn't invoked

                                  Doubt

                                  from https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN18QAbstractItemModel13endResetModelEv:

                                  void QAbstractItemModel::endResetModel()
                                  {
                                      Q_D(QAbstractItemModel);
                                      d->invalidatePersistentIndexes();
                                      QMetaObject::invokeMethod(this, "resetInternalData");
                                      emit modelReset(QPrivateSignal());
                                  }
                                  

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  U 1 Reply Last reply Reply Quote 0
                                  • U
                                    user4592357 @VRonin last edited by

                                    @VRonin
                                    yeah

                                    another weird thing: the 2nd time it's invoked, 3rd time it's invoked twice, 4th time - thrice, etc...

                                    1 Reply Last reply Reply Quote 0
                                    • V
                                      VRonin last edited by

                                      Where are you connecting the signal? looks like you are connecting every time it's invoked

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      U 1 Reply Last reply Reply Quote 3
                                      • U
                                        user4592357 @VRonin last edited by

                                        @VRonin
                                        my bad, right...

                                        how didn't i realize? thanks

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post