Will cell widgets inside QTableWidgets be deleted upon cleaning? - qt

Suppose I fill some cells of a QTableWidget with buttons:
ui->table->setCellWidget (i, 2, new QPushButton ("Details"));
Just a little bit later I scratch them all and do it again:
ui->table->clear();
Will this buttons get deleted automagically? Or do I need to store a list of them and delete manually? Also, Do I need to specify parent for every button like this: QPushButton ("Details", ui->table)?

The table widget takes ownership of the widget. The documentation states this:
Sets the given widget to be displayed in the cell in the given row and column, passing the ownership of the widget to the table.
You also don't need to specify a parent on creation, that will be set by the tablewidget accordingly.

Related

QListView and QAbstractListModel: how to manually set an item under editing, with cursor flashing?

I have a QAbstractListModel and QListView (for example, a list of input ports of an electronic device).
I've just added a row into the model, gave it a temp name (something like "RENAME_ME_PLZ_ASAP"). I want user to rename it, and I want to set the editing focus to this cell to make it possible to start typing the new port name without aiming the mouse to the cell added and double-clicking on it.
The editing of the item should begin, and its temp text contents should be selected (to be deleted by user when the typing will start).
How can it be done?
As you said you should should be able to edit, without aiming the mouse to the cell added and double-clicking on it.
Probably you can use QAbstractItemView::CurrentChanged edit trigger.
QListView *pListView = new QListView(<<Your parent widget>>); //rough
pListView->setEditTriggers(QAbstractItemView::CurrentChanged);
You have many edit triggers in below link, but I guess the above one suits you best.
http://doc.qt.io/qt-5/qabstractitemview.html#EditTrigger-enum
And if you are on embedded LINUX you can also use.
pListView->setEditFocus(true);
Well, I've just called QAbstractItemModel::edit(...) method.
^__^

NextChild Function in JavaFX?

I have three TextArea that are all children of an HBox and I have selected one of them. I was wondering if there was a function for selecting the next child.
I tried to use a for loop of the parent node but I couldn't find a way to select a specific TextArea that was the next child of the one I already selected.
// if you don't already have the index of the text field in its parent do:
int index = textField.getParent().getChildrenUnmodifiable().indexOf(textField);
// then
Node next = textField.getParent().getChildrenUnmodifiable().get(index+1);
Note though that in any typical setup, you won't really need these, as you would only have access to the text field within the same scope that you essentially added them to the parent. So you would "know" which text field followed which.

About deleting, removing widgets and layouts in Qt 4

(I use Qt 4.7, Windows 7, 64bit).
I created a custom table. Each row is a horizontal layout with widgets.
The rows are kept in a QList for easy access, and the children too. The rows are also added inside the parent widget.
If I resize the parent widget, I calculate the new sizes, delete everything, and recreate it again.
My problem is that I don't want to delete any widget. Only when I clear the table, I do it.
Since I have the widgets inside a QList and inside the parent layouts, How can I remove all widgets in each row, delete all layouts, and then add those to new layouts?
If I do: takeAt(0) for every element inside each layout I have a QLayoutItem with a widget inside... How can I delete the layoutItem without deleting the widget?.... How do I remove the widget without killing it, no matter if it's in the parent or the child? Because there are many methods for deleting: removeItem, removeWidget... in a layout, but not takeWidget... just takeAt() and it gives a Qlayoutitem.
I tried several ways, but I still see the widgets no matter what happened to them.
Questions about this:
When does a widget get deleted? If I takeWidget(index) from a layout, is it deleted some time by itself? does it happen if I have a pointer to it in another list?
removeAt(index) does execute the delete method of a widget?
Ok. I got it working.
Let me explain how this Removing, keeping widgets works.
A widget is known by its parent layout. And you remove it through the layout. By doing:
layout()->removeAt(widget);
delete widget;
If you use takeAt(index) in a QLayout (or its children), it gives you a QLayoutItem. To access the widget inside, just use widget(). But there's no way to remove the widget without deleting it. So this approach is non valid.
In the Docs it tells a way to delete the elements:
QLayoutItem *child;
while ((child = layout->takeAt(0)) != 0) {
...
delete child;
}
A special thing to note in Qt is the following:
If you have a hierarchy tree of layouts, added with addLayout() inside layouts, no matter how deep your widget is inserted, you can remove it from the child layouts or any of the parent layouts, if the tree path from the layout and this item is built from child layouts.
The easiest thing is to keep a list of pointers to all the items, in a custom table. When clearing the table to reconstruct it, just do this inside your widget:
CustomTableItem* item;
while ( !items_.isEmpty() && ( (item = items_.takeFirst()) != 0 ) ){
layout()->removeWidget(item);
delete item; // It works no matter where the item is
}
items_.clear(); // clear the list afterwards.
And it works perfectly, updates the layout too by itself.
If you want to keep the elements, just skip the "delete item;" and use them afterwards.
An important thing to note is that different "remove" functions work differently (as i understand on Qt Docs) in QList or similar widgets, and in a QLayout.
In the QList, removeAt actually removes the object.
(Qt 4.7 QList Docs)"Removes the item at index position i. i must be a valid index position in the list (i.e., 0 <= i < size())."
In a QLayout, removeWidget or removeItem don't remove the item/widget, you have the responsability to delete it, as I did before.
(Qt 4.7 QLayout Docs) "Removes the widget widget from the layout. After this call, it is the
caller's responsibility to give the widget a reasonable geometry or to
put the widget back into a layout."
Hope it helps. If you see any error, you could tell me and I will edit the answer!
More on deleting here:
Other stackoverflow post
A widget in Qt is a regular C++ object and can be deleted with the C++ delete operator as any other object:
delete myWidget;
In Qt there is always a parent-child relation between widgets. When the parent widget is destroyed, it will delete all its children. Usually, you do not need to think about explicitly deleting any widgets but the top level widgets, i.e., windows and dialogs. Qt will take care of deleting any child widgets.
QList::removeAt(int) does not delete the object that is removed, it only removes the object from the list. If you also want to delete the object you would have to do something like:
delete myList.takeAt(0);
This applies to all functions such as removeAt(int), takeAt(int), takeFirst(), etc. They never delete objects, they only remove them from the container (list, layout, scrollarea, etc). In most cases the ownership of the widget is then transferred to the caller, (the caller becomes responsible for deleting the widget as the parent-child relation breaks), but do not assume this is always the case, always read the documentation of the function.

How to set icon of the line edit in a QCombobox widget without inserting the item into the list?

I want to set the icon on the left side of the QCombobox widget. I know I can insert a item first and then set the icon of the inserted item and then select this newly inserted item. However, I would like to do that without inserting a new item into the drop down list for special reasons. Windows ComboBox control allows us to change the icon of the edit box by using an index of -1. I don't know how to achieve that with QCombobox.
Thanks for any comments!
Never tried it myself, but here is an idea.
QComboBox is based on Qt's model/view framework, so the items are contained into a QStandardItemModel which can be accessed with QComboBox::model().
The steps would be:
Instantiate a QStandardItem
Use setIcon() and setText() on the QStandardItem (or use the proper ctor)
When you want to add the item to the Combo list, append it thru the model.
Example:
QStandardItem* item = new QStandardItem(theIcon, theText);
[...]
QStandardItemModel* comboModel = qobject_cast<QStandardItemModel*>(theCombo->model());
comboModel->appendRow(item);

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.

Resources