#include <QApplication>
#include <QTableWidgetItem>
#include <QTableWidget>
#include <QStringList>
void populateTableWidget(QTableWidget *tableWidget)
{
static const struct {
const char *ID;
const char *Name;
const char *Sex;
const char *Description;
int rating;
} staticData[] = {
{ "2008***1", "ZhangSan","Boy","Student",3},
{ "2008***2", "LiSi", "Woman","Teacher",5},
{ "2008***3", "WangWu", "Man","Leadership",6},
{ 0,0,0,0,0}
};
for (int row = 0; staticData[row].ID != 0; ++row) {
QTableWidgetItem *item0 = new QTableWidgetItem(staticData[row].ID);
QTableWidgetItem *item1 = new QTableWidgetItem(staticData[row].Name);
QTableWidgetItem *item2 = new QTableWidgetItem(staticData[row].Sex);
QTableWidgetItem *item3 = new QTableWidgetItem(staticData[row].Description);
QTableWidgetItem *item4 = new QTableWidgetItem;
// item4->setData(0,qVariantFromValue(StarRating(staticData[row].rating)));
tableWidget->setItem(row, 0, item0);
tableWidget->setItem(row, 1, item1);
tableWidget->setItem(row, 2, item2);
tableWidget->setItem(row, 3, item3);
tableWidget->setItem(row, 4, item4);
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTableWidget tableWidget(10,5);
tableWidget.setEditTriggers(QAbstractItemView::NoEditTriggers);
QStringList headerLabels;
headerLabels << "ID" << "Name"<<"Sex"<< "Description"<<"Rating";
tableWidget.setHorizontalHeaderLabels(headerLabels);
populateTableWidget(&tableWidget);
tableWidget.resize(550, 300);
tableWidget.show();
return app.exec();
}
还未完....