C++ (Qt)MyModel::MyModel(QObject *parent): QStandardItemModel(parent){} Qt::ItemFlags MyModel::flags(const QModelIndex &index) const{ Qt::ItemFlags flags = QStandardItemModel::flags(index); if (index.column() == 0 )flags |= Qt::ItemIsUserCheckable; return flags;} QVariant MyModel::data(const QModelIndex &index, int role) const{ QVariant value = QStandardItemModel::data(index, role); switch (role) { case Qt::DisplayRole: // Data to display if (index.column() == 1){ if (value.toInt() == 1)return tr("NEW"); else if (value.toInt() == 2)return tr("DELETED"); else if (value.toInt() == 3)return tr("EDITED"); } else return value; ... } return value; }
C++ (Qt) ... strlstTemplate<<"<TABLE BORDER=0 WIDTH=100% CELLSPACING=1 CELLPADDING=5 BGCOLOR=white FRAME=void>"; for (int row=0; row<=model.rowCount()-1;row++){ for (int column=0;column<=model.columnCount()-1;column++){ QStandardItem *itm = model.item(row,column); strTableRow+="<TD>"+itm->data(Qt::DisplayRole).toString()+"</TD>"; }; strlstTemplate<<"<TR>"<<strTableRow<<"</TR>"; strTableRow=""; }; ... strlstTemplate<<"</TBODY>"; strlstTemplate<<"</TABLE>";
strTableRow+="<TD>"+itm->data(Qt::DisplayRole).toString()+"</TD>";
C++ (Qt) model=new MyModel; QList<QStandardItem*>lstRow; for (int row=0;row<10;++row){ for (int column=0;column<4;++column){ QStandardItem *itm=new QStandardItem; itm->setData(row,Qt::DisplayRole); lstRow<<itm; }; model->appendRow(lstRow); lstRow.clear(); }
C++ (Qt) for (int row=0;row<model->rowCount();++row){ for (int column=0;column<model->columnCount();++column){ qDebug()<<"row"<<row<<"column"<<column<<model->item(row,column)->data(Qt::DisplayRole); } }
row 0 column 0 QVariant(int, 0) row 0 column 1 QVariant(int, 0) row 0 column 2 QVariant(int, 0) row 0 column 3 QVariant(int, 0) row 1 column 0 QVariant(int, 1) row 1 column 1 QVariant(int, 1) row 1 column 2 QVariant(int, 1) row 1 column 3 QVariant(int, 1) row 2 column 0 QVariant(int, 2) row 2 column 1 QVariant(int, 2) row 2 column 2 QVariant(int, 2) row 2 column 3 QVariant(int, 2) row 3 column 0 QVariant(int, 3) row 3 column 1 QVariant(int, 3) row 3 column 2 QVariant(int, 3) row 3 column 3 QVariant(int, 3) row 4 column 0 QVariant(int, 4) row 4 column 1 QVariant(int, 4) row 4 column 2 QVariant(int, 4) row 4 column 3 QVariant(int, 4) row 5 column 0 QVariant(int, 5) row 5 column 1 QVariant(int, 5) row 5 column 2 QVariant(int, 5) row 5 column 3 QVariant(int, 5) row 6 column 0 QVariant(int, 6) row 6 column 1 QVariant(int, 6) row 6 column 2 QVariant(int, 6) row 6 column 3 QVariant(int, 6) row 7 column 0 QVariant(int, 7) row 7 column 1 QVariant(int, 7) row 7 column 2 QVariant(int, 7) row 7 column 3 QVariant(int, 7) row 8 column 0 QVariant(int, 8) row 8 column 1 QVariant(int, 8) row 8 column 2 QVariant(int, 8) row 8 column 3 QVariant(int, 8) row 9 column 0 QVariant(int, 9) row 9 column 1 QVariant(int, 9) row 9 column 2 QVariant(int, 9) row 9 column 3 QVariant(int, 9)
C++ (Qt) for (int row=0;row<model->rowCount();++row){ for (int column=0;column<model->columnCount();++column){ //qDebug()<<"row"<<row<<"column"<<column<<model->item(row,column)->data(Qt::DisplayRole); qDebug()<<"row"<<row<<"column"<<column<<model->data(model->index(row,column),Qt::DisplayRole); } }