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

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()

Related

How do I get rid of this anonymous draggable bar in qtcreator?

I wrote a program in qt creator, but it includes this anonymous draggable toolbar that I would like to get rid of. It does not show up in design mode. If I right click on it, it gives me a menu with a checkbox, which I can select to get rid of it while the program is running. What is this bar? How do I get rid of it? If I knew what it was, I could just programmatically tell it to hide when the program loads.
Here are some screenshots demonstrating this weird problem. In the first screenshot, the mouse is pointed at the bar. The second one demonstrates the right-click context menu. The third one shows the program without the bar. The box can also be dragged out of the program's window entirely. I can also upload a picture of that, if necessary.
(mouse pointing at bar)
(bar has a right-click menu!)
(bar is gone)
First things first, make sure you haven't added the toolbar yourself in code (the Designer form is nothing magical, it just generates normal Qt code to ui_XXXX.h, which you should totally study, so you understand what is really happening "under the hood").
Second, perhaps that is the main menu bar, not the main tool bar.
Then, on to remove the nasty bar. On the upper right corner of Design view, you have a tree of all items in your UI. Find the QToolBar there. It is probably called mainToolBar. Right click it and remove it there. Or, if you actually have menu bar, then find QMenuBar, probably called menuBar, and remove it instead. Remember to make version control commit (or other backup) before doing this, so you can easily revert changes.
If this fails for whatever reason, and you just want to get it to work, you can always find and delete (be careful about the dangling pointers left behind, set them to nullptr if possible):
ui->setupUi(this);
// a hack: we want to get rid of the toolbar and the menubar,
// but can't remove it from the .ui, so we delete them like this.
delete findChild<QToolBar *>(); // NULL return value is ok for delete
delete findChild<QMenuBar *>(); // NULL return value is ok for delete
Doc links for findChild and alternative findChildren.
What you have there is probably the mainToolBar QToolBar object you get with every QDesigner-created QMainWindow .ui file.
If you want to get rid of it, locate it in your .ui file and just delete it.
But better think twice: you might later decide you want a QToolBar object and (at least AFAIK) there is no way to recreate it in QDesigner...
you must use this code in your constructor.
ui->mainToolBar->toggleViewAction()->setVisible(false);
mainToolBar is my toolbar name.

Delaying the opening of a NSPopUpButton

I've encountered the same problem as this one:
This one.
It has never been answered so far. In two words:
When you place a NSPopUpButtonCell in a NSTableView, the popUp opens before the selection in the tableView has changed.
If the menu of the popUp relies on the selection of a row of the tableView, it will be updated too late.
Solution 1: differ the opening of the popUp with a block which calls [super] a few nanoseconds later. But I don't know which method I must override.
Solution 2: use other bindings that I'm currently using (the popUp contains the NSSet of the entity selected in the table, I'm using a different controller, not the table's one, and I bind its content set to myTable.selection.theSet.
Is there something else I could do? Thanks !
Yes, if you use a view-based tableView, you can bind the popUp through the cellView it’s inside (like the other table objects you bind in this mode), and so it’s completely unambiguous and doesn’t depend on the selection.
In view-based mode, each cell in each row have is assigned an ‘objectValue’ from your original data, so you can bind directly to it, like ‘objectValue.popUpContents’.

How to set QTableView as a cell of QTableView

I have QTableView - mainTableView, in some cells of which I need to show another QTableView - cellTableView.
Actually I found 2 ways:
1) Use pTableView->setIndexWidget
2) Use pTableView->openPersistentEditor and implement an ItemDelegate.
I have tried both ways and the problem is just the same. When I use an ItemDelegate without openPersistentEditor everything works good. Otherwise, as I understand, there is a problem with focus and with HeaderView of the table in a cell.
I would like to solve the following problems:
sometimes instead of a HeaderView of cellTableView, background of mainTableView is shown.
When I press a key on a cell of cellTableView, editor for this cell is not opened but the pressed symbol is set to the cell (editor opened->editor receives "FocusOut" signal->editor is closed, pressed value is stored to the cell).
With space key or double click editing works good.
Someone could me explain how to solve this behavior?
I have found this solution.
http://qtadventures.wordpress.com/2012/02/04/adding-button-to-qviewtable/
For my case I need even less and it works just great!
I switch a cell to the edit mode manually, when the cell is selected.

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 do I tell Qt to always show an editor in a QTableView?

I've got a QTableView for which I want to display the last column always in edit mode. (It's a QComboBox where the user should be able to always change the value.)
I think I've seen the solution in the Qt documentation, but I can't find it anymore. Is there a simple way of doing it?
I think I could archive this effect by using openPersistentEditor() for every cell, but I'm looking for a better way. (Like specifying it only one time for the whole column.)
One way to get the automatic editing behaviour is to call the view's setEditTriggers() function with the QAbstractItemView::AllEditTriggers value.
To display the contents of a given column in a certain way, take a look at QAbstractItemView::setItemDelegateForColumn(). This will let you specify a custom delegate just for those items that need it. However, it won't automatically create an editor widget for each of them (there could in principle be thousands of them), but you could use the delegate to render each item in a way that makes it look like an editor widget.
There are two possibilities:
Using setIndexWidget, but Trolltech writes:
This function should only be used to
display static content within the
visible area corresponding to an item
of data. If you want to display custom
dynamic content or implement a custom
editor widget, subclass QItemDelegate
instead.
(And it breaks the Model/View pattern…)
Or using a delegate's paint method. But here you have to implement everything like enabled/disabled elements yourself.
The QAbstractItemModel::flags virtual function is called to test if an item is editable (see Qt::ItemIsEditable). Take a look at Making the Model Editable in the Model/View Programming documentation.
I can't see an easy way to do this, but you might be able to manage by using a delegate. I honestly don't know exactly how it would work, but you should be able to get something working if you try hard enough. If you get a proper delegate, you should be able to set it on a whole view, one cell of a view, or just a column or row.

Resources