0

I've got the following struct :

struct HidItem
{
    HidItem(){}
    HidItem(QString commandName, uint commandValue)
    {
        name = commandName;
        value = commandValue;
    }

    QString name;
    uint value;
};

and trying to create an instance like so:

HidCommandModel::HidCommandModel()
    : QAbstractListModel(),
      hidData(new QVector<HidItem>(10))
{
    HidItem *hidItem = new HidItem("USAGE_PAGE", 1);
    hidData->append(*(hidItem));
}

This isn't working too well, as only the uint commandValue gets assigned to the HidItem instance, and not the QString "USAGE_PAGE"

I haven't coded C++ in quite a few years, but this seems to me that I'm passing the string incorrectly, and needs to have some kung fu involved with pass by reference, pointers and copy and write.

The theory is there, but my practical skills are letting me down horribly today. Can anyone assist?

7
  • 2
    How do you check that it's not working? Commented May 7, 2013 at 13:13
  • I check by setting breakpoints in the code and inspecting the variables in a debug run. Commented May 7, 2013 at 13:20
  • Ok, problem isn't with the hidData->append line... Commented May 7, 2013 at 13:21
  • 1
    Unrelated to this particular problem, but you might want to fix that memory leak; you're unnecessarily creating the object with new, copying it, and then discarding the pointer. Try hidData->append(HidItem("USAGE_PAGE, 1)) instead. Commented May 7, 2013 at 13:22
  • 1
    @josef.van.niekerk: Also, hidData should probably be a QVector object, not a pointer. It's best to avoid new unless you really need it. Commented May 7, 2013 at 13:25

1 Answer 1

3

Works for me:

struct HidItem
{
    HidItem(){}
    HidItem(QString commandName, uint commandValue)
    {
        name = commandName;
        value = commandValue;
    }

    QString name;
    uint value;
};


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

    HidItem *hidItem = new HidItem("USAGE_PAGE", 1);
    qDebug() << hidItem->name << "," << hidItem->value;
}

Output:

"USAGE_PAGE" , 1

One thing you should change is the signature of the HidItem constructor, so that it expects a const reference instead of an object:

HidItem(const QString& commandName, uint commandValue) {
...

This avoids unnecessary creation of temporary objects. But still, your approach also works well.


The real issue is the wrong usage of the QVector constructor:

QVector<HidItem>* hidData = new QVector<HidItem>(10);
hidData->append(hidItem);

appends the hidItem as element #11, since the QVector already contains 10 elements after being created.

Simply use

QVector<HidItem>* hidData = new QVector<HidItem>();
hidData->append(hidItem);

(and consider the remarks regarding new by @Mike Seymour).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Andreas, I just realised my problem is with the hidData->append(*(hidItem)); line. When I try to fetch the data by using hidData->at(0).name for example, I get nothing.
Thanks for the qDebug() tip, didn't know you could do that. ;)
Wow, thanks! My C++ has not only rusted over the years, I think I got mould growing all over it...:D
@josef.van.niekerk No issue - did you do Java coding recently? The java.util.Vector API behaves like you expected in your code ;-)
I did Java a few months ago on a project I was working, yes, but don't exactly recall using Vector that much. I do remember using other collection classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.