c++ - 如何将qdate添加到qtableview

标签 c++ qt qtableview qdatetime

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

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

关于c++ - 如何将qdate添加到qtableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21228486/

相关文章:

c++ - 编译器有时可以缓存声明为 volatile 的变量吗

c++ - 删除字符串数组中的所有连续重复项

c++ - QDesktopServices::openUrl() 无法在 MAC Finder 中打开目录

c++ - 如何按列对 QTableView 进行排序?

python - PyQt - 重新实现 QSqlTableModel 数据方法时出现问题

c++ - 析构函数是否总是被调用删除运算符,即使它已过载?

c++ - 如何使用boost使类成员函数成为线程函数

qt - 我可以直接将 Qt 对象绘制到 Win32 DC(设备上下文)吗?

c++ - 有利于限制 Qt 对象的范围吗?

qt - 如何将 QTableView 滚动条位置更改为标题下方?