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.
Related
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!
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.
I need to add checkbox control to the listcontrol subitems .First I will let you know what I did,Initially i added a listcontrol of report style and added checkbox style to the listview as follows.
m_MfpListControl.SetExtendedStyle(LVS_EX_CHECKBOXES);
This step of code is adding checkboxes to the first most column.But,I want to add checkbox to the subitems randomly like as,
Column1 | Column2 | Column3 | COlumn4
[]Item1 | []subitem1 | []subitem2 | [] subitem3
[]-Represents Checkbox
Adding for the "Item1" is not a big deal but adding to the subitems making me down and moreover I tried in manny ways like as after inserting item ,I am setting the state for the particular item as,
m_MfpListControl.SetItemState(0,INDEXTOSTATEIMAGEMASK(3),LVIS_STATEIMAGEMASK);
But this also didn't work fine ,as I am able to add and remove the checkbox for the first column ,I tried in a way of applying the same methodology like setting the item state as I had done using "SetItemState()" API in order to add the checkbox control to the subitems ,unfortunately it is not working in case of subitems.
Can anyone please let me know the right approach so that I should be able to add checkboxes to the subitems.
I use this class to add check boxes on subitems:
http://www.codeproject.com/Articles/8112/CQuickList
It requires LVS_OWNERDATA. In my case, it's not a problem.
This class also adds check boxes on subitems, but without LVS_OWNERDATA:
http://www.codeproject.com/Articles/29064/CGridListCtrlEx-Grid-Control-Based-on-CListCtrl
Vinicius
From the book I'm reading:
By default, QListWidget is read-only. If we wanted the user to edit
the items, we could set the view's edit triggers using
QAbstractItemView::setEditTriggers(); for example, a setting of
QAbstractItemView::AnyKeyPressed means that the user can begin editing
an item just by starting to type.
So, I call the function in my code:
ui->listWidget->setEditTriggers(QAbstractItemView::AnyKeyPressed);
But when I select an item and start typing, nothing happens.
It turns out that the items themselves also have an editable flag, so after adding them I had to iterate all of them and set it. Now it's working.
// set the editable flag for each item
for (int ii = 0; ii < ui->listWidget->count(); ii++) {
ui->listWidget->item(ii)->setFlags(ui->listWidget->item(ii)->flags() | Qt::ItemIsEditable);
}
// set the editable triggers for the list widget
ui->listWidget->setEditTriggers(QAbstractItemView::AnyKeyPressed);
I want to create a QTreeWidget where the items are both editable and launchable. I want it to behave like Windows Explorer:
Single click -> selection
Single click on a previously selected item -> open LineEdit to edit the name
Double click -> perform the 'launch'
So I created slots for itemClicked() and itemDoubleClicked(). The first one is the following:
def EditName(self, item, column):
if self.lastclick == item:
self.editItem(item)
self.lastclick = item
The second one just 'launches' the file.
However, this kind of solution doesn't distinguish between a double click and two consecutive clicks, so the QLineEdit still appears after a double click. Is it possible to get rid of the editor forcibly? I tried a hack solution like hiding and showing the item but it didn't work.
You just need to set flags on your QTreeWidgetItem to include the ItemIsEditable option, and set the edit triggers on the QTreeWidget for SelectedClick
def populate( self, tree ):
tree.setEditTriggers(tree.SelectedClicked)
for i in range(10):
item = QTreeWidgetItem(['Testing %02i' % i])
item.setFlags(item.flags() | item.ItemIsEditable)