1

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);
}

You probably have the wrong mental image of a pipe. It has two ends, each represented by a different handle. Yes, CloseHandle has to be called twice to make the pipe instance disappear. But since they are different handles, that can never cause any problem. Also note that handle instances are process specific. Even if they have the same value in both processes, they do not reference the same pipe endpoint.

4

3 に答える 3

3

小さな例

#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();
}

したがって、qDebug() << *k->at(i); のように、QList の要素を呼び出すときに関数内で * を使用するだけです。ストリング

于 2010-07-05T12:44:58.530 に答える
0

QListを値で渡す必要があります。これは一見ばかげているように見えるかもしれませんが、その理由は、QListが暗黙的に共有されているためです。このトピックの詳細については、http://doc.trolltech.com/latest/implicit-sharing.htmlをお読みください。

于 2010-07-05T19:43:26.133 に答える
0
#include <QtCore/QtCore>

void
printList(const QStringList& list)
{
    foreach (const QString& str, list) {
        qDebug() << str;
    }
}

int main(int argc, char** argv) {
    QCoreApplication app(argc, argv);

    QStringList list;
    list << "A" << "B" << "C";

    printList(list);

    return QCoreApplication::exec();
}

使用する QStringList というクラスが既にあります。また、参照によって渡すこともできます。また、コンテナーまたは QString でポインターを使用したくありません。それらは自動的に暗黙的に共有されるため。したがって、これらの 2 つのポインターを使用するのは悪い設計です。

于 2010-07-05T13:01:56.553 に答える