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

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.
^__^

Related

How did I retrieve changes in the content of all text edit boxs in a widget

Now, I have a widget that used for conguring some parameters, There were some QlineEdit with default value and a save button on this widget. People may change the content of QlineEdit. And click the save button, so that the modified parameters can take effect. Here is my question:
How do retrieve changes in the content of all text line edit in this QWidget?
Once I know which edit content has changed, I can judge whether the modified values is legal, and then let the change take effect.
Can anyone give me some ideas?
OS: Windows10
QT: qt 5.9.0
For this situation, It's better to do one more step before manually validating the user's input. The step is to limit the user to enter invalid settings. If your setting value is a number, use QSpinBox or QDoubleSpinBox for floating-point values. If you want to let the user select from multiple predefined values, like gender(Male, Female), use QComboBox or QRadioButton and so forth. Here is the list of Qt's widgets. So bear in mind, using QLineEdit for all of the inputs is not a good idea.
If your input is something more complex, you can use validators. For getting the idea see this question.
At last, you connect the save button's clicked signal to the slot defined in your widget class using Qt's signals and slots mechanism and get values from all of your inputs and check them, and if everything is OK, apply them to your system.

Putting dynamic data in QT and can selection it

I want to do the next.
I have a program that must search and show some files. Before, I will do it with a QTextEdit, searching in the system and appending it when I found one.
Now I want to to the next: I want to show the name of the files but I want to select it in the GUI and, in anohter text edit, show the first line in the document.
So, I want to transform any file in something that I can select it (like a Radio Button, a check button or something like this).
I search info in the web but I dont found anything.
Anyone knows what I can do?
Usually you use something like a list view (list of selectable items) to do this. Fortunatley Qt has one! called QListView :)
See this link: QListView
You add one entry to the listview per file (for exampe). And then when you click/select an entry this triggers an event which you can make display the contents of the file in a nearby text box.

Accept edited wigets with QDataWidgetMapper

I'm making a small application that represents a to-do list.
The list is stored in a cusom model (derived from QAbstractItemModel) and is displayed in a QTableView.
I have also a custom widget (TaskDialog) that is used to browse and edit tasks from the list. The children of the TaskDialog are mapped to the model with a QDataWidgetMMapper.
On the task dialog I have also buttons that allow browsing through all the tasks in my to-do list (QDataWidgetMapper::toPrevious() ...).
The problem is that if I'm editing a task in the TaskDialog (e.g. the tasks name) and then browse to another task, the new edited enty isn't set in the model.
But if I edit something in the TaskDialog, then first change the focus to an other child widget of the TaskDialog and then press one of the browsing button, than the changes are written to the model.
How can I achive that widgets that are in a editing mode are accepted and written to the model.
I also tried the example of QDataWidgetMapper from Nokia. There it works perfectly. But they use a QStandardItemModel.
Best regards
Luke
I found a solution:
The submit policy of the QDataWidgetMapper has to be set to QDataWidgetMapper::ManualSubmit.
Then allways when I browse to a new item (i.e. one of the browse buttons is pressed) i submit the changes with QDataWidgetMapper::submit().

Show Editors for All Cells in Row in QTableView

I would like to display editors for all cells in a row when a user begins editing any cell in a QTableView. I have made several attempts but I cannot obtain the correct behaviour.
The only way to open multiple editors is by QAbstractItemView::openPersistentEditor() - attempts to successively call QAbstractItemView::edit() results in only one editor.
I cannot use signals such as clicked() and doubleClicked() from QAbstractItemView to invoke editing, because then it would not respect the edit triggers of the view.
There appears to be no "editing complete" signal. I would like to connect this signal to a slot that calls closePersistentEditor() for cells in the editing row.
Any suggestions would be appreciated.
Thanks!
I hate to be the bearer of bad news, but I can't think of any easy way to do what you want. I can think of a couple of options, each more painful than the last:
You could create a delegate that always shows the editors, and when the user changes the selected row, set that delegate for the newly selected row, and the original delegate for the deselected row.
You could try inheriting from the table view, and overriding the behavior for drawing the appropriate items for everything in the given row. I have no idea how hard this would be, but I doubt it would be trivial.
You could create your own view to display the model. I've never done this, and I'd hate to think about all that would be required to "complete" support the models. However, to match with one specific model, you might be able to get away with 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