简体   繁体   English

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

[英]Qt no such signal, but it does exist

Qt can't see a signal that exists. Qt无法看到存在的信号。

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

Signal in addstaffwindow.h (the instantiation of newWin) addstaffwindow.h中的信号(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. 现在,在编辑表单(editStaffWin)中,我得到了信号,并且已经确保Q_OBJECT确实存在,已清理,运行QMake并生成了几次,但它不认为该信号存在。

editstaffwindow.h editstaffwindow.h

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

So in the mainwindow.cpp for editing, sendPerson(Person &, int) doesn't show up: 因此,在用于编辑的mainwindow.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();
    }

}

Any ideas why? 有什么想法吗? I'm not doing anything different for either of them. 我对他们两个都没有做任何不同的事情。

EDIT: full EditStaffWindow header: 编辑:完整的EditStaffWindow标头:

#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. 运行干净,qmake,构建所有内容也可能是其中的一部分。 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. 可能还引起问题的是我如何为connect语句编写信号,但回头看,它是相同的。

What I did do, though, was remove the int declarations from the sendPerson signal, clean, build, then put them back, and it worked. 但是,我所做的是从sendPerson信号中删除了int声明,清理,构建,然后将它们放回原位,并且成功了。

Apart from that, I'm not sure what caused it not to work. 除此之外,我不确定是什么原因导致它无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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