Finding QModelIndex of new entry into Model - c++

I have a model that holds a sorted list of integers. New entries to the model look something like this:
if (!causesCollision(iCode))
{
beginInsertRows(QModelIndex(), this->rowCount(), this->rowCount());
this->_codes.append(iCode);
qSort(this->_codes);
endInsertRows();
return true;
}
I need the QListView that uses this model to automatically highlight new entries to the model, but currently I am unable to find the index of newly created row. this->_codes is a QList<int>.
First, I tried the rowsInserted(...) signal, but it only reports that a row was added at the end, not the position within the list that the new item was added.
I've tried something like this:
int iRow = this->_codes.indexOf(iCode);
QModelIndex index = this->index(iRow, 0);
emit ModelChanged(index);
With the ModelChanged(index) signal connected to the QListView's setCurrentIndex(index) slot, but it did not work.

If this->_codes is a list, you can easily get the index of the item you inserted after sorting. I would rewrite your insertion code in the following way:
if (!causesCollision(iCode))
{
// Calculate the insertion position in the list.
int row = 0;
while (row < _codes.size() && _codes.at(row) < iCode) {
row++;
}
beginInsertRows(QModelIndex(), row, row + 1);
_codes.insert(row, iCode); // Always insert sorted.
endInsertRows();
return true;
}
After this the rowsInserted() signal will indicate the right position where the new row has been inserted.

Related

How to get a specific cell when row is clicked in my qtableview

