如何将QDATE添加到QTableView

How to add qdate to qtableview

本文关键字:QTableView 添加 QDATE      更新时间:2023-10-16

我想在表中添加 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);
 }

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

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

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);
 }