Results 1 to 12 of 12

Thread: How to connect mainwindow and dialog using signal,slot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default 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

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. connect(ui->lineEdit, SIGNAL(textEdited(QString)), dialog, SLOT(seText(s)));
    To copy to clipboard, switch view to plain text mode 
    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 :
    Qt Code:
    1. connect(ui->lineEdit, SIGNAL(textEdited(QString)), dialog, SLOT(seText(QString)));
    To copy to clipboard, switch view to plain text mode 
    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().


    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.
    Last edited by toufic.dbouk; 6th October 2013 at 18:55.

  3. The following user says thank you to toufic.dbouk for this useful post:

    harvey_slash (9th October 2013)

  4. #3
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default 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
    Qt Code:
    1. public slots :
    2. void seText(QString);
    To copy to clipboard, switch view to plain text mode 
    my dialog.cpp is :
    Qt Code:
    1. void Dialog::seText(QString )
    2. {
    3.  
    4. ui->qwe->setText("qerr");
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    and my connect function :
    Qt Code:
    1. connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(setText(QString)));
    To copy to clipboard, switch view to plain text mode 
    Last edited by harvey_slash; 6th October 2013 at 19:32.

  5. #4
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.

  6. The following user says thank you to toufic.dbouk for this useful post:

    harvey_slash (9th October 2013)

  7. #5
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default 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();

  8. #6
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to connect mainwindow and dialog using signal,slot

    Quote Originally Posted by harvey_slash
    Qt Code:
    1. void Dialog::seText(QString )
    2. {
    3.  
    4. ui->qwe->setText("qerr");
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    should be something like :
    Qt Code:
    1. void Dialog::seText(QString str )
    2. {
    3.  
    4. ui->qwe->setText(str);
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    try to avoid hard coding.

    where are you doing this :
    Qt Code:
    1. dialog=new Dialog;
    2. dialog->show();
    To copy to clipboard, switch view to plain text mode 
    show the function where this code executes , or show the complete code.

  9. The following user says thank you to toufic.dbouk for this useful post:

    harvey_slash (9th October 2013)

  10. #7
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default 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
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3.  
    4.  
    5. int i=event->key();
    6. //char z=(char)i;
    7.  
    8.  
    9.  
    10.  
    11.  
    12. if(i>=48&&i<=57)
    13.  
    14. {
    15. QString s= QString::number(i-'0');
    16.  
    17.  
    18. q+=s;
    19. ui->lineEdit->setText(q);
    20.  
    21. }
    22. if(q.length()>20)
    23. {
    24. dialog =new Dialog();
    25. dialog->show();
    26.  
    27. }
    28.  
    29.  
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

  11. #8
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.

  12. The following user says thank you to toufic.dbouk for this useful post:

    harvey_slash (9th October 2013)

  13. #9
    Join Date
    Sep 2013
    Posts
    44
    Thanks
    9
    Qt products
    Qt5
    Platforms
    MacOS X Windows Android

    Default Re: How to connect mainwindow and dialog using signal,slot

    my connect is in the mainwindow constructor.

    I put connect here now
    Qt Code:
    1. dialog =new Dialog();
    2. connect(ui->lineEdit, SIGNAL(textEdited(QString)), dialog, SLOT(seText(QString)));
    3. dialog->show();
    To copy to clipboard, switch view to plain text mode 

    its not showing any error, but the text is not getting displayed
    (I tried textChanged and textEdited ,both)
    Last edited by harvey_slash; 6th October 2013 at 20:07.

  14. #10
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. if(q.length()>20)
    2. {
    3. ui->lineEdit->setText("test");
    4. dialog =new Dialog();
    5. connect(ui->lineEdit, SIGNAL(textChanged(QString)), dialog, SLOT(seText(QString)));
    6. dialog->show();
    7. ui->lineEdit->setText("test1");
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    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...
    Last edited by toufic.dbouk; 6th October 2013 at 20:43.

  15. The following user says thank you to toufic.dbouk for this useful post:

    harvey_slash (9th October 2013)

Similar Threads

  1. Replies: 3
    Last Post: 23rd June 2013, 17:01
  2. signal slot to mainwindow
    By giugio in forum Newbie
    Replies: 3
    Last Post: 9th November 2012, 21:05
  3. Replies: 2
    Last Post: 15th September 2010, 01:54
  4. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 21:42
  5. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 20:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.