QTableWidget and edit mode
-
@JonB I check flag
if(!secFunc->getIsBigMemory())
If condition is true, other rows not editable.
Regarding editing. The matter is that I have 2 modes: in one all lines are available, in another only one. And yes, I need to change the values of each cell in a row. The bottom line is that I need to be able to open the editor of all cells by pressing a key from the keyboard, and not by double clicking the mouseIt may be clearer if I say that this is conditionally the memory of a certain processor, into which I write commands to perform tasks (I write an emulator)
-
@JonB
By the way, I have custom delegate:QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QLineEdit *editor = new QLineEdit(parent); editor->setStyleSheet(StyleHelper::getEditorItemStyle()); connect(editor, &QLineEdit::editingFinished, this, &MyDelegate::commitAndCloseEditor); (void) option; (void) index; return editor; } void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor); lineEdit->setInputMask("HH"); lineEdit->setText(index.data().toString()); } void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor); model->setData(index, lineEdit->text()); } void MyDelegate::commitAndCloseEditor() { QLineEdit *editor = qobject_cast<QLineEdit *>(sender()); emit commitData(editor); emit closeEditor(editor); }
Maybe it's help you :)
-
@Vlad02 said in QTableWidget and edit mode:
Maybe it's help you :)
That may save me all the work I am halfway through!
Since you already have the
createEditor()
delegate written, all I want you to do there is something like:QLineEdit *editor = new QLineEdit(parent); QTimer::singleShot(100, this, [editor]() { editor->deselect(); } );
That's the idea, does it work? [I have tried this now under Ubuntu 22.04, Qt 5.15, and it works as intended.]
-
@Vlad02
This is because you are Qt 5.6 (why such an old version??). I still think you're supposed to havevoid QTimer::singleShot(int msec, const QObject *context, Functor functor)
and I thought my lambda would match as a
Functor
? Sigh. Do you really need to stick on Qt 5.6, it's not a good idea...?You will have to try something like:
QTimer::singleShot(100, [editor]() { editor->deselect(); } );
But I suppose that gives the same error message??
You need a lambda here really to pass the
editor
in context. I never used Qt 5.6 and I don't know what is acceptable for that, maybe an [old!] expert will see and comment...? -
@JonB
No, this is work, thanks!!!But what if, for example, I need to select the text of only the currently selected cell, but at the same time put everything in edit mode? And while doing this, I want to switch between cells through the Tab key. And when you switch to a cell, so that the text is highlighted completely in it. Is it difficult to implement?
I highlighted this with the mouse, but I would like this when switching between cells via Tab -
@Vlad02 said in QTableWidget and edit mode:
I need to select the text of only the currently selected cell, but at the same time put everything in edit mode?
For that one, in
createEditor()
detect whetherindex
refers to "the currently selected cell" and do not do thesingleShot()
to clear the selection for that one.You are asking me too many questions now! I don't know what the Tab key does, I don't know whether that highlights all the content of the new cell or not, and I don't know whether you want the opposite behaviour! I have shown a possible approach using a delay timer to deselect/select in a
QLineEdit
, maybe you need something like this in your Tab case too. You will have to play. -
@Vlad02
If necessary explain exactly what you want now, because I don't understand from what you wrote. So far as I know the Tab key will move to edit the next editable field, does it do that? If then you want it to select or deselect the whole of the new cell's edit's content, I think theQLineEdit
will get some "focus" event or signal? Maybe you could put something on that to then do de/selection, if that is the issue? -
@JonB said in QTableWidget and edit mode:
Tab key will move to edit the next editable field
Yes, you're right.
Just for convenience, it would be nice, when switching to each next cell, to select all the text in it in order to write something new, and not erase the old one and then write down the new one, that's what I'm talking about. And in the previous cell, just the new text remains and that's it, without any selections
P.S. Sorry for lengthy responses, I can write every 10 minutes :)
-
@Vlad02
If I understand what you want (still not sure). https://stackoverflow.com/questions/2804115/how-to-connect-focus-event-from-qlineedit shows to subclassQLineEdit
in order to override itsfocusIn
/OutEvent
s and emit a signal for them. Which you could attach a slot to to do the samedeselect
/selectAll()
principle as in the timer. Is that what you are looking to do?