0

My project is to simulate a "create newtorks" application like PacketTracer and i want to be able to save the status of the project, so after closing and re-running the app, everything is still there if the user wants to. I have used QSetting where I have a SaveSettings() and a LoadSettings() called from button's clicked slot. I got stucked here: I have a QList switch_class named switch_List and I want to do something like that: in the Save Settings():

   QSettings setting("myorganization","blabla");
   setting.setValue("list_of_switch_objects",switch_List);//cannot convert from 
   // QList<switch_list> to const QVariant .

Any suggestions would be appreciated !

  • what is `switch_class`? – eyllanesc Aug 28 '18 at 15:23
  • Your question is deficient until you show what `switch_class` is. – Kuba hasn't forgotten Monica Aug 28 '18 at 17:49
  • @eyllanesc switch_class is the name of the class implemented for storing the data of switches. Its a simple class with two constructors, one private qstring method which returns the switch's name, one static variable which contains the no_of_ports, and a qstringlist which stores the pc's mac adresses. nothing suspicious there :D – Radu Marius Aug 29 '18 at 16:01
  • @KubaOber hope u see the answer too – Radu Marius Aug 29 '18 at 16:02
  • @RaduMarius I do not say it for that, so that you understand me check the comment of KubaOber, if your class can be used in a QDataStream you should not have problem. – eyllanesc Aug 29 '18 at 16:03
  • @eyllanesc. I think i cant. Nevermind. Help me with that and im glad, please. I have a QListview and i want to store all the text into a QStringList. I've seen on stack something like: QStringList list; foreach(const QModelIndex &index, ui->QListview->selectionModel()->selectedIndexes()) list.append(model->itemFromIndex(index)->text()); And they didn't say something about what that "model" is . It's a Qstandardmodel item object? and how can i "connect" him to my qlistview? thank you ! – Radu Marius Aug 29 '18 at 16:23
  • @RaduMarius mmm, that's another question, do not be distracted please. Are you interested in our help in your current question? – eyllanesc Aug 29 '18 at 16:24
  • I know, but i've never worked with QDataStream,there's nothing on youtube and i dont have only QStrings in my switch_class so i dont want to waste your time and me getting upset because of that problem which its too hard for me to solve. So i want to take it easy and at least solve the problem with the QListView – Radu Marius Aug 29 '18 at 16:28
  • @eyllanesc so, if u could answer on the QListView i would be grateful. – Radu Marius Aug 29 '18 at 16:42
  • @RaduMarius the answer depends on the model, what model are you using ?, in general you should use: `QStringList l; for(int i = 0; i < model->rowCount (); i++) {QModelIndex ix = model->index(i, 0); l << model->data(index).toString();}` – eyllanesc Aug 29 '18 at 16:45
  • It worked, thank you sir! – Radu Marius Aug 29 '18 at 16:55

1 Answers1

1

QSettings::setValue() need a QVariant argument. So you have to convert your QList into a QVariant or a QVariantList.

See this thread's accepted answer for a possible solution:

template <typename T>
QVariantList toVariantList( const QList<T> &list )
{
    QVariantList newList;
    foreach( const T &item, list )
        newList << item;

    return newList;
}

Alternatively, you can find some interesting code on Qt doc that may help you to perform the convertion:

QList<int> intList = {7, 11, 42};

QVariant variant = QVariant::fromValue(intList);
if (variant.canConvert<QVariantList>()) {
    QSequentialIterable iterable = variant.value<QSequentialIterable>();
    // Can use foreach:
    foreach (const QVariant &v, iterable) {
        qDebug() << v;
    }
    // Can use C++11 range-for:
    for (const QVariant &v : iterable) {
        qDebug() << v;
    }
    // Can use iterators:
    QSequentialIterable::const_iterator it = iterable.begin();
    const QSequentialIterable::const_iterator end = iterable.end();
    for ( ; it != end; ++it) {
        qDebug() << *it;
    }
}
Antwane
  • 20,760
  • 7
  • 51
  • 84
  • 1
    `QVariant` handles containers automagically, so all that needs to be done is to implement the `QDataStream` operators for `switch_class`, register them, and then `QVariant` will know how to handle that type - whether in containers or not. – Kuba hasn't forgotten Monica Aug 28 '18 at 17:48