C++ Qt Table Rows Clearing When New Row Added - c++

I am using the following code to create an object and then add a row to a qt table and populate it.
However after adding the first new row, the next time the button is clicked and a new row is added all but the first column of the row above are cleared.
What am I doing wrong?
Thanks,
First Click Adds Row Just Fine
Second Click Clears Most Of The Row Above
void MainWindow::on_btnAdd_clicked()
{
Inventory i;
int it = ui->cboItem->itemData(ui->cboItem->currentIndex()).toInt();
double q = ui->spinQnty->value();
int l = ui->cboLoc->itemData(ui->cboLoc->currentIndex()).toInt();
Item item(it);
Location loc(l);
i.insert(it,q,l);
i.setItem(item);
i.setQnty(q);
i.setLoc(loc);
QTableWidgetItem *newItem1 = new QTableWidgetItem(QString::fromStdString(i.getItem().getItem_Name()));
QTableWidgetItem *newItem2 = new QTableWidgetItem(QString::fromStdString(i.getItem().getCategory().getCatName()));
QTableWidgetItem *newItem3 = new QTableWidgetItem(QString::fromStdString(cn.dbl_to_str(i.getQnty())));
QTableWidgetItem *newItem4 = new QTableWidgetItem(QString::fromStdString(i.getLoc().getLocName()));
QTableWidgetItem *newItem5 = new QTableWidgetItem();
newItem5->setData(Qt::UserRole,QVariant(i.getInv_ID()));
QIcon qi;
qi.addFile(QString::fromStdString("red_error_warning_icon.svg"));
newItem5->setIcon(qi);
int j = ui->mainTable->rowCount();
ui->mainTable->insertRow(j);
ui->mainTable->setItem(j,0,newItem1);
ui->mainTable->setItem(j,1,newItem2);
ui->mainTable->setItem(j,2,newItem3);
ui->mainTable->setItem(j,3,newItem4);
ui->mainTable->setItem(j,4,newItem5);
}

I ended up creating a function that adds rows in order to use it elsewhere in my program.
I found that I had column sort turned on in the ui file which was affecting my item inserts.
Here is the working code for adding rows which disables sorting while it adds items.
void MainWindow::addRow(int id)
{
ui->mainTable->setSortingEnabled(false);
Inventory i(id);
QTableWidgetItem *newItem1 = new QTableWidgetItem(QString::fromStdString(i.getItem().getItem_Name()));
QTableWidgetItem *newItem2 = new QTableWidgetItem(QString::fromStdString(i.getItem().getCategory().getCatName()));
QTableWidgetItem *newItem3 = new QTableWidgetItem(QString::fromStdString(cn.dbl_to_str(i.getQnty())));
QTableWidgetItem *newItem4 = new QTableWidgetItem(QString::fromStdString(i.getLoc().getLocName()));
QTableWidgetItem *newItem5 = new QTableWidgetItem();
newItem5->setData(Qt::UserRole,QVariant(i.getInv_ID()));
QIcon qi;
qi.addFile(QString::fromStdString("red_error_warning_icon.svg"));
newItem5->setIcon(qi);
int j = ui->mainTable->rowCount();
ui->mainTable->insertRow(j);
ui->mainTable->setItem(j,0,newItem1);
ui->mainTable->setItem(j,1,newItem2);
ui->mainTable->setItem(j,2,newItem3);
ui->mainTable->setItem(j,3,newItem4);
ui->mainTable->setItem(j,4,newItem5);
ui->mainTable->setSortingEnabled(true);
}

Related

QTableWidget with setItem cannot access the data in the tablewidget

