My first own Qt app
-
@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.
-
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; -
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!
@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.
-
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.
-
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.
-
Hi
When you use SIGNAL and SLOT macros, you can check if connect works with
qDebug() << "conn:" << connect ( xxx );
should return true;@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)
-
@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)