QList 表现得很奇怪

发布于 2024-09-15 16:17:45 字数 1073 浏览 2 评论 0 原文

我对 Qt 没有太多经验,但不知怎的,我觉得这很奇怪。 用VS2005编译:

class Entry
{
public:
    Entry(const QString& aName, bool checked) : 
        name(aName), isChecked(checked)
    { 
        // empty 
    };

    Entry(const Entry& entry) : 
        name(entry.name), isChecked(entry.isChecked)
    { 
        // empty 
    };

    Entry& operator=(const Entry& entry)
    {
        name = entry.name;
        isChecked = entry.isChecked;
        return *this;
    }

    QString name;
    bool isChecked;
};

typedef QList<conduit::Entry> EntryVector;

当在下面几行中使用EntryVector时,QList中的条目成为一个坏指针:

void EntryWidget::setEntries(QStringList& names)
{
    QStringList::iterator member;
    EntryVector list;

    for (member = names.begin(); member != names.end(); ++member)
    {
        Entry entry(*member, false);
        list.append(entry);
    }

    m_model.setEntryData(list);
}

不知何故,列表中的entry.name将成为一个坏指针。我的理解是 QList 应该能够支持其模板中的任何数据,但由于某种原因,我还不明白这不起作用。如果我使用 STL Vector,一切都会正常工作。

有什么想法吗?谢谢

I don't have much experience with Qt but somehow I think this is acting strange.
Compiled with VS2005:

class Entry
{
public:
    Entry(const QString& aName, bool checked) : 
        name(aName), isChecked(checked)
    { 
        // empty 
    };

    Entry(const Entry& entry) : 
        name(entry.name), isChecked(entry.isChecked)
    { 
        // empty 
    };

    Entry& operator=(const Entry& entry)
    {
        name = entry.name;
        isChecked = entry.isChecked;
        return *this;
    }

    QString name;
    bool isChecked;
};

typedef QList<conduit::Entry> EntryVector;

When using EntryVector in the following lines, the entry in QList becomes a bad pointer:

void EntryWidget::setEntries(QStringList& names)
{
    QStringList::iterator member;
    EntryVector list;

    for (member = names.begin(); member != names.end(); ++member)
    {
        Entry entry(*member, false);
        list.append(entry);
    }

    m_model.setEntryData(list);
}

Somehow, entry.name in the list will become a bad pointer. My understanding is that QList should be able to support any data in its template, but for some reason that I yet do not understand this does not work. Everything works just fine if I use a STL Vector.

Any ideas? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趴在窗边数星星i 2024-09-22 16:17:46

我也遇到了同样的问题,并来寻找答案。

我以为我的代码行为不当,但事实并非如此。

VS2005 调试器无法正确显示 QList 中的内容。

正如 davmac 所建议的,当你打印出这些东西时,它工作得很好。

还有 davmac,当那个家伙给你一段代码让你尝试时,请不要指出他可能有内存损坏。如果您无法使用相同的设置进行尝试,那就是另一回事了。

I had this same problem, and came looking for anwsers.

I though my code was misbehaving, when it's not.

VS2005 debugger doesn't show you the things in QList correctly.

And as davmac suggested, when you print the stuff out, it works fine.

And davmac, please don't point out that he might have a memory corruption when the guy gives you a piece of code to try. If you can't try it with the same setup, that's another thing.

多情出卖 2024-09-22 16:17:46

“entry.name”不是一个指针 - 它被声明为 QString,在这里:

public: QString name;

那么,“entry.name 将成为一个坏指针”是什么意思?

最有可能的是,程序的其他部分导致了内存损坏。

"entry.name" isn't a pointer - it's declared as a QString, here:

public: QString name;

So, what do you mean by "entry.name will become a bad pointer"?

Most likely, some other part of your program is causing a memory corruption.

-小熊_ 2024-09-22 16:17:46

我假设 QStringList 在其复制向量中对字符串进行深度复制。

我们知道 QList 不进行深度复制。 查看 QList隐式共享

因此,在调用

void EntryWidget::setEntries(QStringList& names) {...}

名称时,会复制字符串。

但是,当您将新的 Entry-List 设置为模型时,

m_model.setEntryData(list);

列表 不会复制到 mmodel.xy 中。

现在您可以访问mmodel.xy,但是您在setEntries(..)中分配的字符串已被删除。
当您离开 setEntries(..) 方法时,它们就失去了作用域。

注意:
QString 是一个指向字符串的引用指针。这称为“隐式共享”。
Qt 中的所有容器都有惰性求值的概念。
(可能除了 QStringList,它进行深度复制。这可能是 Qt 中的一个小错误。如果我看到 .cpp,我只能说 100%)

qt-project.org/doc /qt-5/qstringlist.html#QStringList-3

I assume, that QStringList makes a depth-copy of the Strings in its copy-ctor.

And we know QList makes no depth-copy. see QList and implicit sharing

So, at the call of

void EntryWidget::setEntries(QStringList& names) {...}

Strings in names are copied.

But when you set the new Entry-List to the model,

m_model.setEntryData(list);

the list is not copied into mmodel.xy.

Now you can access mmodel.xy, but the strings you assign in setEntries(..) are already deleted.
They lost their scope, when you leave the setEntries(..) method.

Note:
QString is a reference pointer to a String. This is called "implicit sharing" .
And all containers in Qt have the concept of lazy-evaluation.
(Probably except the QStringList, which makes a depth-copy. This is perhaps a little bug in Qt. I can it only say 100% if I see the .cpp)

qt-project.org/doc/qt-5/qstringlist.html#QStringList-3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文