August 14, 2011

kalster kalster
Lab Rat
315 posts

[SOLVED] help with signal and slot

 

The following code runs good without any errors but the signal and slot is not working correctly. In the code, the dialog calls the click signal and in the Dialog the function returns a variable. at the mainwindow, the slot should be then called but it is not. Below is a basic example about what i am trying to do. if the slot works correctly it then sets the text to the pushbutton on the mainwindow. I have verified that the function clicked at the mainwindow is never called. Your help is greatly appreciated. thank you in advanced.

dialog.cpp

  1. #include "dialog.h"
  2. #include "ui_dialog.h"
  3. #include "mainwindow.h"
  4.  
  5. QString pass1 = "pass";
  6. Dialog::Dialog(QWidget *parent) :
  7.     QDialog(parent),
  8.     ui(new Ui::Dialog)
  9. {
  10.     ui->setupUi(this);
  11.     clicked();
  12. }
  13.  
  14. Dialog::~Dialog()
  15. {
  16.     delete ui;
  17. }
  18.  
  19. const QString Dialog::clicked()
  20. {
  21.  emit click();
  22.  return pass1;
  23. }

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "dialog.h"
  4. QString pass;
  5.  
  6. MainWindow::MainWindow(QWidget *parent) :
  7.     QMainWindow(parent),
  8.     ui(new Ui::MainWindow)
  9. {
  10.     ui->setupUi(this);
  11.     tt = new Dialog(this);
  12.     this->connect(tt, SIGNAL(click()), this, SLOT(clicked()));
  13.     tt->show();
  14. }
  15.  
  16. MainWindow::~MainWindow()
  17. {
  18.     delete ui;
  19. }
  20.  
  21. void MainWindow::clicked(){
  22.     pass = tt->clicked();
  23.     ui->pushButton->setText(pass);
  24. }

dialog.h

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3.  
  4. #include <QDialog>
  5.  
  6. namespace Ui {
  7.     class Dialog;
  8. }
  9.  
  10. class Dialog : public QDialog
  11. {
  12.     Q_OBJECT
  13.  
  14. public:
  15.     explicit Dialog(QWidget *parent = 0);
  16.     ~Dialog();
  17.  
  18. const QString clicked() ;
  19. private slots:
  20.  
  21. signals:
  22.     void click();
  23.  
  24. private:
  25.     Ui::Dialog *ui;
  26. };
  27.  
  28. #endif // DIALOG_H

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. class Dialog;
  6. namespace Ui {
  7.     class MainWindow;
  8. }
  9.  
  10. class MainWindow : public QMainWindow
  11. {
  12.     Q_OBJECT
  13.  
  14. public:
  15.     explicit MainWindow(QWidget *parent = 0);
  16.     ~MainWindow();
  17.     void clicked();
  18.  
  19. private:
  20.     Ui::MainWindow *ui;
  21.     Dialog *tt;
  22. };
  23.  
  24. #endif // MAINWINDOW_H

4 replies

August 14, 2011

sierdzio sierdzio
Area 51 Engineer
2320 posts

A few issues here.

Firs of all, in mainwindow.h, declare clicked() like that:

  1. public slots:
  2. void clicked();

Then, look into your implementation: when click() is emitted, it will invoke mainwindow’s clicked(), which will in turn invoke dialog’s method of the same name, and finally it will re-emit click() signal. Sounds like a loop to me, but I might have overlooked something.

 Signature 

(Z(:^

August 14, 2011

sierdzio sierdzio
Area 51 Engineer
2320 posts

Another problem is, you call clicked() in dialog constructor, which is called in mainwindow BEFORE signal-slot connection. Therefore, your initial signal will not be caught by main window. Mind you, making the connection before dialog instantiation is also wrong – you have to rethink the whole thing.

 Signature 

(Z(:^

August 14, 2011

Zlatomir Zlatomir
Dinosaur Breeder
326 posts

The problem is that you call Dialog::clicked (the function that emits the signal) from the constructor and then you have:

  1.     tt = new Dialog(this);          //signal emited and call all connected slots (NONE)
  2.     this->connect(tt, SIGNAL(click()), this, SLOT(clicked())); //and after the first signal emit you connect

August 14, 2011

kalster kalster
Lab Rat
315 posts

i got it working. i solved it with the first reply advice plus i did not have a click event. thank you for your help.

 
  ‹‹ avi into a stage.      How to change style of window? ››

You must log in to post a reply. Not a member yet? Register here!