postb99
19th February 2014, 09:52
Hello,
In Qt examples, there are examples that usually go beyond my simple test program. I have a QListView in main windo. I don't use designer or Qt quick.
Why do I loose the itemName value? My program crashes after I add a new item to my model and thanks to debugging I see that I indeed have 3 items in my vector but there is a blank string rather than expected value for last item (the one I created by clicking on the button).
Thank you for pointing my learning coding errors.
Here is some shortened source code, I attach the whole source code to this post.
MyModel.h:
class MyModel : public QAbstractTableModel
{
Q_OBJECT
public:
MyModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const ;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void addItem(TestItem item);
private:
std::vector<TestItem*> m_items;
public slots:
void onButtonClicked();
};
TestItem.h:
class TestItem {
public:
TestItem();
TestItem(QString itemName);
QString getName() const;
void setName(QString itemName);
private :
QString m_itemName;
};
MyModel.cpp:
QVariant MyModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
// generate a log message when this method gets called
qDebug() << QString("row %1, col%2, role %3")
.arg(row).arg(col).arg(role);
if(role == Qt::DisplayRole){
if (col == 0) return (*m_items[row]).getName();
return QString("Row%1, Column%2")
.arg(row + 1)
.arg(col +1);
}
return QVariant();
}
void MyModel::addItem(TestItem item)
{
int nbRows = m_items.size();
beginInsertRows(QModelIndex(), nbRows, nbRows);
m_items.push_back(&item);
endInsertRows();
}
void MyModel::onButtonClicked(){
TestItem newItem("new element");
addItem(newItem);
}
10050
In Qt examples, there are examples that usually go beyond my simple test program. I have a QListView in main windo. I don't use designer or Qt quick.
Why do I loose the itemName value? My program crashes after I add a new item to my model and thanks to debugging I see that I indeed have 3 items in my vector but there is a blank string rather than expected value for last item (the one I created by clicking on the button).
Thank you for pointing my learning coding errors.
Here is some shortened source code, I attach the whole source code to this post.
MyModel.h:
class MyModel : public QAbstractTableModel
{
Q_OBJECT
public:
MyModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const ;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void addItem(TestItem item);
private:
std::vector<TestItem*> m_items;
public slots:
void onButtonClicked();
};
TestItem.h:
class TestItem {
public:
TestItem();
TestItem(QString itemName);
QString getName() const;
void setName(QString itemName);
private :
QString m_itemName;
};
MyModel.cpp:
QVariant MyModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
// generate a log message when this method gets called
qDebug() << QString("row %1, col%2, role %3")
.arg(row).arg(col).arg(role);
if(role == Qt::DisplayRole){
if (col == 0) return (*m_items[row]).getName();
return QString("Row%1, Column%2")
.arg(row + 1)
.arg(col +1);
}
return QVariant();
}
void MyModel::addItem(TestItem item)
{
int nbRows = m_items.size();
beginInsertRows(QModelIndex(), nbRows, nbRows);
m_items.push_back(&item);
endInsertRows();
}
void MyModel::onButtonClicked(){
TestItem newItem("new element");
addItem(newItem);
}
10050