Qt Signals and Slots between Forms - qt

How do I connect a Signal from a form to a Slot in another form? In other words I have a value that I am manipulating in a dialog.ui file that I want to emit to a slot that changes the value in the main application.

go to controller of the main GUI and connect both of them , like :
connect(form1,SIGNAL(increment()),form2,SLOT(manipulate()));
don't forget to use emit in the first form.

Related

Assign new slot clicked() for button

I have copied several buttons in design form. As result all buttons got the same slot for clicked(). How to create and assign new personal slots for buttons?
UPD
Trying to use Signals & Slots editor. It was empty in the beginning, and I added new one:
But if I right click on button btnInfoCB->go to slot.. clicked() , it will point me to the same old method.
Qt creator will create slots for you. From designer, Right click on any push button and select 'Go to slots' option. select Clicked() signal press OK. Qt creator will navigate you to the created slot. Qt automatically connects your button clicked signal to the created slots.
You can do the same for all push buttons you created
Qt has an auto-connect feature that connects the slots named like this: on_UIELEMENTNAME_SIGNALNAME(SIGNAL_PARAMS) with the corresponding signal of the ui element, see the documentation here
So to fix this you have 2 options:
Option 1: rename the slots to have all the names corresponding with the names from .ui file:
private slots:
void on_MyFirstBtn_clicked();
void on_MySecondBtn_clicked();
//and so on
Or use option 2: manually connect
private slots:
//notice the prefix "on_" and the suffix "_clicked()" not part of the function name
void MyFirstBtnClicked();
void MySecondBtnClicked();
And in the constructor of the class (i named it MyWidget) add the connect calls:
connect(ui->MyFirstBtn, &QPushButton::clicked, this, &MyWidget::MyFirstBtnClicked);
connect(ui->MySecondBtn, &QPushButton::clicked, this, &MyWidget::MySecondBtnClicked);
LE: I don't recommend using the UI Designer for connecting slots that are defined in code because of maintainability reasons. That feature in Designer is very good if you need to connect already defined signals and slots, like check/unched to show/hide and other similar predefined functionality, but for your defined slots i don't like it.
New slot for an existing QPushButton /w auto connection
You have to change the objectName of QPushButton first. Then right click and create a new slot connection e. g. clicked(). Qt Creator will then create a new slot with the new objectName e. g. void on_newObjectName_clicked() { }. The old slot is no longer used then and you will get a new fresh function.
Manually connect /w signals and slots editor
If you like to connect it through your editor, you first have to click at the QWidget (e. g. MainWindow) and select change signals/slots from the menu. There you have to add a new function e. g. myNewSlot(), which is then available in the drop-down in the signals and slots editor as shown in your screenshot. If you use this type of connection you need to declare the slot in your .h in private slots and in the .cpp file. The object name may still link to an auto connection, so you probably need to change the objectName as well or remove the auto connected slot.

Signal to Slot in QT Creator, where is connect() function?

In QT Creator, design mode, I right-click on a widget and select "Go to slot" and it creates a slot function for one of the widget's signals.
I would have thought that this would have generated a connect() function to create this connection, however, I can't find anything like that in any of the source code.
Where is the actual code that connects the widget's signal to the slot function?
thanks
If you're using QtCreator's Designer, one of the outputs from this is a .ui file
Qt Designer ui files are an XML representation of your form's widget tree, and are processed by uic, the "User Interface Compiler"
One of the features provided by Qt's ui format is AutoConnect.
uic automatically generates code in the form's setupUi() function to connect your signals and slots.
The way it works is as follows:
Your slots must conform to the following format:
void on_<object-name>_<signal-name>(<signal-parameters>);
where object-name is the name of the object which emits the signal this slot is for.
Later, uic then generates code which calls QMetaObject::connectSlotsByName(this);
Using Qt's reflection system, the QObject which has objectName()=object-name is found, and it's signal is connected to your slot.

Function connected to the button is called twice after one click

I have a problem with slots and signals. I created buttons and connected them to the clicked() slot. Then i decided to connect signals and slots manually and since then when I click the button it calls its function twice.
connect(ui->okButton, SIGNAL(clicked()), this, SLOT(on_okButton_clicked()));
void settingswindow::on_okButton_clicked()
{
qDebug() << "ok clicked";
this->close();
}
I was looking for the answer on google, but all i found was this: Where is the generated code of qt signals slots editor but my *.ui file looks like this: pastebin to the code. As you can see there's only one line with and nothing more. I can't find where the information about signals and slots is saved. Rebuild and clean options won't help.
This is not a bug in Qt. If you look at the generated code for your ui_*.h file, you'll notice that the last statement executed in the setupUi() function is a call to QMetaObject::connectSlotsByName().
Since your slot already conforms to the naming convention that this function is looking for, your slot is automatically connected to the signal.
By manually connecting the signal to the slot, in your settingswindow class, you effectively duplicate the connection.
As #Devopia mentioned, this is a documented feature.

Parameter value transfer in Qt connect

I'm using the next line in order to display the position of the slider on the label.
connect(slider, SIGNAL(valueChanged(int)), label, SLOT(setNum(int)));
It works fine.
However, I don't really understand how is this value being transferred.
How does the parameter of the valueChanged function pass to the setNum function ?
When you call connect you specify a signature of a signal and of a slot (or another signal).
Qt uses these signatures to store an "internal link" between 2 methods.
When a signal method is called (for example, with emit valueChanged(5)) Qt looks for a corresponding slot method in the list of "links".
Then Qt calls a slot method passing arguments from the first signal method.
Read this article thoroughly. It's really awesome.
Every time the signal is triggered, it gives the int to the slot function. It's like if you (the slot) go and see you neighbor to give him eggs and then he do whatever he wants with it.
You can see this excellent doc about how Qt Signals and Slots works

QT signals & slots

How to use signal and slots for handling events i.e when a pushButton is clicked some number should be displayed in QLineEdit.Just like in caluculator application when a number is clicked the corresponding is displayed .
connect a widget's signal to other's widget slot.
Signal = Emitted when the button (for instance) is pressed.
Slot = called when a certain singal is fired, and this signal is connected to this slot.
If You use QtCreator here is the procedure:
right click on the pushbutton. Select go to slot.
Select clicked()
QtCreator will open C++ editor with new empty member function defined that implements
your slot. This function will be called whenever someone clicks this button.
Now in this function you can call method of another widget ot modify its contants.
This book is quite good for starters:
http://www.amazon.com/Book-Qt-Art-Building-Applications/dp/1593271476/ref=sr_1_8?ie=UTF8&qid=1300905855&sr=8-8
Good luck.

Resources