How to add qdate to qtableview - c++

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.

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

Related

editing treeview item do not automatically updates database

I have a qtableview and Treeview in my program.
qtableview code is:
void MainWindow::tableShow(QString mystrings)
{
testModel = new QSqlTableModel(this);
testModel->setTable("parts");
if (mystrings=="")
{
testModel->setFilter("");
}
else
{
testModel->setFilter("type='"+mystrings+"'");
}
testModel->select();
ui->tableView->setModel(testModel);
}
it works just fine and the very interesting ability it has is you can edit cells and database updates automatically.
Now I have made a TreeView with this code:
void MainWindow::treeView2()
{
QStandardItemModel *myModel= new QStandardItemModel;
QStandardItem *item = new QStandardItem();
item = myModel->invisibleRootItem();
myQuery.exec("select groupName from maingroup");
while(myQuery.next()){
QString temp =(myQuery.value(0).toString());
QStandardItem *parItem= new QStandardItem(temp);
item->appendRow(parItem);
QSqlQuery childqry;
childqry.exec("select sub from subgroup where main = '"+temp+"'");
while(childqry.next()){
QStandardItem *childItem= new QStandardItem(childqry.value(0).toString());
parItem->appendRow(childItem);
}
}
ui->treeView->setModel(myModel);
ui->treeView->expandAll();
}
but editting item in this treeview do not updates the database and after restarting the program the item is like before. what should i do to automatically update database with new value that edited in treeview?
thanks in advance.

Add items to columns in QStandardItemModel

