PDA

View Full Version : How to connect mainwindow and dialog using signal,slot



harvey_slash
6th October 2013, 18:31
I have done this so far:
in dialog.h
public slots :
void seText(QString q);

in dialog .cpp::
void Dialog::seText(QString q)
{

ui->qwe->setText(q);

}

in mainwindow.h ::
Dialog *dialog;

how do I connect two LineEdits (one is in mainwindow, and another is in dialog)
Dialog is my class name

I used this ::
connect(ui->lineEdit, SIGNAL(textEdited(QString)), dialog, SLOT(seText(s)));

its showing error::
QObject::connect: Cannot connect QLineEdit::textEdited(QString) to (null)::seText(s)

please be precise and explanatory as I am new to this concept.
thanks

toufic.dbouk
6th October 2013, 18:44
Hello,
Please use code tags for better reading, viewing, and understanding of code.
check this link Code Tags (http://www.qtcentre.org/misc.php?do=bbcode)


connect(ui->lineEdit, SIGNAL(textEdited(QString)), dialog, SLOT(seText(s)));
The signature of a signal must match the signature of the receiving slot. You cant pass a value in the connect() statement , which is in your case SLOT(seText(s))
it should be SLOT(seText(QString)). You should always pass the signature , the type of the parameter that is passed by the signal or the slot not the string its self.
should be :
connect(ui->lineEdit, SIGNAL(textEdited(QString)), dialog, SLOT(seText(QString)));
Signals and slots can take any number of arguments of any type. They are completely type safe.
you should check this link Signals and Slots. (http://qt-project.org/doc/qt-5.0/qtcore/signalsandslots.html)
PS. note the difference between textEdited and textChanged
textEdited()This signal is emitted whenever the text is edited. The text argument is the new text.
Unlike textChanged(), this signal is not emitted when the text is changed programmatically, for example, by calling setText().


in dialog.h
public slots :
void seText(QString q);
even in the header file the slot should be seText(QString). // just a declaration
you define the function in you .cpp file
Good Luck.

harvey_slash
6th October 2013, 19:27
still showing error :
QObject::connect: Cannot connect QLineEdit::textChanged(QString) to (null)::setText(QString)

my dialog.h is

public slots :
void seText(QString);
my dialog.cpp is :

void Dialog::seText(QString )
{

ui->qwe->setText("qerr");

}

and my connect function :

connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(setText(QString)));

toufic.dbouk
6th October 2013, 19:32
can you show your complete code please ?
where are you creating the instance of QDialog ? seems like the instance doesnt exits when the connect() statement executes.

harvey_slash
6th October 2013, 19:36
I have made a qt form class called Dailog.
I have a pointer of Dialog type called dialog in my mainwindow.h
I am calling that from some function in mainwindow.
dialog=new Dialog;
dialog->show();

toufic.dbouk
6th October 2013, 19:38
void Dialog::seText(QString )
{

ui->qwe->setText("qerr");

}

should be something like :

void Dialog::seText(QString str )
{

ui->qwe->setText(str);

}
try to avoid hard coding.

where are you doing this :


dialog=new Dialog;
dialog->show();
show the function where this code executes , or show the complete code.

harvey_slash
6th October 2013, 19:40
I had tried the str method before, same error
here is the place where I am opening the dialog


void MainWindow::keyPressEvent(QKeyEvent *event)
{


int i=event->key();
//char z=(char)i;





if(i>=48&&i<=57)

{
QString s= QString::number(i-'0');


q+=s;
ui->lineEdit->setText(q);

}
if(q.length()>20)
{
dialog =new Dialog();
dialog->show();

}



}

toufic.dbouk
6th October 2013, 19:54
well where is your connect statement ? is it being executed before the dialog instance is created ?
add the connect statement under the line where you instantiate the dialog class ie under dialog= new Dialog(this) and see what happens

the note about the "str" was just to avoid hard coding didn't have anything with the error.

harvey_slash
6th October 2013, 19:58
my connect is in the mainwindow constructor.

I put connect here now


dialog =new Dialog();
connect(ui->lineEdit, SIGNAL(textEdited(QString)), dialog, SLOT(seText(QString)));
dialog->show();


its not showing any error, but the text is not getting displayed
(I tried textChanged and textEdited ,both)

toufic.dbouk
6th October 2013, 20:16
Put a qDebug in the seText() and check if its being executed or not
and try this maybe its a logical error

if(q.length()>20)
{
ui->lineEdit->setText("test");
dialog =new Dialog();
connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(seText(QString)));
dialog->show();
ui->lineEdit->setText("test1");

}

it would have been really helpful and time saving if you just posted the complete code instead of parts of it every now and then...

harvey_slash
6th October 2013, 20:27
it IS getting executed

OKAY
IT WORKS NOW.
I think there are plenty of logical errors.
Thanks a lot buddy!!

toufic.dbouk
6th October 2013, 20:40
It would have been much much easier if you just posted the complete code.
i guess you should have grasped the idea behind signals and slots by now.
And if you need any further help don't hesitate to ask.
Your welcome friend.
Good Luck.