how to link a textbox made in QT designer with a coded function using signal slots - qt

I have made the ui using QT designer. Which has a textbox in main window in which user enters his name. i want to call function 'input' when the data entered by user is over.
but i cant find a way to link a textbox from ui designed using QT designer to a coded function form other class.
I guess signal and slots (in qt designer) can only link to components of the same window.
Again,
at the same time i want to store the name entered by the user in a char variable usr_nm[] in the same function input. I later want to display the name in a text browser with some other details in function output.(-->Input and output function are in same class.)

Do you have to define this in Qt Designer?
It seems to me it would be better if you simply used an auto-connection slot, for example:
on_textBoxName_textChanged() // I'm assuming you are using QTextEdit
{
ui->textBoxName->toPlainText(); // this returns text in QTextEdit as QString.
}
This uses auto-connection - see here.

Related

How did I retrieve changes in the content of all text edit boxs in a widget

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.

How can i change QStackWidget page index with button in Qt Designer?

I am currently working in Qt Designer and i am trying to make button change the index of QStackWidget with click signal.
I entered signal editor mode and connected Button to QStackWidget, This is what i got:
As you see in the picture, setCurrentIndex(int) is grayed out, If i choose any signal from QPushButton, there is nothing associated with changing page.
Sorted Question:
How can i change the page in QStackedWidget using a button? (In Qt Designer).
That is not possible directly with Qt Designer, because QButton click signal doesn't send any index or argument, and setCurrentIndex(int) need an argument to chose change the index.
You have to do it using signal/slot in C++ code, or use an other widget like QSpinBox which emit a signal with an integer argument.

Calling a form from a pushbutton using only QtDesigner

I have created a form. I have many 2 push buttons. On clicking on a pushbutton I want to call another custom form. I am using only QtDesigner. I am NOT using QtCreator. Using QtCreator, there are so many examples on how I can do it. But using only QtDesigner 4 there are no examples. I have also tried creating a MainWindow and then having pushbuttons in that. I want to call a new pop up window when I click on a button (which is a custom form). I am using Eclipse CDT as the IDE. I have installed Qt plugin so that I can do both C++ and Qt development. The problem is I cannot use 'Form' to declare my custom form in header file of the mainwindow.
I read in few posts that this is not possible to do using only QtDesigner and also read it can be done using QObject::connect. Please can anyone help me to confirm if we can do it and if yes please can you provide me an example?
Yes, it's definitely possible with C++. You'll need to connect() pushbutton's clicked() signal with a slot in your first form:
connect(pushButton, SIGNAL(clicked()), this, SLOT(show2ndForm()));
The good place to connect is in your first form constructor.
In that slot just show your second form (for example, using QDialog::exec()):
void FirstForm::show2ndForm()
{
static SecondForm *form = 0;
if(!form)
form = new SecondForm(this);
form->exec();
}
You'll probably need to inherit your second form from QDialog to use this method and also create header and source files for your second form.
For modeless form instead of modal, use form->show() instead of exec().
This is not complete possible if you need to customize a slot, but for simple operation where the existing slots are available, you can just use the signal-slot edit as below.
You can select the receiver object and then the correponding slot. I am providing further screenshot to show it is done for customized slots as well.
Right click with your mouse in the middle of the main window and the change signals and slots
Select the change signals and slot option
Use the Add button to add a new slot
Click on the OK button to conirm it once you chose the desired name
In the signal-slot editor double click on the desired object's slot, and it will show the available slots including your new custom slot.
Select your custom slot and you are done from the designer parts. Do not forget to actually implement that in your C++ code.
If you do not need a custom slot, and a built-in will suffice, you can select that off-hand without the previous steps. Those are provided for completeness.

Qt Editable QGraphicsTextItem validating text and emitting signal on change

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.

Qt: How to connect class to custom Qt Designer Widget

Maybe I'm thinking about this completely wrong...
I've created a new widget in Qt Creator with a Designer file (I picked the Widget template, which generated a source and header file for my custom widget class, and also a designer file).
I then designed the widget with the Designer. I can now create instances of this widget and it will show up in my app.
But it's not terribly useful because I don't know how to customize the widget at runtime.
Let's say all I've got in the widget is a Label and a Button. At runtime, how can I change the text of this label? I can't figure out how to connect the designer stuff to my actual class, and I can't find any documentation on how to do this. Am I missing something?
Thanks!
A few things:
In designer, each of your widgets (the QPushButton, and the QLabel in your case) has a name assigned to it. This name is the name of the variable that you can use in C++ to reference that widget and call functions on it.
Depending on how your custom widget was implemented, you will be able to reference these variables using one of two methods:
If your class inherits from Ui::MyCustomwidget, then your variables are simply member variables of your class and can be accessed at any time (myLabel->setText())
If you have a member variable (generally named ui, of type Ui::MyCustomWidget), then you can access your widgets using the ui object (ui->myLabel->setText())

Resources