Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. Problema con QList ed oggetto custom
QtWS25 Last Chance

Problema con QList ed oggetto custom

Scheduled Pinned Locked Moved Solved Italian
8 Posts 3 Posters 1.5k Views
  • 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.
  • F Offline
    F Offline
    fermatqt
    wrote on last edited by
    #1

    ciao!

    sto cercado di riempire una QList con un oggetto custom, prendendo i record da db.
    ho creato questa classe che rappresenta la mia tabella (i getter ed i setter li ho fatti generare da qt creator):

    #ifndef SYNC_H
    #define SYNC_H
    
    #include <QObject>
    
    class Sync : public QObject {
        Q_OBJECT
    public:
        Sync();
        Sync(const Sync &other);
        Sync &operator = (const Sync &other);
    
        qint32 getId() const;
        void setId(const qint32 &value);
        QString getNome() const;
        void setNome(const QString &value);
        QString getComando() const;
        void setComando(const QString &value);
        QString getSource() const;
        void setSource(const QString &value);
        QString getDetination() const;
        void setDetination(const QString &value);
    
    private:
        qint32 id;
        QString nome;
        QString comando;
        QString source;
        QString detination;
    };
    
    #endif // SYNC_H
    
    
    #include "sync.h"
    
    Sync::Sync() {}
    
    Sync::Sync(const Sync &other) {
    }
    
    qint32 Sync::getId() const {
        return id;
    }
    
    void Sync::setId(const qint32 &value) {
        id = value;
    }
    
    QString Sync::getNome() const {
        return nome;
    }
    
    void Sync::setNome(const QString &value) {
        nome = value;
    }
    
    QString Sync::getComando() const {
        return comando;
    }
    
    void Sync::setComando(const QString &value) {
        comando = value;
    }
    
    QString Sync::getSource() const {
        return source;
    }
    
    void Sync::setSource(const QString &value) {
        source = value;
    }
    
    QString Sync::getDetination() const {
        return detination;
    }
    
    void Sync::setDetination(const QString &value) {
        detination = value;
    }
    

    poi eseguo le query e faccio questo:

    QList<Sync> Database::getAll() {
        QList<Sync> list;
        QSqlDatabase db = QSqlDatabase::database();
        QSqlQuery query("SELECT * FROM sync");
        while (query.next()) {
            qDebug() << query.value("nome").toString();
    
            Sync sync;
            sync.setId(query.value("id").toInt());
            sync.setNome(query.value("nome").toString());
            list.append(sync);
        }
    
        QList<Sync>::const_iterator iter;
        for (iter = list.constBegin(); iter != list.constEnd(); ++iter) {
            const Sync &sync = *iter;
            qDebug() << sync.getNome();
        }
    
        return list;
    }
    

    il problema è che il primo debug mi tira fuori i valori correttamente, mentre il secondo un valore vuoto.
    penso di sbagliare qualcosa nel setter, ma non capisco cosa.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      L'errore e' che hai il copy-constructor vuoto.
      cancella

      Sync::Sync(const Sync &other) {
      }
      

      e cambia

      Sync(const Sync &other);
          Sync &operator = (const Sync &other);
      

      in

      Sync(const Sync &other) = default;
          Sync &operator = (const Sync &other) = default;
      

      "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

      F 1 Reply Last reply
      0
      • VRoninV VRonin

        L'errore e' che hai il copy-constructor vuoto.
        cancella

        Sync::Sync(const Sync &other) {
        }
        

        e cambia

        Sync(const Sync &other);
            Sync &operator = (const Sync &other);
        

        in

        Sync(const Sync &other) = default;
            Sync &operator = (const Sync &other) = default;
        
        F Offline
        F Offline
        fermatqt
        wrote on last edited by
        #3

        @VRonin
        ciao!

        ho fatto le modifiche che mi hai detto, però ottengo tutta una serie di errori di questo tipo:

        /usr/include/qt/QtCore/qlist.h:435: error: use of deleted function ‘Sync::Sync(const Sync&)’
             if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
                                                                         ^~~~~~~~
        sync.h:10: error: use of deleted function ‘QObject::QObject(const QObject&)’
        

        il problema è quanto faccio l'appenda:

            while (query.next()) {
                qDebug() << query.value("nome").toString();
        
                Sync sync;
                sync.setId(query.value("id").toInt());
                sync.setNome(query.value("nome").toString());
                list.append(sync);
            }
        

        se commento l'append il programma non va in errore.

        VRoninV 1 Reply Last reply
        0
        • F fermatqt

          @VRonin
          ciao!

          ho fatto le modifiche che mi hai detto, però ottengo tutta una serie di errori di questo tipo:

          /usr/include/qt/QtCore/qlist.h:435: error: use of deleted function ‘Sync::Sync(const Sync&)’
               if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
                                                                           ^~~~~~~~
          sync.h:10: error: use of deleted function ‘QObject::QObject(const QObject&)’
          

          il problema è quanto faccio l'appenda:

              while (query.next()) {
                  qDebug() << query.value("nome").toString();
          
                  Sync sync;
                  sync.setId(query.value("id").toInt());
                  sync.setNome(query.value("nome").toString());
                  list.append(sync);
              }
          

          se commento l'append il programma non va in errore.

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @fermatqt said in Problema con QList ed oggetto custom:

          use of deleted function

          Sei sicuro di aver fatto = default come sopra e non = delete?

          "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

          F 1 Reply Last reply
          0
          • VRoninV VRonin

            @fermatqt said in Problema con QList ed oggetto custom:

            use of deleted function

            Sei sicuro di aver fatto = default come sopra e non = delete?

            F Offline
            F Offline
            fermatqt
            wrote on last edited by
            #5

            @VRonin said in Problema con QList ed oggetto custom:

            @fermatqt said in Problema con QList ed oggetto custom:

            use of deleted function

            Sei sicuro di aver fatto = default come sopra e non = delete?

            si!

            class Sync : public QObject {
                Q_OBJECT
            public:
                Sync();
                Sync(const Sync &other) = default;
                Sync &operator = (const Sync &other) = default;
                ~Sync();
            
                qint32 getId() const;
                void setId(const qint32 &value);
                QString getNome() const;
                void setNome(const QString &value);
                QString getComando() const;
                void setComando(const QString &value);
                QString getSource() const;
                void setSource(const QString &value);
                QString getDetination() const;
                void setDetination(const QString &value);
            
            private:
                qint32 id;
                QString nome;
                QString comando;
                QString source;
                QString detination;
            };
            
            #include "sync.h"
            
            Sync::Sync() {}
            
            Sync::~Sync() {
            }
            
            qint32 Sync::getId() const {
                return id;
            }
            
            void Sync::setId(const qint32 &value) {
                id = value;
            }
            
            QString Sync::getNome() const {
                return nome;
            }
            
            void Sync::setNome(const QString &value) {
                nome = value;
            }
            
            QString Sync::getComando() const {
                return comando;
            }
            
            void Sync::setComando(const QString &value) {
                comando = value;
            }
            
            QString Sync::getSource() const {
                return source;
            }
            
            void Sync::setSource(const QString &value) {
                source = value;
            }
            
            QString Sync::getDetination() const {
                return detination;
            }
            
            void Sync::setDetination(const QString &value) {
                detination = value;
            }
            

            ti ho postato entrambi i file della classe!

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              Molto vero. colpa mia, non ho visto che Sync e' un QObject. Ti serve inviare signal da quella classe? come mai l'hai creata partendo da QObject?

              "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

              F 1 Reply Last reply
              0
              • VRoninV VRonin

                Molto vero. colpa mia, non ho visto che Sync e' un QObject. Ti serve inviare signal da quella classe? come mai l'hai creata partendo da QObject?

                F Offline
                F Offline
                fermatqt
                wrote on last edited by
                #7

                @VRonin in verità non c'è nessun motivo particolare.

                ho modificato così e funziona alla perfezione:

                #include <QObject>
                
                class Sync {
                
                public:
                    Sync();
                    Sync(const Sync &other) = default;
                    Sync &operator = (const Sync &other) = default;
                    virtual ~Sync();
                
                    qint32 getId() const;
                    void setId(const qint32 &value);
                    QString getNome() const;
                    void setNome(const QString &value);
                    QString getComando() const;
                    void setComando(const QString &value);
                    QString getSource() const;
                    void setSource(const QString &value);
                    QString getDetination() const;
                    void setDetination(const QString &value);
                
                private:
                    qint32 id;
                    QString nome;
                    QString comando;
                    QString source;
                    QString detination;
                };
                

                grazie!

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  PaoloMicroel
                  wrote on last edited by
                  #8

                  potreste spiegarmi il significato del costruttore ?
                  Sync(const Sync &other) = default;
                  Sync &operator = (const Sync &other) = default;

                  grazie

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved