Signal for QStringList count changed?
-
I have a
QStringListstoring a bunch of names andQcomboBoxto populate the list. I'll be adding new QString to the list and at the end of process, I want the QcomboBox to update with the list. But I couldn't find a signal for count change.connect(m_stringlist, &QStringList::countChanged??, this, &slotHandleCountChange ); -
Hi
QStringList has zero signals.
Maybe you could use
https://doc.qt.io/qt-5/qstringlistmodel.htmlOr simply define a new signal in the holding class and emit that to make combobox do what its needs to do.
-
Hi
QStringList has zero signals.
Maybe you could use
https://doc.qt.io/qt-5/qstringlistmodel.htmlOr simply define a new signal in the holding class and emit that to make combobox do what its needs to do.
-
Hi,
No signal needed, update the content of the model and the changes will be propagated to the view.
-
Hi
Just a mini sample since we work directly with a model// make the model QStringListModel *mo = new QStringListModel; // stuff something in it mo->setStringList( QStringList() << "a" << "b" ); // assign the model to the combobox ui->comboBox->setModel(mo); // use helper function to add string to the end of the model AddString(mo,"hi"); AddString(mo,"there"); AddString(mo,"im a model"); ... void AddString(QAbstractItemModel *model, QString newString ) { if(model->insertRow(model->rowCount())) { QModelIndex index = model->index(model->rowCount() - 1, 0); model->setData(index, newString); } }
-
Hi
Just a mini sample since we work directly with a model// make the model QStringListModel *mo = new QStringListModel; // stuff something in it mo->setStringList( QStringList() << "a" << "b" ); // assign the model to the combobox ui->comboBox->setModel(mo); // use helper function to add string to the end of the model AddString(mo,"hi"); AddString(mo,"there"); AddString(mo,"im a model"); ... void AddString(QAbstractItemModel *model, QString newString ) { if(model->insertRow(model->rowCount())) { QModelIndex index = model->index(model->rowCount() - 1, 0); model->setData(index, newString); } }
@mrjj said in Signal for QStringList count changed?:
void AddString(QAbstractItemModel *model, QString newString )
Wow looks like it would be better for me to create a subclass of qstringlistmodel to put all these in because I'm probably going to need more functions for this such as removeString.
-
@mrjj said in Signal for QStringList count changed?:
void AddString(QAbstractItemModel *model, QString newString )
Wow looks like it would be better for me to create a subclass of qstringlistmodel to put all these in because I'm probably going to need more functions for this such as removeString.
-
I encountered a problem when creating the subclass. When I created a new item model class in Qt Creator, it asks me to reimplement all the basic functions...
-
Oh I was creating the class with the default one from the wizard, which was inheriting the abstactitemmodel. Manually declaring the classs solved it.
void GenericStringListModel::append(QString &a_string) { if (insertRow(rowCount())) { QModelIndex index = this->index(rowCount() - 1, 0); setData(index, a_string); } }I'm not sure if using
thisis correct because it's giving me error without it.And is this correct for remove string from the list model?
void GenericStringListModel::removeOne(QString &a_string) { int index = stringList().indexOf(a_string); removeRow(index); } -
Oh I was creating the class with the default one from the wizard, which was inheriting the abstactitemmodel. Manually declaring the classs solved it.
void GenericStringListModel::append(QString &a_string) { if (insertRow(rowCount())) { QModelIndex index = this->index(rowCount() - 1, 0); setData(index, a_string); } }I'm not sure if using
thisis correct because it's giving me error without it.And is this correct for remove string from the list model?
void GenericStringListModel::removeOne(QString &a_string) { int index = stringList().indexOf(a_string); removeRow(index); } -
Oh I was creating the class with the default one from the wizard, which was inheriting the abstactitemmodel. Manually declaring the classs solved it.
void GenericStringListModel::append(QString &a_string) { if (insertRow(rowCount())) { QModelIndex index = this->index(rowCount() - 1, 0); setData(index, a_string); } }I'm not sure if using
thisis correct because it's giving me error without it.And is this correct for remove string from the list model?
void GenericStringListModel::removeOne(QString &a_string) { int index = stringList().indexOf(a_string); removeRow(index); }@lansing said in Signal for QStringList count changed?:
QModelIndex index = this->index(rowCount() - 1, 0);...
I'm not sure if using
thisis correct because it's giving me error without it.You defined a new local variable called
index. That conflicts with name of the functionindex(). That's why you needthisto tell the compiler to use the function instead of the variable.If you gave your variable a different name, then you don't need
this. -
@lansing said in Signal for QStringList count changed?:
QModelIndex index = this->index(rowCount() - 1, 0);...
I'm not sure if using
thisis correct because it's giving me error without it.You defined a new local variable called
index. That conflicts with name of the functionindex(). That's why you needthisto tell the compiler to use the function instead of the variable.If you gave your variable a different name, then you don't need
this.