Show the contents of a QList

0

Hi, I've been working with the Qt library on C ++ for a while now and I'm getting the following problem:

Given a class named "project.h" I define a list as follows QList<Capa*> datosCapas; .

Following this, I proceed to load the list with the void addDataLayer (Capa* unaCapa); method. Basically what this method does is the following

void proyecto::addDataLayer(Capa* unaCapaFromXml){
    this->datosCapas.append(unaCapaFromXml);
}

At the moment of wanting to print the data by console, using a qDebug() and in this way to make sure that the list I have loaded the data I need. It occurred to me to implement the following:

void proyecto:: imprimirLista(){
    QListIterator<Capa*> iteratorCapas(this->datosCapas);
    Capa objetoCapa;
    while(iteratorCapas.hasNext()){
      objetoCapa = iteratorCapas.next();
      qDebug() << objetoCapa.getH() << "|" << objetoCapa.getHNeta() << "|" << objetoCapa.getPorosity();
    }
}

The problem is that there is an incompatibility at the time of iterating the list, precisely because it is pointers.

error: no match for 'operator=' (operand types are 'Capa' and 'Capa* const')
       objetoCapa = iteratorCapas.next();
                                   ^

How should I work it so I can print the data without problems?

  

Pd: If you ask me why I add a smart pointer to the bone list   ( Capa* unaCapaFromXml )

is because unaCapaFromXml is the parameter I use to store an object of type Layer, that is, at one point in the program I make an instance of the layer object, that is, new Capa(parámetro1,parámetro2,parámetro3) .

Greetings.

    
asked by marcos vasquez 29.06.2017 в 20:41
source

2 answers

0

Problem.

As I read in the QListIterator documentation , the next has the following signature:

const T &  next()

As I see in your code, your list is as follows:

QList<Capa*> datosCapas;

That is, you save pointers to Capa . And next returns a reference to the returned data, so it is as if it had this signature:

const Capa *& next()

Returns a pointer reference, but you try to save it to a Capa object (not a pointer to Capa ):

//      vvvvvvvvvv <---- Instancia
   Capa objetoCapa;
// ^^^^ <---- Objeto
   while(iteratorCapas.hasNext()){
    //                            vvvv <---- Puntero
       objetoCapa = iteratorCapas.next();
    // ^^^^^^^^^^ <---- Instancia

Solution.

Use a pointer:

void proyecto:: imprimirLista(){
    QListIterator<Capa*> iteratorCapas(this->datosCapas);
      // vvvvvvvvvvvv <---- Puntero
    Capa *punteroCapa;
    while(iteratorCapas.hasNext()){
     //                             vvvv <---- Puntero
        punteroCapa = iteratorCapas.next();
     // ^^^^^^^^^^^ <---- Puntero
        qDebug() << objetoCapa.getH() << "|" << punteroCapa->getHNeta() << "|" << punteroCapa->getPorosity();
     //                                         ^^^^^^^^^^^ <----- Puntero -----> ^^^^^^^^^^^
    }
}

Alternative.

Unreferenced before use:

void proyecto:: imprimirLista(){
    QListIterator<Capa*> iteratorCapas(this->datosCapas);
    while(iteratorCapas.hasNext()){
     //                    v <---- Des-referenciar.
        Capa &objetoCapa = *iteratorCapas.next();
     // ^^^^^^ <---- Referencia           ^^^^ <---- Puntero
        qDebug() << objetoCapa.getH() << "|" << objetoCapa.getHNeta() << "|" << objetoCapa.getPorosity();
    }
}
    
answered by 30.06.2017 в 08:52
0

I think it's important to mention that QList is not a linked list itself, as indicated by the documentation The linked list of Qt is the QLinkedList . Rather, QList is a modified vector:

  

Internally, QList is represented as an array of T if sizeof (T) < = sizeof (void *)

Now, the use of iterators with QList can be substituted by one of the following ways:

  • Q_FOREACH (obsolete from Qt 5.7):

    Q_FOREACH (auto capa, this->datosCapas) {
      // 'capa' es de tipo 'Capa*'
    }
    
  • Equivalent if you use C ++ 11:

    for (auto capa : this->datosCapas) {
      // 'capa' es de tipo 'Capa*'
    }
    
  • A loop iterating the indexes manually:

    for (int ii = 0; ii < this->datosCapas.count(); ++ii) {
      this->datosCapas[ii]-> ...;
    }
    
  • answered by 04.07.2017 в 08:28