PDA

View Full Version : I am new in Qt !!!



tom1972
9th June 2012, 13:06
Hello to everyone !!!
I created a project with qt creator with the name "asd", with
Base class: QDialog
Class name: asdDialog
Header file: asdDialog.h
Source file: asdDialog.cpp
Form file: asdDialog.ui

In the form I added
1. QPushButton with text "HELLO"
2.QLineEdit

I tried to connect them by-code like this way:
I put the connect function in the "asdDialog.cpp", in the constractor "asdDialog::asdDialog(QWidget *parent)" , under "ui->setupUi(this);"

connect(ui->button, SIGNAL(clicked()),
ui->lineEdit, SLOT(writeMessage("HELLO")));

As you see I want to create a new slot for "ui->lineEdit" which does not exist ...

Where can I declare and write the code of "writeMessage("HELLO")"

Is it possible without using the "right click --> Go to slot" ??
If you advice me with "right click --> Go to slot", then when can I use the "connect method" ??

Anyone can help me ?? Thanks in advance !!! (I am from Greece - Volos)

wysota
9th June 2012, 13:11
Hey, good football match yesterday :)

You can't add slots to already existing classes thus it is not possible to add a slot to "ui->lineEdit". You need to add that slot to your asdDialog class and modify your connect() statement accordingly. Moreover you can't pass arguments to the slot you want to call from within the connect() statement. You have to create a generic slot that will call ui->lineEdit->setText("HELLO").

tom1972
9th June 2012, 14:29
My friend thank you for the reply !! It was a good match indeed !

I tried to put my slot in asdDialog.h class like this way:
public slots:
void writeMessage();

And I wrote the code in asdDialog.cpp like this:
void asdDialog::writeMessage()
{
ui->lineEdit->setText("HELLO");
}

And the connect:
connect(ui->button, SIGNAL(clicked()),
ui->lineEdit, SLOT(writeMessage()));
It said:

Object::connect: No such slot QLineEdit::writeMessage()
Object::connect: (sender name: 'button')
Object::connect: (receiver name: 'lineEdit')

Have I done the "You need to add that slot to your asdDialog class and modify your connect() statement accordingly", correct?

Why slots cannot have args , in that you said "you can't pass arguments to the slot ..."
There are slot with args , aren't there?

Sorry friend but how can I create a generic slot? What is it ??

Friend sorry about the way I speak, I don't speak very good english (first time in a foreign forum)

Ali Reza
10th June 2012, 07:44
your football team is professional indeed.
i enjoyed watching it.
good luck

tom1972
10th June 2012, 11:19
@ Ali Reza

Thank you very much my friend (although I don't watch fanatically football....)

@ wysota


Good friend I FOUND IT !!!
I created a slot to the initial asdDialog form ....

e.g. in asdDialog.h

class asdDialog : public asdQDialog
{
...............
public slots:
void writeMessage();
...............
};

And the connect, in asdDialog.cpp

asdDialog::asdDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
..............
connect(ui->pushButton, SIGNAL(clicked()),
this, SLOT(writeMessage()));
}

void Dialog::writeMessage()
{
ui->lineEdit->setText("LINUX");
}

Watch my friend "this" refers to initial form, as you advised me ....

Thank you very much for your advises and for your help !!! tomkat