Passing Variable between 2 Forms QT - c++

i'm a beginner so if it possible explain to me how to pass variable between 2 forms for example:
i have the first Form called : Send.ui - send.cpp - send.h
and the second form called : Receive.ui - receive.cpp - receive.h
we gonna suppose that i have a variable called age=25 in Send.cpp and a push button
i want that when i press the push button he will open the second form Receive.ui and i will have a variable in this second form age=25
that's meen if i change the variable in the Send.cpp he will be automatically changed in Receive.cpp
and thanks in advence

For communication between objects, Qt has a mechanism called Signals and Slots.
Here is a example of how you can use signals and slots:
In this example I will use spinboxes to visually represent the variable(age) that you mentioned.
1. Open Receive form
In Send, create the button that will open the receive form.
send.h
public:
QPushButton *pushButton;
send.cpp
pushButton = ui->pushButtonSend;
Now create a slot in mainwindow to show receive form.
mainwindow.cpp
void MainWindow::showReceiveForm()
{
receiveForm->show();
}
mainwindow.h
private slots:
void showReceiveForm();
Now connect clicked signal from the push button to the slot in mainwindow.cpp. This will call the slot function everytime the button is clicked.
connect(sendForm->pushButton,SIGNAL(clicked()),this,SLOT(showReceiveForm()));
2. Send Age value
To send the age value to receive form, whenever it's value is changed in send form, connect the valueChanged signal from the QSpinBox in send form to a slot in receive form.
send.h:
public:
QSpinBox *spinBox;
send.cpp
spinBox = ui->spinBoxSend;
Slot in receive.cpp:
void Receive::receiveAge(int age)
{
ui->spinBoxReceive->setValue(age);
}
Now connect signal and slot in mainwindow.cpp.
connect(sendForm->spinBox,SIGNAL(valueChanged(int)),receiveForm,SLOT(receiveAge(int)));
In this example, I used predefined signals from QPushButton and QSpinBox, but you can create your own signals and emit them whenever.
Define custom signals in the header files:
signals:
void exampleSignal(int someArgument);
And emit them with the "emit" keyword.
emit exampleSignal(somenumber);

Related

How do I connect a QTableWidget and a QPushButton

I have a QTableWidget where I gathers informations of each item, the information is used in a function called
void DrawGraph (QTableWidgetItem *)
My problem is that I want to create a QPushButton that whenever I clicked it calls the function : void DrawGraph (QTableWidgetItem ), but I can't use it because the Signal and the Slot of connect dont have the same entry, I am waiting your help.

Qt Pass Values between 2 Froms

I haveing 2 FORMS in my Qt Project, the SplashForm and the MainForm!
The SplashForm start's first and when the user presses the button 'Connect' then the MainForm opened!
In SplashForm, i have some comboBoxes with values which user can choose,
my problem is that I want to pass those values from SlashForm comboBoxes that users have chosen to MainForm class and save them in private members of class, and then show them in MainForm in labels.
So far I can't find anything useful about how to pass values from forms and classes with Qt.
I have tried the Signal/Slot example but I didn't work.
Any Suggestions?
In event handler for 'Connect' button, open MainForm after assigning combobox value to SplashForm.
With
connect(m_button, SIGNAL (released()),this, SLOT (handleButton()));,
void SplashForm::handleButton()
{
MainForm* popup = new MainForm(this);
popup->setLabel(m_comboBox->currentText());
popup->show();
}

Qt custom QPushButton clicked signal

