简体   繁体   中英

Signals and slots not working in qt

in my Qt program I need the dialog to send a signal to a slot in the mainwindow. I have set the connection up correctly to the best of my knowledge and it does not give me any errors during compile or run time but for some reason it just doesn't do anything when the button that is supposed to activate the signal is clicked. Why is this happening?

beastiary.h (mainwindow header)

namespace Ui {
class Beastiary;
}

class Beastiary : public QMainWindow
{
    Q_OBJECT

public:

    explicit Beastiary(QWidget *parent = 0);

    Ui::Beastiary *ui;

    QStringList MyList;

    ~Beastiary();

public slots:
    void refresh();

private slots:

    void on_pushButton_clicked();

    void on_listWidget_itemClicked(QListWidgetItem);

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

beastiary.cpp (mainwindow cpp file)

Beastiary::Beastiary(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Beastiary)
{
    ui->setupUi(this);
    Dialog  dialog;
    connect(&dialog, SIGNAL(gorefresh()),this, SLOT(refresh())) ;


    void Beastiary::refresh()
    {
      qDebug () << "recieved";
      ui->listWidget->clear();
      QString path = "C:/Program Files/Bargest/bin";
      QDir myPath(path);
      myPath.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
      MyList = myPath.entryList();
      ui->listWidget->addItems(MyList);
    }

dialog.h

namespace Ui {
class Dialog;

}

class Dialog : public QDialog
{
    Q_OBJECT


public:

    explicit Dialog(QWidget *parent = 0);
    ~Dialog();


signals:
    void gorefresh();

private slots:
    void on_pushButton_done_clicked();

    void on_pushButton_cancel_clicked();

    void on_pushButton_clicked();

private:
    Ui::Dialog *ui;

dialog.cpp

void Dialog::on_pushButton_done_clicked()
{    
  emit gorefresh();
}

I did leave out large parts of the code that just have nothing to do with the actual signals and slots mechanism at play here.

You're only connecting the dialog instance you created locally in the Bestiary 's constructor, which goes out of scope as the constructor finishes.

connect is connecting instances , not classes . That means you need to connect each created dialog:

void Beastiary::on_pushButton_clicked() {
    Dialog* ad = new Dialog(this);
    connect(ad, SIGNAL(gorefresh()), this, SLOT(refresh()));
    ad->show();
}

While at it, you should seriously consider using the type-safe connect syntax that came with Qt 5:

void Beastiary::on_pushButton_clicked() {
    Dialog* ad = new Dialog(this);
    connect(ad, &Dialog::gorefresh, this, &Bestiary::refresh));
    ad->show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM