Programmatically set editable/active QLineEdit (like when user double click on it)
-
wrote on 27 Sept 2020, 02:52 last edited by
Hi,
I have
QLineEdit
. I'm trying to do the following:
after user finishes editing (presses 'enter' or mouse click somwhere or something) I check if it is ok and if it is not ok then I want to select all text in lineEdit and show ToolTip.
The problem is that I cant programmatically set lineEdit editable/active (like when user double clicks on it).How can I do that? Simply calling
lineEdit->selectAll()
doesn't work. -
@Please_Help_me_D said in Programmatically set editable/active QLineEdit (like when user double click on it):
imply calling lineEdit->selectAll() doesn't work.
It does work.
Setting the focus can be done with QWidget::setFocus() -
Hi,
Why not use a validator so your users cannot enter invalid values ?
-
@Please_Help_me_D said in Programmatically set editable/active QLineEdit (like when user double click on it):
imply calling lineEdit->selectAll() doesn't work.
It does work.
Setting the focus can be done with QWidget::setFocus()wrote on 27 Sept 2020, 11:39 last edited by@Christian-Ehrlicher I completely forgot to notice that my
QLineEdit
is a delegate. So to edit something in it I need firstly double-click on it.
In this way setFocus and selectAll doesn't work as I expect.@SGaist I use validator but after user finishes editing I need to additionally check if the full file name is available and unique (filename is a string from lineEdit).
-
@Christian-Ehrlicher I completely forgot to notice that my
QLineEdit
is a delegate. So to edit something in it I need firstly double-click on it.
In this way setFocus and selectAll doesn't work as I expect.@SGaist I use validator but after user finishes editing I need to additionally check if the full file name is available and unique (filename is a string from lineEdit).
@Please_Help_me_D
Hi
The view can do it.
https://doc.qt.io/qt-5/qabstractitemview.html#edit -
@Please_Help_me_D
Hi
The view can do it.
https://doc.qt.io/qt-5/qabstractitemview.html#editwrote on 27 Sept 2020, 12:02 last edited by@mrjj thank you
is it possible to get view from my delegate inherited fromQStyledItemDelegate
? -
@mrjj thank you
is it possible to get view from my delegate inherited fromQStyledItemDelegate
?@Please_Help_me_D said in Programmatically set editable/active QLineEdit (like when user double click on it):
is it possible to get view from my delegate inherited from QStyledItemDelegate?
Why do you need it? Add a validator as @SGaist suggested and you're fine. Or overwrite QStyledItemDelegate::setModelData() and do your checks there.
-
@Please_Help_me_D said in Programmatically set editable/active QLineEdit (like when user double click on it):
is it possible to get view from my delegate inherited from QStyledItemDelegate?
Why do you need it? Add a validator as @SGaist suggested and you're fine. Or overwrite QStyledItemDelegate::setModelData() and do your checks there.
wrote on 27 Sept 2020, 13:12 last edited by@Christian-Ehrlicher I think validator can't help in this case.
What I'm doing:
I have aQLineEdit
where user writes name of a file. Well file name can't have some symbols like?<>/*
etc. So I setQRegularExpressionValidator
to check symbols.
Besides there are already files in the folder that have their names. After user finishes editing I must check whether new filename is unique or not. If not then I want thatQLineEdit
stays active and show tooltip likeFileName is not unique
.Do I need to add all the file names in the folder to the regular expression and set it to validator?
-
QLineEdit has some signals which are emitted when the content changes.
-
wrote on 27 Sept 2020, 16:59 last edited by
Can't understand how to perform the idea I described above.
The problem was thatQStyledItemDelegate::setModelData
is invoked beforeQLineEdit
editing finished. I checked that withQAbstractItemView::isPersistentEditorOpen(const QModelIndex &index) const
. Thus at the moment when I need to open lineEdit for editing it is already opened but soon will be closed :)
That means thatQAbstractItemView::openPersistentEditor(const QModelIndex &index)
won't help.And
QAbstractItemView
I get from parent of my model wich is available fromQStyledItemDelegate
Now I solved my problem in the following way:
beside Validator I also added completer with file-name list that are in the folder. So when user fills lineEditor he can see what file-names already in the folder. I hope that is enough for now -
You need your users to enter the path of an existing file ?
-
Can't understand how to perform the idea I described above.
The problem was thatQStyledItemDelegate::setModelData
is invoked beforeQLineEdit
editing finished. I checked that withQAbstractItemView::isPersistentEditorOpen(const QModelIndex &index) const
. Thus at the moment when I need to open lineEdit for editing it is already opened but soon will be closed :)
That means thatQAbstractItemView::openPersistentEditor(const QModelIndex &index)
won't help.And
QAbstractItemView
I get from parent of my model wich is available fromQStyledItemDelegate
Now I solved my problem in the following way:
beside Validator I also added completer with file-name list that are in the folder. So when user fills lineEditor he can see what file-names already in the folder. I hope that is enough for now@Please_Help_me_D said in Programmatically set editable/active QLineEdit (like when user double click on it):
Can't understand how to perform the idea I described above.
-
Can't understand how to perform the idea I described above.
The problem was thatQStyledItemDelegate::setModelData
is invoked beforeQLineEdit
editing finished. I checked that withQAbstractItemView::isPersistentEditorOpen(const QModelIndex &index) const
. Thus at the moment when I need to open lineEdit for editing it is already opened but soon will be closed :)
That means thatQAbstractItemView::openPersistentEditor(const QModelIndex &index)
won't help.And
QAbstractItemView
I get from parent of my model wich is available fromQStyledItemDelegate
Now I solved my problem in the following way:
beside Validator I also added completer with file-name list that are in the folder. So when user fills lineEditor he can see what file-names already in the folder. I hope that is enough for nowwrote on 27 Sept 2020, 17:39 last edited by@Please_Help_me_D
Using theQFileDialog
to get a filename from the user for either reading from a writing to (whichever of those you want) is an awful lot easier than displaying existing filenames or writing validators if it can suit your situation? -
wrote on 27 Sept 2020, 19:41 last edited by Please_Help_me_D
@SGaist here is the picture. I have hdf5 container somewhere on the disk (say
D:\MyProject\DATA\raw_le.h5
). This container contains many datasets that should have unique names. And from this point of view we can look at this like we have a folderraw_le.h5
and files in this folder that should have unique names. When I edit itemData in my proxy model I actually change the name of a file in theraw_le.h5
folder.
Ok with validator I can check if there is no restricted symbols in file-name but I also need to check if final itemData (item name/file name) is unique.
I wanted that when user finishes editing delegated lineEdit and the final name is not unique I wanted that the lineEdit stays active and a toolTip appears with messageFilename is not valid!
and user continues to correct the filename without reDOubleClick on item.
@Christian-Ehrlicher probably you are right. Probably I need to check uniqueness of a file when typing and not when editingFinished. -
@SGaist here is the picture. I have hdf5 container somewhere on the disk (say
D:\MyProject\DATA\raw_le.h5
). This container contains many datasets that should have unique names. And from this point of view we can look at this like we have a folderraw_le.h5
and files in this folder that should have unique names. When I edit itemData in my proxy model I actually change the name of a file in theraw_le.h5
folder.
Ok with validator I can check if there is no restricted symbols in file-name but I also need to check if final itemData (item name/file name) is unique.
I wanted that when user finishes editing delegated lineEdit and the final name is not unique I wanted that the lineEdit stays active and a toolTip appears with messageFilename is not valid!
and user continues to correct the filename without reDOubleClick on item.
@Christian-Ehrlicher probably you are right. Probably I need to check uniqueness of a file when typing and not when editingFinished.wrote on 28 Sept 2020, 08:22 last edited by JonB@Please_Help_me_D said in Programmatically set editable/active QLineEdit (like when user double click on it):
@Christian-Ehrlicher probably you are right. Probably I need to check uniqueness of a file when typing and not when editingFinished.
Yes, if you want to stay in editing mode, instead of exiting it and then having to restart, your validator should check for a unique file as the user types and reject the input if not. Then the
editingFinished()
signal will not be emitted on a duplicate filename.
1/15