how to set a text in line edit Qt Creator? - c++

I am working in c++ Qt Creator. I have a form with labels and lineEdits. I would like to set as default in each lineEdit a text. It would be more efficient than writing the same stuff each time I run the application. Can you please tell me how to do this?

Qt has a concept of properties, and for each property, there's usually a getter and a setter, in your case "Text" (as also displayed in the designer) -> void setText(QString), QString text().
As a serious advice: Learn to use the excellent documentation. Nearly everything in QtCreator lets you open a context-sensitive help via F1. And read some introductions;

Use
void setText( const QString & )
You can set it in the constructor or maybe set all those defaults in an init() function.

Read thorugh the documentation.
Why not just set it to your default value at start? Would be the easiest way if you know how to set a textedit to a value anyhow.
hope this helps, tell me if you need aything more

When you double click in the UI designer on the QTextEdit you can enter a default text which is set every time your application is run.
Alternate you can set the text using setText(QString) function in the constructor of your window.

Related

whats differences between setInformativeText &setText functionaly?

In the QMessageBox class in QT there are setText & setInformativeText functions,whats function of second one while it do any thing that first do?
Qt documentation says about informativeText:
On the Mac, this text appears in small system font below the text().
On other platforms, it is simply appended to the existing text.
So if you don't use your application on Mac then you can just use setText with whole text you want to display to user.

How do you programatically update the UI in Qt?

I'm fairly new to Qt, and am trying to wrap my head around signals and slots. Though I've figured out how to create custom slots, I've yet to figure out how do update the GUI from my C++ code. I realized that the entire UI, which I created in the designer, is only written in what appears to be XML based UI code. Do I have to handwrite my own Qt C++ UI in order to update the interface, or can I somehow use C++ to update the XML based UI? I'm just looking to add a widget to the main form on a button click. Any help is appreciated. Thanks!
To add a widget to a form you can simply do
ui->layout()->addWidget(new Widget);
XML is used by QtDesigner as a mean to create, update, and persist your GUI, enabling a visual approach to development, but in the end you can build your application entirely without it.
you dont havfe to update the xml of UI . you can inherit the ui into your own class using setupUi
http://crpppc19.epfl.ch/doc/qt4-doc-html/html/qwidget.html#setupUi
now in your C++ class you can update the widgets like changing label text, lineEdit text, setting up spinbox value using Signals & slot.
That xml is used by Qt designer and outside of designer it's only used by uic as a source to generate C++ code, so everything that you do in the designer end-up as C++ code, so it can be updated (or done completely) in C++ code (you can look into the code and see that you most likely have a member called ui that is most likely a pointer, but it can also be an instance of a C++ class that is generated for you by uic, and trough that ui member you can access the widgets and layouts that you created in the designer).
A generic resource that i recommend you to read can be found here and then (if you still can't figure it out) ask a specific question about what exactly are you trying to achieve.
LE: that link contains 3 methods to use the generated code into C++ code, don't try all of them into your working project, you may choose one method or use the default one (ui member pointer)

How to use .hide on a Qt LineEdit, still take input?

I have a QLineEdit which I would like to hide from the user but still take in input form somewhere. I am creating a typing tutor and I want to take input in a hidden manner in order to provide a more dynamic form of feedback.
Any other suggestions as to best accomplish would be greatly appreciated
You can not do it. When QLineEdit is hidden, there is no focus on it, and you can not grab events.
If you persist on using QLineEdit there's an option to turn off displaying text.QLineEdit::NoEcho.
lineEdit->setEchoMode(QLineEdit::NoEcho);
This will show the edit box, but it doesn't show any text.
Otherwise, you should write a slot to grab window keyPressed signals, and handle everything yourself.
For other people who try to do such a thing, a workaround is simply to implement a QLineEdit visible, but with a MinimumSize = MaximumSize = 0x0 :)

retranslateUi() clears QLabel text

My qt4-based application dynamically changes the GUI language by calling retranslateUi() function. It works like a charm, but when it processes the QLabel which text changes by the program, it tries to translate original text - the one, specified in Qt Designer, not the one, set by my program.
I understand it happens because retranslateUi() is auto-generated during build process so it couldn't possibly know about my changes, but I'd prefer to skip such QLabels completely. Is there a way to tell Qt to skip certain widgets in retranslateUi()? Alternatively, maybe I can disable content change for QLabel in my program?
I know I can sub-class QLabel and by overriding setText() solve this problem, but I prefer to use standard QLabel to be able to manipulate it using Qt Designer.
As I remember, in Designer you can uncheck on QLabel should it be translated. By default it is. Choose label you don't want to be translated, in property editor scroll to "text" property, expand it and uncheck translate checkbox. Then you should generate ui_*.h file again. After that your label shouldn't be in retranslateUi code

How to change QPushButton icon using stylesheets in Qt app

Is it possible to set and change the icon on a QPushButton using stylesheets?
I need this for a Windows-based white-label Qt4.5 application that customers stylize using stylesheets.
Thanks.
A little late for a reply, but in case anyone else pops on this via google;
You can add the icon by changing the QAbstractButton-property. Simply use:
QAbstractButton {
qproperty-icon: theme_url("/button_action/add");
}
The type-selector (QAbstractButton), enables you to set the style for the selected object and all its subclasses. Since QProcessButton extends the QAbstractButton, this will do the trick. (Of course you could also use the QPushButton type-selector here).
For more information on how to use selectors, I found this quite helpful.
If you want to learn more about the button-properties, refer to the QT-Documentation here.
PS. In case you want to change the icon, once the button is clicked, use
qproperty-icon: url(":/Icons/before_click.png") off,
url(":/Icons/after_click.png") on ;
Yup.
border-image: url(/url/to/image);
You can use:
image: url(/url/to/image);
See http://doc.qt.io/qt-5/stylesheet-reference.html#image-prop and http://doc.qt.io/qt-5/stylesheet-customizing.html#box-model.