0

I'm not 100% up to scratch regarding my understanding of pointers.

I have some code that is giving me the following error:

base operand of '->' has non-pointer type 'QStandardItemModel'

Here is the relevant code:

stocklist.h

class StockList
{
public:
    void populateStockModel();
private:
    QStandardItemModel m_stockModel;
};

stocklist.cpp

void StockList::populateStockModel() {
    foreach(Item* item, m_items) {
        QList<QStandardItem*> newRow;

        QStandardItem* stockID = new QStandardItem(QString("%1").arg(item->stockID()));
        QStandardItem* desc = new QStandardItem(QString("%1").arg(item->stockID()));
        QStandardItem* price = new QStandardItem(QString("%1").arg(item->stockID()));

        newRow.append(stockID);
        newRow.append(desc);
        newRow.append(price);

        m_stockModel->appendRow(newRow);
    }
}

The error refers to the line that contains m_stockModel->appendRow(newRow);.

Any idea how I can resolve this? I'm basically trying to populate a model using those item values.

Help would be much appreciate! Thank you!

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143

2 Answers2

4

It's right there to see - m_stockModel is a QStandardItemModel, and it doesn't have an operator->().

You probably meant to write m_stockModel.appendRow(), or to declare m_stockModel as a pointer to QStandardItemModel. Without a full example, it's hard to be sure which.

You probably want to improve your exception-safety too - if allocating price fails, you'll leak stockID and desc.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
1

For m_stockModel to be regarded as a pointer, you will need to declare it as such by using the asterisk notation:

QStandardItemModel * m_stockModel;

You will also need to allocate memory for m_stockModel by using the new operator before calling its functions.

I recommend studying the basics of pointers in C++

gortsu
  • 126
  • 8