QListWidget not removing items on screen - qt

Im using QListWidget to show names as a type of dropdown when someone
types entries in another QLineEdit field. It hits the database and shows
all possibilities to choose from. As they type, the list changes, so I want
it to delete all entries and re-fill the QListWidget.
When I call the following code, it indeed empties the QListWidget list, but
the screen elements are still visible. Can someone help me figure out why
they arent being removed from the display? Im using Qt 4.8.4. Thank you!
void myClass::clearListWidget()
{
QListWidget * lw = m_ui->db_listWidget;
while(lw->selectedItems().size())
{
delete lw->takeItem(0);
}
lw->update();
lw->repaint();
qApp->processEvents();
}

Use slot void QListWidget::clear() to clear all contents. QListWidget documentation is here.

Related

Add a delete Button to each Item in QListView

Is it somehow possible to add to each Item in a QListview a Button which is deleting the Object onClick? As shown in the following Picture:
EDIT: As I'm new in QT it would be nice to have some example, to understand it better. And as it seems there are three different Ways? What will be the best? Do use a QAbstractItemView?
Yes. You'll need to use:
QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget )
QListView inherits QAbstractItemView and when you're trying to customize list/tree views that's usually the place to look. Be careful though, without a delegate this doesn't scale very well. Check out this thread: http://www.qtcentre.org/threads/26916-inserting-custom-Widget-to-listview
You can also go for a generic approach that can work on variety of containers, including the underlying model of your list view.
Each item in the list has a requestRemoval(Item*) signal and a removeMe() slot, connect the X button to the removeMe() slot in each item constructor, in removeMe() you emit the requestRemoval(this) signal, which is connected to a removeHandler(Item*) slot in your "parent" object upon creation of that item, which receives the pointer of the item which has requests deletion, and removes it from the underlying container being used.
Basically, pressing the remove button causes that particular item to send a pointer of itself to the parent's remove handler which removes that entry.
EDIT: Note that this is a generic approach, as noted in the comments below it can be applied without signals and slots as well, and even though it will work it is not the most efficient solution in your particular case.

QListWidget of QGroupBox selecteables

Hi i'm trying to create a series of QGroupBox for a number of categories which then should be able to be modified at the request of the user, via code, I use a QScrolArea to place there the QGroupBox, so I have not had any problem, the problem is that I need that are "selectable" so I decided to place them in a QListWidget using setItemWidget (), I have no problems with that, the problem comes when selecting these items, because when you hover over the QGorupBox the "box shown selection "but to click the item not selected, I looked at other similar post but none solved my problem, Is there an easy way and without many complications to do this?
Thank you for your attention
void VentPrinc::recibeCajaCategoria(QGroupBox *caja){
QListWidgetItem *test = new QListWidgetItem;
test->setSizeHint(QSize(232,97));
ui->listWidgetTest->setViewMode(QListView::ListMode);
ui->listWidgetTest->addItem(test);
ui->listWidgetTest->setItemWidget(test,caja);
}

Focusing on last data entry on Qtreewidget

I am trying to use Qtreewidget as listview (like in C#) to display some data. As seen on the image below, while new datas displayed during the runtime, the widget doesn't focus on the last entry.That is what I want but couldn't find a method to make it focus on the last entry. In other words, I want it to be scrolled down to the last entry. Is there any way to do it or do I have to use something another to handle this ?
Thanks in advance.
Maybe this code will helps you:
QTreeWidget *mytree = new QTreeWidget(this);
...
mytree->scrollToBottom();

How to delete widgets from a cell in a QGridLayout

I am not able to remove a particular widget from a cell in a qgridlayout. I tried several codes found in internet... but i failed!! the way how i did the work was, first i created a qwidget class containing button,qpixmap,qplaintextedit. i then created an object of this class and it was set dynamically on the QGridLayout. the layout was then set on the current widget using this pointer. I am able to addwidgets on the gridlayout, but not able to delete it.. i want to delete the whole widget i created only if the pixmap is null!!! Do anyone knows a suitable remedy for this problem??
To remove a widget without deleting it, call
void QLayout::removeWidget(QWidget*)
To remove and delete a widget, just delete it.

How can I tell a QTableWidget to end editing a cell?

I'm showing a popup menu to select some values in a QTableWidget. The lowest item is a "Modify list" entry, when I select it a new window should automatically appear and the QComboBox should vanish and the cell return to a Qt::DisplayRole state.
Now Qt has all those nice API-calls like QTableWidget.edit() and QTableWidget.editItem(), what I'm really looking for is a QTableWidget.endEditing(), preferably without specifying the index of the cell, though I could get that using this call:
table.currentIndex()
… but I don't know if I can guarantee that the current cell is the cell being edited at all times.
Is there an API to close those kind of editors?
QTableWidget inherits 19 public slots from QWidget. One of those is setDisabled(), which should disable input events for that widget and all of its children.
I would try:
table.setDisabled( true );
table.setDisabled( false );
Although you said it does not work for you, there is an alternative method:
If you don't like that (the table loses focus, I believe), you can try using EditTriggers. For example:
table.setEditTriggers( QAbstractItemView::NoEditTriggers );
table.setCurrentItem(None) is what worked for me. (Don’t forget to block signals if you use some cellChanged/itemChanged slot function.)
This is with PyQt. For C++ I think replace None with NULL.
You may be able to use QTableWidget.closePersistentEditor() to close the editor. However, QAbstractItemView.closeEditor() may be closer to what you want, especially since you seem to be comfortable with the QModelIndex-based API and are already using a custom editor widget.
In my case, none of the options worked properly. So, I figured: I need to send the key press event to the line edit itself. The following works with QTreeView but probably does work with any other view or widget that opens a line edit to edit cells.
QWidget* editingWidget = treeView->findChild<QLineEdit*>();
if(editingWidget)
{
QKeyEvent keyPressEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
QApplication::sendEvent(editingWidget, &keyPressEvent);
QApplication::processEvents(); // see note below
}
In my case, I wanted to start editing another field directly when having finished editing one item. That is why I put processEvents there, in most cases you can probably remove that line.
PS: yeah, it's C++, but should be easily adaptable to Python. I found this thread when I searched for the C++ solution, so maybe it helps anyone else, too.
I can't speak for list widgets. But, I got here trying to do something similar.
I was double-clicking a cell, and based on the column, bringing up a sub-form with a list, then when that was closed move to the next appropriate column based on the value selected.
My problem was I could get the value in the cell and "select" the next appropriate cell, but the original cell stayed selected in edit mode!
It finally dawned on me that my double-click was selecting the cell, ie. editing.
A single-click selects the cell but doesn't open an edit mode.
Side note: Never could get that sub-form to act truly modal, so I created a loop in the calling form: while the sub form was visible, with the only code being app.processEvents()

Resources