Unsolved My first own Qt app
-
Hi all,
Please look at these files. These are the files of an app that I want to develop and extent it gradually. Now is the first steps.
This is employee.h:
#ifndef EMPLOYEE_H #define EMPLOYEE_H #include <QDialog> class QLabel; class QPushButton; class QLineEdit; class Employee : public QDialog { Q_OBJECT public: Employee(QWidget* parent = 0); private: int mySalary; QLabel* label; QLineEdit* lineEdit; QPushButton* show_sl; QPushButton* quit; }; #endif // EMPLOYEE_H
And this is employee.cpp:
#include <QtWidgets> #include "employee.h" Employee::Employee(QWidget* parent) :QDialog(parent) { mySalary = 15; label = new QLabel(tr("mySalary")); lineEdit = new QLineEdit; show_sl = new QPushButton(tr("Show")); quit = new QPushButton(tr("Close")); QHBoxLayout* layout1 =new QHBoxLayout; layout1 -> addWidget(label); layout1 -> addWidget(lineEdit); QHBoxLayout* layout2 =new QHBoxLayout; layout2 -> addWidget(show_sl); layout2 -> addWidget(quit); QVBoxLayout* vlayout = new QVBoxLayout; vlayout -> addLayout(layout1); vlayout -> addLayout(layout2); setLayout(vlayout); }
And this is main.cpp:
#include <QApplication> #include "employee.h" int main(int argc , char* argv[]) { QApplication app(argc, argv); Employee* employee = new Employee; employee -> show(); return app.exec(); }
It runs successfully. Now my question is that, what widgets/functions/slots/signal do I need to add to the code so that when I click the Show button, it shows the value of my_Salary which is 15 in the lineEdit please?
As I said, I'm going to make the app bigger step-by-step. -
@tomy
you could add a method to set your values before you call show()Very basic example:
class Employee : public QDialog { Q_OBJECT public: Employee(QWidget* parent = 0); void setSalary(int value) { mySalary = value; lineEdit->setText( QString::number(value) ); } ... };
employee = new Employee; employee->setSalary(15);
-
@raven-worx
Thank you for your reply but I want to use the widgets in my code, and as I said, by clicking the button namedShow
, its value to be shown inlineEdit
.
Show
is in line show_sl = new QPushButton(tr("Show")); inemployee.cpp
.I later develop the code and add some function to be able to set the salary.
-
at the bottom of the constructor add
connect(show_sl,&QPushButton::clicked,[this]()->void{my_Salary->setText( QString::number(mySalary ));});
-
What are these??!!
I'm new in Qt and you write things like&QPushButton::clicked,[this]()->void
.... I don't understand these, sorry.I think I need a signal and a slot like this:
I added these to theemployee.h
signals: void show_salary(); private slots: void show_salary_in_lineEdit();
And a
connect
and the definition ofshow_salary_in_lineEdit()
to theemployee.cpp
this wayconnect(show_sl, SIGNAL(show_salary()), lineEdit, SLOT(show_salary_in_lineEdit())); . . . void Employee::show_salary_in_lineEdit() { lineEdit -> setText(QString::number(mySalary)); }
Now the code runs but clicking the Show button doesn't do anything!
-
Hi
When you use SIGNAL and SLOT macros, you can check if connect works with
qDebug() << "conn:" << connect ( xxx );
should return true; -
@tomy said in My first own Qt app:
What are these??!!
That's the Qt5 connection syntax with a lambda
I think I need a signal and a slot
You just need a slot
connect(show_sl, SIGNAL(show_salary())
show_sl has no signal show_salary hence @mrjj remark warning you that the connect won't work
this is correct
void Employee::show_salary_in_lineEdit() { lineEdit -> setText(QString::number(mySalary)); }
now the connect:
connect( show_sl //who sends the signal , &QPushButton::clicked // what signal do you want to react to (in this case when the button is clicked) ,this // who should catch the signal ,&Employee::show_salary_in_lineEdit //what slot should be called );
-
Hi,
Your connect code not is correc, what you need is connect de Button signal clicked with your Show function,Something like that:connect(show_sl, SIGNAL(clicked(bool)), this, SLOT(show_salary_in_lineEdit()));
If you allow me a question, would not it be easier for you to use the graphic editor instead of creating the graphic objects by code?
EDIT: I am sorry, I was responding when the @VRonin had already done so.
-
-
@mlago said in My first own Qt app:
Hi,
Your connect code not is correc, what you need is connect de Button signal clicked with your Show function,Something like that:connect(show_sl, SIGNAL(clicked(bool)), this, SLOT(show_salary_in_lineEdit()));
Thank you. I changed the connect the way you showed and the code ran as expected. Thanks again.
If you allow me a question, would not it be easier for you to use the graphic editor instead of creating the graphic objects by code?
I'm learning Qt using a book and it walks me this way. I will be taught the Designer mode shortly. :)
EDIT: I am sorry, I was responding when the @VRonin had already done so.
-
@mrjj said in My first own Qt app:
Hi
When you use SIGNAL and SLOT macros, you can check if connect works with
qDebug() << "conn:" << connect ( xxx );
should return true;Thanks. Although the code runs fine now, let me ask how to use that code for example in my code? (instructions)
-
@tomy
Hi
You can use qDebug() for many nice things :)1: #include <QDebug>
2: use it
qDebug() << "text" << variable << "more text" << some_other_variable ;
-
Thank you.