我想将 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/