I am working with a qtableview that is filled by a model and has two columns. I can get the contents of a selected cell no problem but only want the contents of the second column even if the first column is clicked.
void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
QString cellText;
if (index.isValid()) {
cellText = index.data().toString();
}
ui->lineEdit->setText(cellText);
}
the index looks like this for column 0:
QModelIndex(7,0,0x55f2e5d06b00,QStandardItemModel(0x55f2e5d09740))
And for column 1:
QModelIndex(8,1,0x55f2e5d06b00,QStandardItemModel(0x55f2e5d09740))
I tried to find a way to change the index for the cell that is clicked but I think there is no way to change it directly and I can not seem to find a way to tell my function to always use column 1 the second column.
Thanks for your time.
Code edited to reflect comment 1 below
{
QString cellText;
if (index.isValid()) {
QModelIndex idx = index;
idx = idx.sibling(idx.row(), 1);
cellText = idx.data().toString();
}
ui->lineEdit->setText(cellText);
}```

QTableView: Removing Row from Model -> Empty row and "QSortFilterProxyModel: index from wrong model passed to mapFromSource"

I'm trying to implement a qTableView structure.
Here's part of my code:
m_model = new PatientModel(this); //This is my QAbstractTableModel subclass
m_proxy = new QSortFilterProxyModel(this);
m_proxy->setSourceModel(m_model);
To append a row I say (I want to display patient objects):
void PatientModel::append(Patient* patient) {
beginInsertRows(QModelIndex(), m_data.count(), m_data.count());
m_data.append(patient);
endInsertRows();
}
Which works fine. The row gets added to the view and the data (m_data is a QList of
To remove a row I tried several things, at the moment I have this
bool PatientModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
this->layoutAboutToBeChanged();
beginRemoveRows(QModelIndex(), row, row+count-1);
m_data.removeAt(row);
endInsertRows();
this->layoutChanged(); //force refresh, keine Ahnung
return true;
}
I added the layoutAboutTobeChanged() and the layoutChanged() after some research. Before adding these 2 lines there was an empty row after deleting. Now there is not but when I remove line 3 for example I can't first click on Line 3+ anymore or the app will crash with the following error message:
QSortFilterProxyModel: index from wrong model passed to mapFromSource
Segmentation fault: 11
Is there anything I'm doing wrong?
Thanks in advance!
Nevermind, I guess I did something wrong. Changed RemoveRows to this and now it works:
bool PatientModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
beginRemoveRows(QModelIndex(), row, row+count-1);
for (int i=0; i < count; ++i) {
m_data.removeAt(row);
}
endRemoveRows();
return true;
}

Selecting a row in QTableWidget with conditions to disable pushButton

I'm not sure if the title is correct but this is the situation:
When a row is selected and contains Application Status of FULL_MEMBER,
A pushButton is enabled else it is disabled.
You have to get the text of the item (cell) in the fifth column of the selected row, each time the selection changes.
Have a slot like this in your widget:
private slots:
void tableSelectionChanged();
In the widget constructor, connect it to the table widget signal itemSelectionChanged:
connect(ui->tableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(tableSelectionChanged()));
The slot definition is like this:
//retrieve a list of all selected rows
QModelIndexList list = ui->tableWidget->selectionModel()->selectedRows();
if(list.size() > 0)
{
//retrieve the index of the first (and only, maybe?) selected row
int row = list[0].row();
const int col = 4; //Application Status column id
QTableWidgetItem * item = ui->tableWidget->item(row, col); //the item we want to inspect
ui->pushButton->setEnabled( item->text() == "FULL_MEMBER" );
}

Adjusting QSqlTableModel for a QTreeView

I'm trying to put a MySQL table into a treeView.
Each entry has three values in the database - id, text, parentId.
This treeView needs to be editable so I really like the QSqlTableModel approach, as the functionality to save back to database is already built in.
Now, the treeView is showing all entries in the same line, of course, and I need a hierarchy. What would be the best way to build this hierarchy, while maintaining the editing and saving capabilities?
(I'm using MySQL.)
mainwindow.h
private: QSqlTableModel* goalModel;
mainwindow.cpp
(this makes a flat table hierarchy. the table is filtered based on which entry is clicked in another table)
void MainWindow::on_tableViewGoals_clicked(const QModelIndex &index)
{
goalModel = new QSqlTableModel(this);
goalModel->setTable("goals");
//Gets the id from the clicked entry to use as filter
QString idGoals = index.sibling(row,0).data().toString();
goalModel->setFilter( "id_goals=" + idGoals );
goalModel->setEditStrategy(QSqlTableModel::OnRowChange);
goalModel->select();
ui->treeView->setModel(goalModel);
...
I tried this. It's wrong, but I don't really know why. (the third column contains the parentId value, if the entry has it)
for (int row = 0; row < goalModel->rowCount(); ++row)
{
//if the entry has a value over 0 in the parentId column
if (goalModel->index(row,2).data().toInt() > 0)
{
//get number in column 2
int parentId;
parentId = goalModel->index(row,2).data().toInt();
QModelIndex newChild = goalModel->index(row,0);
//find QModelIndex row with matching number in column 0
for (int row = 0; row < goalModel->rowCount(); ++row)
{
if (goalModel->index(row,0).data().toInt() == parentId )
{
//make this entry that entry's child
QModelIndex newParent = goalModel->index(row,0);
newChild = goalModel->index(0,0,newParent);
}
}
}
}
All indexes are already made, so no need to make new ones, just assign a parent to all those who have one. How best to do that?

How to remove all rows and child rows from QTreeview

I don't know why I have trouble removing all rows and sub rows from qtreeview
I'm using QStandardItemModel as the model. Now here is my code that doesn't work.
What could be the problem?
QModelIndex FirstQModelIndex;
QModelIndex parentQModelIndex;
int iMdlChidCound = m_model->hasChildren();
if(iMdlChidCound > 0)
{
// only if there at list 1 row in the view
FirstQModelIndex = m_model->item(0,0)->index();
QStandardItem* feedItem = m_model->itemFromIndex(FirstQModelIndex);
// get the parent of the first row its the header row
QStandardItem* parentItem = feedItem->parent();
// here im getting exception
int parent_rows= parentItem->hasChildren();
parentQModelIndex = m_model->indexFromItem(parentItem);
// now i like to delete all the rows under the header , and its dosnt work
if(parent_rows>0)
{
bool b = feedItem->model()->removeRows(0,y,parentQModelIndex);
}
}
It seems like a lot of what you're doing is superfluous. If your only goal is to remove all the rows from the model, you could probably just use QStandardItemModel::clear
In your code you're jumping between the model and the items in a way you don't have to.
if(m_model->hasChildren()) {
m_model->removeRows(0, m_model->rowCount());
}
That should do what you're seeking.
QStandardItemModel::clear()
Which clears all the items including the header rows.