How to connect mainwindow and dialog using signal,slot
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
Re: How to connect mainwindow and dialog using signal,slot
Hello,
Please use code tags for better reading, viewing, and understanding of code.
check this link Code Tags
Quote:
Code:
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 :
Code:
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.
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().
Quote:
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.
Re: How to connect mainwindow and dialog using signal,slot
still showing error :
QObject::connect: Cannot connect QLineEdit::textChanged(QString) to (null)::setText(QString)
my dialog.h is
my dialog.cpp is :
Code:
{
ui->qwe->setText("qerr");
}
and my connect function :
Code:
connect(ui
->lineEdit,
SIGNAL(textChanged
(QString)), dialog,
SLOT(setText
(QString)));
Re: How to connect mainwindow and dialog using signal,slot
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.
Re: How to connect mainwindow and dialog using signal,slot
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();
Re: How to connect mainwindow and dialog using signal,slot
Quote:
Originally Posted by harvey_slash
Code:
{
ui->qwe->setText("qerr");
}
should be something like :
Code:
{
ui->qwe->setText(str);
}
try to avoid hard coding.
where are you doing this :
Quote:
Code:
dialog=new Dialog;
dialog->show();
show the function where this code executes , or show the complete code.
Re: How to connect mainwindow and dialog using signal,slot
I had tried the str method before, same error
here is the place where I am opening the dialog
Code:
void MainWindow
::keyPressEvent(QKeyEvent *event
) {
int i=event->key();
//char z=(char)i;
if(i>=48&&i<=57)
{
q+=s;
ui->lineEdit->setText(q);
}
if(q.length()>20)
{
dialog =new Dialog();
dialog->show();
}
}
Re: How to connect mainwindow and dialog using signal,slot
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.
Re: How to connect mainwindow and dialog using signal,slot
my connect is in the mainwindow constructor.
I put connect here now
Code:
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)
Re: How to connect mainwindow and dialog using signal,slot
Put a qDebug in the seText() and check if its being executed or not
and try this maybe its a logical error
Code:
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...
Re: How to connect mainwindow and dialog using signal,slot
it IS getting executed
OKAY
IT WORKS NOW.
I think there are plenty of logical errors.
Thanks a lot buddy!!
Re: How to connect mainwindow and dialog using signal,slot
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.