How to add qdate to qtableview

1.3k Views Asked by At

I want to add Qdate to my table say QTableview.The problem is if i convert it into string i can add and retrieve the data.But i want to store as date only in my model.

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

The problem is i'm not able to see the date in my table.

1

There are 1 best solutions below

0
On

As the documentation says, you must set the item into the model specifying the row and columng where you are going to set the item.

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

Modifying your code:

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