How do I connect a QTableWidget and a QPushButton - c++

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.

Related

How To Access Dynamically Created Buttons Click events Qt C++

I Created buttons dynamically for data from the database
QPushButton *btnComment = new QPushButton("Comment");
btnComment->setProperty("id",qry.value(0).toString());
Is the button that i created dynamically
I set a connect
connect(btnComment, &QPushButton::clicked, this, &Planner::commentButton);
and created a function on slots
public slots:
void commentButton();
How can I get the id value so I can execute a SQL query after the button click
I tested the function
void Planner::commentButton()
{
QMessageBox inpass;
inpass.setText("Comment");
inpass.exec();
return;
}
It works but after clicking OK the application closes
I get something like this in the console
QMainWindowLayout::addItem: Please use the public QMainWindow API instead
Any possible approach ?
Update
I was able to solve the lambda issue by declaring the passing variable as a global variable
connect(btnComment, &QPushButton::clicked, this, [this]{ commentButton(taskID); });
As mentioned in the comment you can connect to a lambda rather than directly to a non-static member.
Change the signature/definition of Planner::commentButton to...
void Planner::commentButton (QPushButton *button)
{
/*
* Use button accordingly.
*/
}
Then simply change your connect call to...
connect(btnComment, &QPushButton::clicked, this,
[this, btnComment]
{
commentButton(btnComment);
});
Now a pointer to the QPushButton that triggered the call will be passed to Planner::commentButton.

Add copied text to a lineEdit when a button is pushed

I just started reading into QT but I don't quite get the SIGNAL SLOT functions.
I have a form with 2 QLineEdit and I want to copy the text from the first QLineEdit to the second one when a button is clicked but I don't know how to set up the connect function properly.
I tried tying textChanged function to itself but the result is that the text will edit everytime I press a letter, since that's the signal.
newForm::newForm() {
widget.setupUi(this);
connect(widget.nameEdit, SIGNAL(textChanged(const QString&)),
this, SLOT(textChanged(const QString&)));
connect(widget.pushMe, SIGNAL(pressed()),
this, SLOT(handleButton()));
}
void newForm::handleButton(){
}
I think I have to do something inside the handleButton function but I don't understand how to read and copy the text from the first line since the text() doesn't work inside handleButton
To copy the text from the first QLineEdit called widget.nameEdit to a second one, widget.nameEdit2, when a button is clicked, you can do it with one SIGNAL/SLOT connection using the QLineEdit setText() in a lambda:
connect(widget.pushMe, &QPushButton::released,this, [=](){
widget.nameEdit2.setText(widget.nameEdit.text());
};
You don't need to use the first QLineEdit textChanged() signal, and the above connection copies the whole text available in first field to second filed all at once when you press the button. One the other hand, if you want second QLineEdit to get updated continuously as text changes in first field, then you could use the textchanged() signal of first field , to setText() of second :
connect(widget.nameEdit, &QLineEdit::textChanged,
widget.nameEdit2, &QLineEdit::setText);

Passing Variable between 2 Forms QT

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);

QT Signals & slots in diffrent class

My program has 2 classes. One of them is MainWindow and another is Calc.
In main window I use automatic generated function on_PushButton_clicked. This function should send two values: double & char to function in Calc.
first:
void MainWindow::on_OneButton_clicked(){
QObject::connect(ui->ZeroButton , SIGNAL(clicked()), this, SLOT(...)) );
ui->TextEdit->insertPlainText("1");
}
second :
void Calc::Add(double val, char oper){
//compute something
}
It's my first app with Qt and I do not know how can I connect them. I've searched similar question on this forum, but can't found.
Sorry if i'm wrong.
First of all, you have to well understand what signal/slot mecanism is, and what you are doing.
Signal/slot mecanism is a Qt concept to link a function (signal) to another function (slot). To "make a link" between a signal and a slot, you have to connect them using QObject::connect(...).
When you use automatic generated function on_PushButton_Clicked() with Qt designer, you, in fact, "make a link" between the signal clicked() emitted when the pushButton is clicked, with a slot on_PushButton_Clicked(). However, the connection between this signal and this slot doesn't appear in your code so it may be confusing and that's why I'm pointing it out.
When you write this:
void MainWindow::on_OneButton_clicked(){
QObject::connect(ui->ZeroButton , SIGNAL(clicked()), this, SLOT(...)) );
ui->TextEdit->insertPlainText("1");
}
You create a connection with zeroButton when clicked and a slot, each time you clic on your button. As a connection is valid till an object is destructed, if you clic again on your pushButton, you'll have a second connection between zeroButton when clicked and your slot.
A better way to create connection is to use connect(...) function when you create your object (mainWindow in your case).
To make it simple for your calculator, you can create 9 buttons for digits, 4 buttons for operators, and 1 button to compute everything.
In your mainwindow constructor, you could have something like:
connect(ui->pushButton1, SIGNAL(clicked()), this, SLOT(onPushButton1Clicked()));
.... // Every other signal for each button
connect(ui->pushButtonEqual, SIGNAL(clicked(), this, SLOT(onPushButtonEqualClicked());
And in your body
void MainWindow::onPushButton1Clicked()
{
// concatenate current value + 1
ui->textEdit->insertPlainText(ui->textEdit->toPlainText() + "1");
}
void MainWindow::onPushButtonEqualClicked()
{
// check textedit content (need a digit + operator + digit)
...
// compute result
...
// write result in TextEdit
...
}
I hope it will help a little bit ;)

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.