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!
Related
I have QTableWidget with specific row and column. When I click on empty cell a text opens and let user to write on cell. Is there any way to avoid this situation, I mean, to avoid adding text on empty cell?
UPDATE:
I added this part to slot of cellclick(), but empty cells are yet editable,
QTableWidgetItem *item = filesTable->item(row, column);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
Is there any way to force empty cells to be read-only?
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.
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 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.
}
I am creating a model using this code:
QStandardItemModel table_model(4,4);
for(int row=0; row<4; row++){
for (int column=0; column<4; column++){
QStandardItem* item = new QStandardItem((QString("100")));
table_model.setItem(row,column,item);
}
}
Then, I am passing it to QTableView as follow:
QStandardItemModel* model = &table_model;
ui->table->setModel(model);
ui->table->show();
However something is wrong. The QTableView shows nothing, just white space all over it. yet, if, and only if, I click on the trigger button (which creates the model and links it to the QTableView) I can see the 4x4 table, and yet without any data. immediately after the "click" everything disappear (it is just the moment of clicking the button) ..
so what am I missing ? – thanks
You create your model on stack and pass a pointer which gets invalid right after you leave method where you declare it.
QStandardItemModel * table_model = new QStandardItemModel(4,4);