I am currently adding rows to my QTableView as such
QStandardItem* itm;
QStandardItemModel* model = new QStandardItemModel(this);
model->setColumnCount(2);
model->appendRow(new QStandardItem("Some Text in Column1");
How do I add items to column 2 dynamically by appending?
In the above example column 2 is empty. How do I add item to column 2?
Calling appendRow(QStandardItem *) only adds a single item to the first column. You would need to pass in a QList to appendRow() to add items to each column, e.g.:
QList<QStandardItem *> items;
items.append(new QStandardItem("Column 1 Text"));
items.append(new QStandardItem("Column 2 Text"));
QStandardItemModel* model = new QStandardItemModel(this);
model->setColumnCount(2);
model->appendRow(items);
See http://doc.qt.io/qt-5/qstandarditemmodel.html#appendRow for more detail.

Qt: set columns in treeView

How can I implement this code I have for a qTreeWidget for a qTreeView?
for (const auto & i : names) {
QTreeWidgetItem * item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0, QString::fromStdString(i));
ui->treeWidget->addTopLevelItem(item);
const std::unordered_map<std::string, double> map = m_reader.getMapFromEntry(i);
for (const auto & j : map) {
QTreeWidgetItem * item2 = new QTreeWidgetItem();
item2->setText(0,QString::fromStdString(j.first));
item2->setText(1,QString::number(j.second));
item->addChild(item2);
}
}
I have a model and a treeView, like this:
m_model = new QStandardItemModel(m_reader.getAllNames().size(),2,this);
ui->treeView->setModel(m_model);
I tried this, but that only shows one column:
QStandardItem * parentItem = m_model->invisibleRootItem();
for (const auto & i : names) {
QStandardItem * item = new QStandardItem(QString::fromStdString(i));
parentItem->appendRow(item);
const std::unordered_map<std::string, double> map = m_reader.getMapFromEntry(i);
for (const auto & j : map) {
QList<QStandardItem *> rowItems;
rowItems << new QStandardItem(QString::fromStdString(j.first));
rowItems << new QStandardItem(QString::number(j.second));
item->appendRow(rowItems);
}
}
With the treeWidget, I had so set the columnCount, like this:
ui->treeWidget->setColumnCount(2);
But treeView does not have a method like this.
So, to summarize: How can I implement a TreeView with more than one column?
EDIT:
To clarify, I want something like this:
|-A
| |-B-C
| |-D-E
where A is the parent and B,C,D,E the children, with B,D being in column 0 and C,E in column 1.
Hope this helps!
To support multiple columns, the model must contain data for multiple columns.
So in some sense, columns are a property of the model, not the view. Views then can decide to hide or rearrange certain columns (For example, a QListView always only shows the first column, while one can hide or reorder columns in a QTableView).
As you use QStandardItemModel, its documentation should give a few hints how to create multiple columns.
E.g., look at this example from the documentation:
QStandardItemModel model(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model.setItem(row, column, item);
}
}
It creates a model with 4 initial rows and columns each, and then fills it with items via setItem().
Alternatively, you can pass a list of items to QStandardItemModel::appendRow(), with an item for each column:
QList<QStandardItem*> items;
items.append(new QStandardItem(tr("One"));
items.append(new QStandardItem(tr("Two"));
model->appendRow(items);
This adds a new row with "One' in the first column and "Two" in the second. For even more ways to deal with multiple columns, see the QStandardItemModel docs.
Note: QTreeView expects the same number of columns on all levels of the hierarchy, so one should fill rows with empty items for the unused columns if need be.
Just an addition to answer by Frank Osterfeld:
QTreeView displays all columns of subtables inserted into top level QStandardItems. You just have to "force" it to show additional columns by inserting dummy QStandardItems into top-level table. Example:
QStandardItemModel *objectTreeModel = new QStandardItemModel(NULL);
QStandardItem *mainItem = new QStandardItem(tr("Main Item"));
QStandardItem *subItem1 = new QStandardItem(tr("Sub-Item 1"));
QStandardItem *subItem2 = new QStandardItem(tr("Sub-Item 2"));
mainItem->appendRow(QList<QStandardItem *>() << subItem1 << subItem2);
QStandardItem *dummyItem = new QStandardItem();
objectTreeModel->appendRow(QList<QStandardItem *>() << mainItem << dummyItem );
Now you will be able to see 2 columns and if you expand mainItem, both subitems will be visible.

Qt hide column in QTableView

I want to hide the ID column in the QtableView and i can't do that on my implementation. Can anyone help me?
void MainWindow::on_actionClear_Search_triggered()
{
model = new QStandardItemModel(cars.size(),6,this);
//create header
createHeader(model);
//set data to the table view
populate(cars);
ui->tableView->setColumnHidden(6,true);
ui->tableView->setModel(model);
}
void MainWindow::createHeader(QStandardItemModel *model){
model->setHorizontalHeaderItem(0,new QStandardItem("Car"));
model->setHorizontalHeaderItem(1, new QStandardItem("Type"));
model->setHorizontalHeaderItem(2, new QStandardItem("Mileage"));
model->setHorizontalHeaderItem(3, new QStandardItem("Year"));
model->setHorizontalHeaderItem(4, new QStandardItem("Is registered"));
model->setHorizontalHeaderItem(5, new QStandardItem("ID"));
}
void MainWindow::populate(const QList<Vehicle> &vehicles)
{
int j = 0;
QList<Vehicle>::ConstIterator iter;
for( iter= vehicles.begin(); iter != vehicles.end(); iter++){
const Vehicle& car = *iter;
//set car
QString makeAndModel = car.getGeneralData().getMake() + car.getGeneralData().getModel();
QStandardItem *mAndM = new QStandardItem(QString(makeAndModel));
mAndM->setEditable(false);
model->setItem(j,0,mAndM);
//set type
QStandardItem *type = new QStandardItem(QString(car.getGeneralData().getType()));
type->setEditable(false);
model->setItem(j,1,type);
//set mileage
QString mileageString = QString::number(car.getGeneralData().getMileage());
QStandardItem *mileage = new QStandardItem(QString(mileageString));
mileage->setEditable(false);
model->setItem(j,2,mileage);
//set year
QString yearString = QString::number(car.getGeneralData().getYear());
QStandardItem *year = new QStandardItem(QString(yearString));
year->setEditable(false);
model->setItem(j,3,year);
//set registration
QString regString = VehicleHelper::convertBoolToString(car.getRegistration().isRegistered());
QStandardItem *regDate = new QStandardItem(QString(regString));
regDate->setEditable(false);
model->setItem(j,4,regDate);
//set ID column
QStandardItem *idNumber = new QStandardItem(QString(car.getVehicleID().getID()));
idNumber->setEditable(false);
model->setItem(j,5,idNumber);
j++;
}
}
You use ui->tableView->setColumnHidden(6, true);, but there is no column with index 6. You should write ui->tableView->setColumnHidden(5, true); instead, because ID column number is rather 5 than 6.
UPDATE:
You also need to hide column(s) after you set the model to the view, i.e:
ui->tableView->setModel(model);
ui->tableView->setColumnHidden(5, true);
Another approach is set specified column's width to zero : ui->tableView->setColumnWidth(col,0); ui->tableWidget->setColumnWidth(col,0);.
Ui->tableView->horizontalHeader()->hideSection(col);
where col - number of table column

Retrieving a QStandardItem through QStandardItemModel by searching or key

Is there any way to assign a unique key to an entry in a QStandardItemModel so that we can check for the presence of that key. If it is present we get the relevant QstandardItem ?
Update:
Here is what I am trying to do. I have 3 column in my table so so i have 3 QStandardItem.
This is the code I am using:
QStandardItem* item0 = new QStandardItem("Column1");
QStandardItem* item1 = new QStandardItem("Column2");
QStandardItem* item2 = new QStandardItem("Column3");
Now my model is called model and I am attaching these to my model as such
moddel->setItem(0,0,item0);
moddel->setItem(0,1,item1);
moddel->setItem(0,2,item2);
I need to assign a row some unique key so that I could check the model for that key and the model would return the row number. Any suggestions.
You could use the setData function of QStandardItem in order to set a custom key for a user defined role, eg
#define MyRole Qt::UserRole + 2
myItem->setData(Qvariant(key), MyRole)
You can get the data of any index in your model by using the data call.
QVariant d = mymodel->data(anindex, MyRole)
Writing a function that checks if a key exists should be straight forward.
The answer by pnezis addresses the storing of a key but not the accessing of a QStandardItem from the model. I addressed the storing of data with a QStandardItem by sub classing QStandardItem as I needed to store a lot of complex data.
To obtain the QStandardItem from the model you need to create a QModelIndex instance with the row/column and then call itemFromIndex(index)
on the model.
My example is taken from a selection callback.
QModelIndex& selectedItem = itemsSelected.front();
QStandardItemModel* model = reinterpret_cast<QStandardItemModel*>(tableView->model());
if (nullptr == model)
return;
QStandardItem *item = model->itemFromIndex(selectedItem);
if (nullptr == item)
return ;