Search code examples
qtpointersqlist

how to send QList<Object *> objects to another class?


i'm trying to send a QList as a parameter to another class but for some reason i lose all it's content ... (when i open the object with the debuger i see for objects...)

trying to send QList books to class Print:

class Store: public QWidget {
    Q_OBJECT
public:
    Analyze(QWidget *parent = 0);
    void generate_report();
    ~Analyze();

private:
    QList<Book *> books;

};

class Print
{
public:
    Print();
    bool generate_report_file(QList<Book *> *);
};

i'm sending books like this:

void Analyze::generate_report()
{
.
.
.

    Print p;
    if (!p.generate_report_file(&books))
        QMessageBox::warning(this, "XML Escape","Error creating out.html", QMessageBox::Ok);
}

Solution

  • Small example

    #include <QtCore/QCoreApplication>
    #include <QDebug>
    #include <QList>
    #include <QString>
    
    void print_list(QList<QString *> * k)
    {
        for (int i=0; i<k->size(); i++)
        {
            qDebug() << *k->at(i);
        }
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QList<QString *> books;
        books.append(new QString("asd"));
        books.append(new QString("asdfgh"));
        books.append(new QString("asdjhhhhhhtyut"));
        print_list (&books);
    
        return a.exec();
    }
    

    so just use * in function when calling elements of your QList, like in qDebug() << *k->at(i); string