PDA

View Full Version : deleting selected headers



ru_core
15th April 2008, 20:55
Hello everybody,
Have a trouble:

I have to delete selected headers in the tableView model. To get this I create two slots:

slotHeadersToBeDeleted slot checks whether selected Item was selected before. If yes (was selected and clicked again then it should be deleted from the vector for deleted headers) other way it should be added to the vector for deleted headers. When the necessary headers have been selected the user should push the button “deleteButton” and the headers are to be deleted.


Class1
{
…
QVector<int> m_vHeadersToBeDeleted;
QStandardItemModel m_pDataModel;
…
};
…

void Class1:: slotHeadersToBeDeleted(int LogicalIndex)
{
bool headerStatus = false;
QVector<int>::iterator i = m_vHeadersToBedeleted.begin();
while (i < m_vHeadersToBedeleted.end())
{
if (*i == LogicalIndex)
{
m_vHeadersToBedeleted.erase(i);
headerStatus = true;
}

i++;
}
if (headerStatus) m_vHeadersToBedeleted.push_back(LogicalIndex);
}

void Class1::slotDeleteSelectedItems()
{
QVector<int>::iterator i = m_vHeadersToBedeleted.begin();
while (i < m_vHeadersToBedeleted.end())
{
m_pDataModel->removeRow(*i);
i++;
}
}


and in the constructor of class Class1 I created two connections:


connect(m_pTable->verticalHeader(), SIGNAL(sectionClicked(int LogicalIndex)), this, SLOT(slotHeadersToBedeleted(int LogicalIndex)));
connect(m_pDeleteButton, SIGNAL(clicked()), this, SLOT(slotDeleteSelectedItems()));

where m_pTable is QTableView object
m_pDeleteButton is a QPushButton

the headers are not deleted. Where am I mistaken?
Thank you.

wysota
16th April 2008, 00:36
First of all I'd use i!=m_vHeadersToBedeleted.end(). Second of all if you start deleting rows then their internal numbers are going to change, so your list of rows to delete won't make sense unless it's sorted and you iterate in descending order (which does not seem to be the case here). Third of all what is the type of m_pDataModel?

ru_core
16th April 2008, 06:23
thank you for the reply.

m_pDataModel has QStandardItemModel Class. m_pDataModel is set as

m_pTable->setModel(m_pDataModel);

wysota
16th April 2008, 08:53
Ok, so correct the remaining problems and then we'll continue.