I'm working with Qt5...and I'm tryng to set a default value in QLineEdit in case of I wrote nothing in the linetext area.
Is possible to do that?
You can do this for example by connecting
void QLineEdit::editingFinished () [signal]
and check in the slot actual text from QLineEdit and if necessary set yours.
If you want user to have empty field when they start editing
First use placeHolderText property of QLineEdit to set the default value to display.
Then connect editingFinished (or possibly some other, check them out) signal to your own slot, where you check if text is empty (and then use the placeHolderText value) or if user entered something.
If you want to leave default text for user to edit
Instead of using placeHolderText, simply set contents of the QLineEdit to desired default value, when you create it. Then in the slot for editingFinished, if user made field empty, restore the text to default.
Related
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.
I have QFileDialog with 11 items in the filter. By default QComboBox shows 10 and for the rest you need to scroll - i.e. you have to scroll for 1 item. I would like to change it. QComboBox has maxVisibleItems property, but how can I access QFileDialog's filter combobox? I can see it in QDialogPrivate::qFileDialogUi->fileTypeCombo, but I don't have access to it.
Thanks!
The following solution only works for non native file dialogs (i.e. you must set the QFileDialog::DontUseNativeDialog flag).
In that case, QObject::findChild can be used to find the combobox in the widget. The following example assumes the dialog has only one combobox. If thats not the case, you must find the correct one via QObject::findChilden, which returns a list of all children.
Example code could look like this:
auto dialog = new QFileDialog(parent);
dialog->setOptions(QFileDialog::DontUseNativeDialog);
auto cBox = dialog->findChild<QComboBox*>();
if(cBox)
cBox->setMaxVisibleItems(11);
else
qCritical() << "Unable to find any combobox child";
//setup and show
Important: If you can't find the child, it's possible you have to first show the dialog before you can modify the box. In that case, place the code after the show call, and it might work.
I am really stuck up with a task relating to Qt GraphicsView. Any help or suggestions will be highly appreciated.
In my QGraphicsView application, I have a few editable QGraphicsTextItems that I have added to the scene. I need the following functionality:
Setting validator for float so that the user does not by mistake enter a character or new line in that text item.
Emitting a signal once the text is changed by the user.
Can anyone please suggest how I can implement this in my application? I have tried real hard but I am not able to find anything suitable. If there is any alternative or workaround, I'll be grateful to know.
Thanks!
QGraphicsTextItem does not support this ability, as I'm sure you have discovered. So you have a few options:
Reimplement focusOutEvent(QFocusEvent* event) and/or keyReleaseEvent(QKeyEvent* event) to detect when you validator needs to run. A QValidator can be created as a member of your text class, and queried either when focus is lost and/or a key is pressed (the enter key to signify completion, or on every letter). Then just create a custom signal for you when deem the editing to have finished or changed.
Use a GraphicsProxyWidget to hold a 'real' QLineEdit for text entry, just set it up with a validator as you would if placing in a traditional GUI form. You will need to 'forward' the editingFinished() or textEdited(const QString& text) signal from the QLineEdit to your QGraphicsTextItem, so you don't have to provide external access to the widget.
You could also use the internal QTextDocument of the QGraphicsTextItem, this is what actually holds and formats the text (access it with document()). However it doesn't support having a QValidator installed, so you would have to create a signal-slot loop whereby when the text is changed (signalled by contentsChanged()) it's received by the QGraphicsTextItem, validated, then either updated/cleared if it fails validation (which will update the QTextDocument, and trigger this process again) or ignored if it passes.
Neither is difficult to implement; the first requires more code but will give you more control over the visual appearance of the text box.
I am instantiating an editable QLabel like so:
QLabel foo("some text");
foo.setTextInteractionFlags(Qt::TextEditorInteraction);
I can click the text and modify it, and the modified text must be in a buffer somewhere, but even after examining the data fields in Qt Creator I don't see where it is:
QString notmodified = foo.text(); // only returns the original text
is the modified text somewhere that I can access it?
EDIT: I think using something else is indeed an easier way, but I'm still interested in knowing the answer to my question.
EDIT: OK, it's been a week. "Answered".
I would say that even though you can set this flag on a QLabel (the Qt::TextInteractionFlag is used by other widgets than QLabel), it is not designed to be edited.
Why don't you use a QLineEdit ?
For editable text field you have a good choise, QLineEdit or QTextEdit. Use one of these widgets. QLabel is just for labeling.
I have a QTreeView with data that changes over time, with a dataChanged signal being emitted in the QAbstractItemModel every second. The items in the QTreeView can be edited as well, but when a editor is opened for a certain item the editor string is updated while I edit is, which is very annoying. Any way to prevent an editor to be updated with the new values?
Do you use your own model with QTreeView? In this case you overwrite it and not return any data for Qt::EditRole. If it is not convenient for you (you want to have the current data in the field when you begin to edit it), then you could create your own QItemDelegate/QStyledItemDelegate and implement some custom logic there: make it so the widget is not updated with new value, when it has focus, for example.
Althought I might be missing something, and there is an easier way to do this.