Qt没有这样的信号,但它确实存在

Qt no such signal, but it does exist

本文关键字:存在 信号 Qt      更新时间:2023-10-16

Qt 看不到存在的信号。

我有一个打开子窗口的主窗口,其连接语句工作正常:

主窗口.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

主窗口中的方法.cpp可以正常工作:

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

addstaffwindow.h 中的信号(newwin 的实例化)

signals:
    void sendData(Person &p);

现在,在编辑表单(editStaffWin)中,我有了信号,并且我已经确保Q_OBJECT肯定在那里,清理,运行QMake并构建几次,但它认为信号不存在。

编辑员工窗口.h

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

所以在主窗口中.cpp为了编辑,sendPerson(Person &, int) 没有显示:

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

知道为什么吗?我没有为他们中的任何一个做任何不同的事情。

编辑:

完整的编辑人员窗口标题:

#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

似乎不使用代码自动完成可能是答案的一部分,因为我必须手动输入信号

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

运行干净,qmake,构建所有可能也是其中的一部分。还可能导致问题的是我如何为 connect 语句编写信号,但回头看,它是一样的。

不过,我所做的是从 sendPerson 信号中删除 int 声明,清理、构建,然后将它们放回原处,它奏效了。

除此之外,我不确定是什么原因导致它不起作用。