void IdListForTrain::SetListIdInList(QStringList IdList)
{
QTableWidgetItem *item=nullptr;
for(int index=0;index<IdList.count();index++)
{
ui->Id_tableWidget->insertRow(index);
for(qint32 columnIndex=0;columnIndex<=1;columnIndex++)
{
item = new QTableWidgetItem;
if(columnIndex==0)
{
ui->Id_tableWidget->setItem(index,columnIndex,item);
item->setText("00:00:00");
}
if(columnIndex==1)
{
ui->Id_tableWidget->setItem(index,columnIndex,item);
item->setText(IdList.at(index));
}
QString tmp1 = ui->Id_tableWidget->item(index,1)->text();
QString tmp = item->text();
}
}
}
Hi! I am new to Qt and I'm facing a problem. I was asked to create a tablewidget with the effective date of train Ids in one column and the names of the trains in another column,I did this code to the best of my knowledge but I can't set or access the data in the widget in the line eventhough the code doesnt give any errors but it crashes everytime I open the application window at this line,
QString tmp1 = ui->Id_tableWidget->item(index,1)->text();
I'm not sure what the reason is.
Let look at first iteration - index=0 and columnIndex=0.
You go through if(columnIndex==0) condition and set item to
QTableWidget's row = 0 (index) and column = 0 (columnIndex)
Second condition skipped
You try to access item at row = 0 (index) and column = 1 with code QString tmp1 = ui->Id_tableWidget->item(index,1)->text();. But at this time you have no item assigned to that column
That's all!

Qt QTreeWidget alternative to IndexFromItem?

I have derived the class QTreeWidget and creating my own QtPropertyTree. In order to populate the tree with widgets (check boxes, buttons, etc) I am using the following code:
// in QtPropertyTree.cpp
QTreeWidgetItem topItem1 = new QTreeWidgetItem(this);
QTreeWidgetItem subItem = new QTreeWidgetItem(this);
int column1 = 0
int Column2 = 1;
QPushButton myButton = new QPushButton();
this->setIndexWidget(this->indexFromItem(this->subItem,column1), myButton);
QCheckBox myBox = new QCheckBox();
this->setIndexWidget(this->indexFromItem(this->subItem,column2), myBox);
This works fine, but the problem is that i want to avoid using the "indexFromItem" function since it is protected, and there are other classes that are populating the tree and need access to that funcion. Do you know any alternative to using that function?
You can try to use your QTreeWidget's model (QAbstractItemModel) to get the right index by the column and row numbers:
// Row value is 1 because I want to take the index of
// the second top level item in the tree.
const int row = 1;
[..]
QPushButton myButton = new QPushButton();
QModelIndex idx1 = this->model()->index(row, column1);
this->setIndexWidget(idx1, myButton);
QCheckBox myBox = new QCheckBox();
QModelIndex idx2 = this->model()->index(row, column2);
this->setIndexWidget(this->indexFromItem(idx2, myBox);
UPDATE
For sub items, the same approach can be used.
QModelIndex parentIdx = this->model()->index(row, column1);
// Get the index of the first child item of the second top level item.
QModelIndex childIdx = this->model()->index(0, column1, parentIdx);
The obvious solution is to de-protect indexFromItem like this:
class QtPropertyTree {
...
public:
QModelIndex publicIndexFromItem(QTreeWidgetItem * item, int column = 0) const
return indexFromItem (item, column) ;
}
} ;

empty row in QTreeWidget after item delete

I have created a QTreeWidget with Qt Creator and filled it.
When i delete an item from the tree,an empty line moves down to the end of the tree.
When i then add to the tree, the item adds below the empty line.
How do i get rid of this empty line?
Image: http://picpaste.com/gpa-GNtd84Ev.PNG
void Gpa::on_removeButton_clicked()
{
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
item = ui->treeWidget->currentItem();
QString txt = item->text(0);
//search vector for item
int i;
for(i = 0; i < vec.size(); i++)
if(vec.at(i)->getCode() == txt)
break;
vec.erase(vec.begin() + i);
int index = ui->treeWidget->indexOfTopLevelItem(item);
ui->treeWidget->takeTopLevelItem(index);
}
If you are removing a top level item, you should take a look at this method:
QTreeWidgetItem * QTreeWidget::takeTopLevelItem ( int index )
Otherwise, if you are removing a child you should get a pointer to the top level item (or a child of a top level item) and use:
QTreeWidgetItem * QTreeWidgetItem::takeChild ( int index )
EDIT (after OP has posted his code)
I believe the problem lies here:
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
item = ui->treeWidget->currentItem();
You are allocating a new QTreeWidgetItem, and then you assign the item you want to remove to that pointer. So basically, you are creating new empty item and you remove the old one. Try to change your code to:
QTreeWidgetItem *item;
item = ui->treeWidget->currentItem();

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