Prevent selected item from going out of view in QListView - c++

I am using a QListView with only single selection and selection changes handled by the up and down arrows. Everything is working as expected with one minor issue. If many items are added to the top of the list then the currently selected item risks moving down and out of view. Is there any way to detect when this happens? Thanks.

I found solution, but first of all I try to do dimilar thing, I run timer and every 2 second I insert new row to the begin of list and try to detect, is selected item still visible?
//somewhere in the constructor, it is not important
ListModel = new QStandardItemModel();
ui->listView->setModel(ListModel);
Now when I want to insert new row , I wrote code, which checks is the item visible
QStandardItem* Items = new QStandardItem(QString::number(qrand()%100));
ListModel->insertRow(0,Items);
// here we get rect of current selected item
QRect rec = ui->listView->visualRect(ui->listView->currentIndex());
//here we get all listView region, convert it to rect and check is our listView
//contain selected item
if(ui->listView->viewport()->visibleRegion().boundingRect().contains(rec))
{
qDebug() << "visible";//all good
}
else
{
qDebug() << "not visible";//not good, we need do something
//you want to detect it, so you get!
//now you know that item "runs away from you"
//and you can do some actions
someAction();
}
I test it on my computer, when I can see item, I get visible word, when item tries "run away from me" I get not visible
I hope it helps.

Related

How can use WM_LBUTTONUP with ON_WM_PARENTNOTIFY()

I have a window containing a list. When more than one item is highlighted I want a button to become visible to be pushed over another button. If it goes back to one or none highlighted I want the button to be disabled and the previous button to be shown again.
Currently I have a OnParentNotify function
if(message = LBUTTONDOWN)
int count = pMyListControl->GetSelectedCount();
if(count>1)
{
pTotalBox->ShowWindow(SW_HIDE);
pCheckBut->ShowWindow(SW_SHOW);
count=0;
}
else
{
pTotalBox->ShowWindow(SW_SHOW);
pCheckBut->ShowWindow(SW_HIDE);
count = 0;
}
This approach is working somewhat. If I click one item, nothing happens, as wanted. But if I select 2, nothing happens still. It isn't until I select another, after selecting at least 2, that it works correctly.
So I understand it is only getting the count of items prior to being clicked. I need it to get items already selected, AND items selected when performing a click.

Remove a row from a QStandardItemModel where selected row is a QTableView

I have a Qt project where I have a QStandardItemModel called stockModel. This model is linked to a QTableView called tvStock.
I have a button called btnDelete which has a clicked event set up as follows:
void StockItems::on_btnDelete_clicked()
{
//delete
}
How do I delete the selected row of tvStock from stockModel?
I assume I can start with this (but I'm not sure how to complete it):
stockModel->removeRow(/* What goes here? */);
Otherwise let me know if I'm completely on the wrong track.
Update:
I found and modified some code that allows to me to delete the row if the entire row's contents are selected:
QModelIndexList indexes = ui->tvStock->selectionModel()->selectedRows();
while (!indexes.isEmpty()) {
stockModel->removeRows(indexes.last().row(), 1);
indexes.removeLast();
}
Is there a way this code could be modified to delete the entire row if only one of the row items is selected?
Thanks!

Delete currently selected item from QTreeWidget

Im working with a Qt GUI application, and i have a QTreeWidget with values.
I have added each value to the tree like so:
QTreeWidgetItem *node = new QTreeWidgetItem();
node->setText(0, m_stringList[i];
node->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemisDragEnabled);
ui->sourceTreeWidget->addTopLevelItem(node);
What i am trying to implement now, is a delete button to allow the user to select one or multiple tree items by clicking them, and then pressing the delete button.
The button part is easy.
The part i need some help with, is finding out how to retrieve the string/text value of the currently selected tree item(s).
Anyone have some tips or hints?
What exactly is your problem with that? You create a SLOT for the button and retrieve a list of the selected items with
QList<QTreeWidgetItem*> sel_items = ui->sourceTreeWidget->selectedItems();
for(int i=0; i<sel_items.size(); i++){
...
}
as stated in the documentation for QTreeWidget. You may then iterate through the list and delete them directly or simply retrieve the string/text value as you asked.

QT check at least one row is selected QTableWidget

I need a piece of code to check that the user has selected at least one row in a QTableWidget
The QTableWidget can be referenced using ui->tableWidget.
I'm trying to check there are selecting, if not, display a message box, if so, moving on to code I have written.
Thanks.
You can obtain the selected rows from the selection model like:
QItemSelectionModel *selectionModel = ui->tableWidget->selectionModel();
QModelIndexList *selectedRows = selectionModel->selectedRows();
if (selectedRows.size() > 0) {
// There is at lease one selected row.
}

Scrolling QListView to keep items in view while inserting at start of list

I have a QListView with a model that I am inserting data into. I'm inserting data at the start of the list which causes all the items in the view to scroll down.
What would be the best way to scroll the view automatically to keep the view fixed (i.e the view should move with the visible items as new items are inserted in the model)?
This seems to work. The problem was not actually scrolling to the specific index - it was finding the correct index in the first place.
The following code scrolls the view to keep the same items in the view when inserting at the start of the list. It also checks if the view is at the top of the list and does not scroll in this case.
QScrollBar *pVerticalScrollBar = m_pUi->listView->verticalScrollBar();
bool bScrolledToTop = pVerticalScrollBar->value() == pVerticalScrollBar->minimum();
int iRowIndex = m_pUi->listView->indexAt(QPoint(8, 8)).row();
int iRowCount = m_pInfoListModel->rowCount();
/*
insert text into m_pInfoListModel here
*/
// move scroll bar to keep current items in view (if not scrolled to the top)
if (bScrolledToTop == false)
{
iRowCount = m_pInfoListModel->rowCount() - iRowCount;
m_pUi->listView->scrollTo(m_pInfoListModel->index(iRowIndex + iRowCount, 0), QAbstractItemView::PositionAtTop);
}
This gives me a list of items that scroll by if I am at the top of the list, but when I wan't to look at items lower down the view stays fixed.
I guess you could give setAutoScroll(True) to QListView instance.
I would do the following:
Find the list view item that I want to keep visible and than get its coordinates using
QRect QAbstractItemView::visualRect(const QModelIndex &index)
Than with the item's coordinates I will call QScrollArea::ensureVisible(int, int, int, int) function to keep the item visible after adding each new item to the list.