开发者

QList acting strange

开发者 https://www.devze.com 2023-01-13 12:46 出处:网络
I don\'t have much experience with Qt but somehow I think this is acting strange. Compiled with VS2005:

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


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.


"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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消