如何将qdate添加到qtableview

时间:2014-01-20 07:33:46

标签: c++ qt qtableview qdatetime

我想在我的表中添加QdateQTableview。问题是如果我将其转换为字符串我可以添加和检索数据。但我想仅在我的模型中存储日期

void MainWindow::setUpTabel()
{
   QDateTime myDate;
   myDate.setDate(QDate::currentDate());
   //myModel 
   QStandardItemModel model = new QStandardItemModel(this);
   QStandardItem *item = new QStandardItem;
   item.setData(myDate,Qt::UserRole);
   //Myview is also created and set the model to it
   m_tableView->setModel(model);
 }

问题是我无法在表格中看到日期。

1 个答案:

答案 0 :(得分:0)

正如文档所述,您必须将项目设置到模型中,指定要设置项目的行和列。

http://qt-project.org/doc/qt-4.8/qstandarditemmodel.html

修改您的代码:

void MainWindow::setUpTabel()
{
   int row = 0, column = 0; // here you decide where is the item

   QDateTime myDate;
   myDate.setDate(QDate::currentDate());

   QStandardItemModel model = new QStandardItemModel(this);
   QStandardItem *item = new QStandardItem(myDate);

   model.setItem(row, column, item);

   m_tableView->setModel(model);
 }
相关问题