problem creating QAbstractItemModel
-
Hi all -
This is only my 2nd time working in the model/view world. I was trying to use the code from my 1st time as a, well, you know...model.
I want to populate a model with data I receive from a TCP message. I have a class containing the model:
model_ap_scan.h
class ModelApScan : public QObject { Q_OBJECT private: QAbstractItemModel *m_model; // the main model public: explicit ModelApScan(QObject *parent = Q_NULLPTR); ~ModelApScan(); public slots: void update(Message *msg, vector<string> &keys);
model_ap_scan.cpp:
ModelApScan::ModelApScan(QObject *parent) : QObject(parent), m_model(new QStandardItemModel(this)) { m_model->insertColumns(0, AP_TABLE_NBR_COLUMNS); m_model->setHeaderData(AP_TABLE_COLUMN_MACADDR, Qt::Horizontal, tr("MAC Address"), Qt::DisplayRole); m_model->setHeaderData(AP_TABLE_COLUMN_SSID, Qt::Horizontal, tr("SSID"), Qt::DisplayRole); m_model->setHeaderData(AP_TABLE_COLUMN_RSSI, Qt::Horizontal, tr("RSSI"), Qt::DisplayRole); } void ModelApScan::update(Message *msg, vector<string> &keys) { int row; int rowCnt = m_model->rowCount(); ...
This is, as far as I can see, verbatim from my 1st model. But, I get a segfault on the call to rowCount() in update(). Can anyone see what I'm doing wrong?
Thanks...
-
@mzimmers said in problem creating QAbstractItemModel:
I get a segfault on the call to rowCount()
If you get a segfault there then it means m_model is invalid pointer.
Do you delete m_model somewhere? -
@KillerSmath hi - I'm not inserting any items yet. My model is empty when I make that call.
@jsulm no, I'm not deleting it, but...in stepping through the debugger, I just realized that my c'tor isn't being called. That's really odd...don't know what to make of that, though it does explain why the call to rowCount() blows up.
-
Hi,
Where are you creating the instance of
ModelApScan
?
Are you sure it's still valid when calling update ? -
Bingo...I wasn't creating it. I created the pointer but never invoked the c'tor from the parent. Now corrected:
EditDialog::EditDialog(DeviceModel *d, QModelIndex *qmi, Worker *pWorker, QWidget *parent) : QDialog(parent), ui(new Ui::EditDialog), m_d(d), m_qmi(qmi), m_worker(pWorker) { ui->setupUi(this); // create the model that holds the results of the AP scan. m_modelApScan = new ModelApScan();
Amazing the brain-dead mistakes one can make when distracted...thanks for the help.
-
Side note (partly aesthetics), you can keep
m_modelApScan
in the initialiser list of yourEditDialog
class constructor.