Qt Forum

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

    [solved] QObject: invalid conversion issue.

    General and Desktop
    2
    4
    3439
    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.
    • S
      sleroux last edited by

      Good day everyone

      I have an issue that is probably related to me not using pointers and classes correctly. I am stuck and I would really appreciate some help.
      I have a book class, that is derived from QObject:
      @
      class book : public QObject
      {
      Q_OBJECT
      public:
      //Default constructor.
      explicit book(QObject *parent = 0);
      //Constructor.
      book(QString a, QString t, double c);
      //Retrieve values in string format.
      QString toString() const;
      //Getters.
      QString getAuthor() const;
      QString getTitle() const;
      double getCost() const;

      private:
      //Data members.
      QString author;
      QString title;
      double cost;
      };
      @

      Now I have a library class, that keeps a QList of books:
      @
      class library
      {
      public:
      //Default constructor.
      library();
      //Add a book to the list of books.
      void addBook(book *b);
      //Return a list of all the books.
      QStringList getBookList() const;
      //Destructor.
      ~library();

      private:
      //Member to contain books.
      QList<QObject *> libraryBooks;
      };
      @

      My error is in the getBookList() function. Here is the code:
      @
      //Return a list of all the books.
      QStringList library::getBookList() const{
      QStringList bookList;
      QString entry;
      book *myBook;
      foreach(myBook, libraryBooks){
      entry = myBook->toString();
      bookList << entry;
      }
      return bookList;
      }
      @

      The error that I get is "invalid conversion from 'QObject*' to 'book*'[-fpermissive]
      What gets me is that book is derived from QObject, so that shouldn't be a problem?
      Any ideas?

      Thank you

      [edit: added missing coding tags @ SGaist]

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        libraryBooks is a QList of QObject * which is not the same thing as a QList of book * so you shoulde either:

        • Use a QList<book *>
        • Use qobject_cast in your loop

        e.g.
        @
        foreach(QObject *book, libraryBooks){
        book entry = qobject_cast<book>(book);
        bookList << entry->toString();
        }
        @

        But the first question is: does it really make sense to store your books as a list of QObject * rather than book * ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • S
          sleroux last edited by

          Thank you so much. You are 100% correct, it is not necessary.
          QList<book*> did the trick.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            You're welcome !

            Since you have your library running, please update the thread title perpending [solved] so other forum users may know a solution has been found :)

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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