Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] QObject: invalid conversion issue.
QtWS25 Last Chance

[solved] QObject: invalid conversion issue.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.0k 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.
  • S Offline
    S Offline
    sleroux
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        sleroux
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0

          • Login

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