简体   繁体   中英

Qt no such signal, but it does exist

Qt can't see a signal that exists.

I have a main window that opens up a child window, and the connect statement for that works absolutely fine:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "addstaffform.h"
#include "stafflist.h"
#include "editstaffwindow.h"
#include <QDialog> 
#include <QString>
#include <QList>
#include "person.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_btn_add_clicked();
    void refreshStaffList();

    void receiveData(Person& newPerson);
    void receiveEditedPerson(Person &editedPerson, int index);

    void updateDeleteEnabled();
    void updateEditEnabled();
    void on_btn_delete_staff_clicked();

    void on_btn_staff_edit_clicked();


private:
    Ui::MainWindow *ui;
    QString s;
    QMainWindow *newWin;
    QMainWindow *editStaffWin;
    StaffList *m_staffList;
    QList<Person> m_personList;
    Person m_person;

};

#endif // MAINWINDOW_H

The method in mainwindow.cpp that works with no problem:

void MainWindow::on_btn_add_clicked()
{
    //Open the add staff window, which is a form
    newWin = new AddStaffWindow(this);
    //connect the signal from the new form to the slot in this window to receive the person object
    connect(newWin, SIGNAL(sendData(Person&)), this, SLOT(receiveData(Person&)));
    newWin->show();
}

Signal in addstaffwindow.h (the instantiation of newWin)

signals:
    void sendData(Person &p);

Now, in the edit form (editStaffWin), I have the signal, and I've made sure that Q_OBJECT definitely IS there, cleaned, run QMake and build several times, but it doesn't think that the signal exists.

editstaffwindow.h

signals:
    void sendPerson(Person &, int index);

So in the mainwindow.cpp for editing, sendPerson(Person &, int) doesn't show up:

void MainWindow::on_btn_staff_edit_clicked()
{
    //Get the index of the widget
    int index = ui->lst_staff->currentRow();
    int size = ui->lst_staff->count();

    if (index > -1 && index <= size)
    {
        //get from staff list
        m_person = m_staffList->getStaffAt(index);
        editStaffWin = new EditStaffWindow(this, m_person, index);

        connect(editStaffWin, SIGNAL(sendPerson(Person &, int)), this, SLOT(receiveEditedPerson(Person&,int)));

        editStaffWin->show();
    }

}

Any ideas why? I'm not doing anything different for either of them.

EDIT: full EditStaffWindow header:

#ifndef EDITSTAFFWINDOW_H
#define EDITSTAFFWINDOW_H

#include <QMainWindow>
#include "mainwindow.h"
#include "person.h"
#include <QObject>

namespace Ui {
class EditStaffWindow;
}

class EditStaffWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit EditStaffWindow(QWidget *parent, Person &p, int index);
    ~EditStaffWindow();

signals:
    void sendPerson(Person &, int index);

private:
    Ui::EditStaffWindow *ui;
    Person m_person;
    int m_index;

public slots:
    void showData(Person &p, int index);

private slots:
    void on_btn_save_clicked();
    void on_btn_cancel_clicked();

};

#endif // EDITSTAFFWINDOW_H

It seems that not using the code autocompletion is probably part of the answer, as I have to manually type the signal

connect(editStaffWin, SIGNAL(sendPerson(Person &, int)), this, SLOT(receiveEditedPerson(Person&,int)));

Running clean, qmake, build all may have been part of it as well. What could have also caused a problem was how I was writing the signal for the connect statement, but looking back, it's the same.

What I did do, though, was remove the int declarations from the sendPerson signal, clean, build, then put them back, and it worked.

Apart from that, I'm not sure what caused it not to work.

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