I want to send two integers, string and fret, to a SLOT that will process the location of the button that was pressed. The SIGNAL and SLOT argument have to match so I am thinking I need to reimplement the QPushButton::clicked event method. Problem is I am new to Qt and could use some direction.
connect(&fretBoardButton[string][fret], SIGNAL(clicked()), this, SLOT (testSlot()));
If you use the C++11 connection syntax you can use a lambda with calls testSlot with your string and fret arguments:
connect(&fretBoard[string][fret], &QPushButton::clicked, [this, string, fret]() {
testSlot(string, fret);
});
This code creates a lambda using the [captures, ...](arguments, ...) { code } syntax. When you make the connection it captures the string and fret variable values, and will then pass them on to testSlot when the button is clicked.
There are Two approaches you could use to add the string and fret information. one is to use the sender() function to get the button which emitted the signal. you can the access fret and string if they are members of your button class so in the SLOT you would have.
MyPushButton *button = (MyPushButton *)sender();
button.getFret();
button.getString();
However since you are already subClassing QPushButton you could use a private SLOT to catch the buttonClicked signal and re-emit a signal with the right values.
In the constructor
connect(this, SIGNAL(clicked()), this, SLOT(reemitClicked()));
and then the reemit SLOT
void MyPushButton::reemitClicked()
{
emit clicked(m_fret, m_string);
}
be sure to add the appropriate private slot and public signal to you class
https://doc.qt.io/archives/qq/qq10-signalmapper.html see this artical for a good discussion on various ways to add an argument to signal.

How to know which QLineEdit emitted the editingFinished() inside the signal handler?

I want to implement a custom response to user input for several similar QLineEdit objects. I want to create a common handler of editingFinished() or textChanged() signal and assign it to all the QLineEdits. However, the response requires the knowledge of the sender of the signal - for example, it must highlight the entered text with different colors.
How do I know the sender of the signal inside it's handler?
You can get pointer to sender with call to QObject::sender() and then cast this pointer to QLineEdit. Something like
void MyClass::onTextChanged(const QString& text)
{
QLineEdit* edit = qobject_cast<QLineEdit*>(sender());
if (edit)
{
// Do something with QLineEdit
}
else
{
// Just to make sure that you have not make mistake with connecting signals
}
}
May be you should consider using of QSignalMapper technique: http://doc.qt.io/qt-4.8/qsignalmapper.html

Signals and Slots with Qtoolbutton

I have created a ToolButton with my qt designer and Im trying to connect it to a slot.
I wrote this
connect(ui->toolButton_addfiles, SIGNAL(triggered()), this, SLOT(changeDirectory()));
Im able to run the program but when I press the button I see the following log into my qt Application Output :
Object::connect: No such signal QToolButton::triggered() in ../RightDoneIt/rightdoneit.cpp:10
Object::connect: (sender name: 'toolButton_addfiles')
Object::connect: (receiver name: 'RightDoneIt')
If I change the toolButton_addfile to some action like (actionChange_addfile) it will work fine.
How can I make this connection work ?
As the error says, there's no signal triggered() but triggered(QAction*) in the QToolButton.
Edit
In the connect function you must have the signal signature like triggered(QAction*) since QToolButton class has no signal triggered() (with no parameter) declared
You could use the auto-connection process of Qt.
In the class referencing your UI, create a slot called :
on_toolButton_addfiles_clicked();
Exemple :
See : A Dialog With Auto-Connect
class ImageDialog : public QDialog, private Ui::ImageDialog
{
Q_OBJECT
public:
ImageDialog(QWidget *parent = 0);
private slots:
void on_okButton_clicked();
};
Hope this helps !
Edit : No triggered signals in qAbstractButton. See http://doc.qt.nokia.com/4.7/qabstractbutton.html
QToolButton() has the signal method triggered(QAction *), this signal is to be addressed if triggering of the related QAction connected to the QToolbutton is of interest. At the same time QToolButton is inherited from QAbstractButton(), which has the toggled(bool checked) signal. If you want to catch the signal triggered by pressing/unpressing the tool button, you may do as following:
auto toolbutton = new QToolButton(this);
connect(toolbutton , &QAbstractButton::toggled, this, []() { // your code });
Alternatively (I have not checked if this solution works), you may define explicitly, what signal to be caught
connect(toolbutton , qOverload<bool>(&QToolButton::toggled), this, [](bool val) { // your code});
I'm guessing you're creating a QAction, adding it to the QToolButton and trying to connect it to a slot in your own class?
You can connect your slot to either the QToolButton::triggered(QAction*) signal or directly to the QAction::triggered() signal. Either way, the QAction must be added to the QToolButton through QWidget::addAction(QAction*), the slot's method signature must match the signal's signature, and the connect invocation must include the signal/slot parameters, not just the names of the signal and slot.