Qt5将自定义窗口关闭信号连接到另一个窗口

时间:2021-05-02 20:49:56

I'm trying to connect a signal from one class to a slot in another class but when I do, my application crashes on startup. I read some other posts on here and the Qt forums that eluded to connecting custom signals as such but I think I'm connecting them wrong. Any help is greatly appreciated.

我试图将一个类中的信号连接到另一个类中的一个插槽,但是当我连接时,我的应用程序在启动时崩溃了。我在这里阅读了一些其他的文章和Qt论坛,这些论坛避开了连接自定义信号,但我认为我把它们连接错了。非常感谢您的帮助。

AdministrativeWindow.h

AdministrativeWindow.h

class AdministrativeWindow : public QMainWindow
{
    Q_OBJECT

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

    private slots:
        void on_actionExit_Administrative_Window_triggered();

    private:
        Ui::AdministrativeWindow *ui;

    signals:
        void windowClose();
};

AdministrativeWindow.cpp

AdministrativeWindow.cpp

void AdministrativeWindow::on_actionExit_Administrative_Window_triggered()
{
    emit windowClose();
    close();
}

MainWindow.cpp

MainWindow.cpp

connect(adminWindow, SIGNAL(windowClose()), this, SLOT(adminWindowClose()));

void MainWindow::on_ConfigureUsersBtn_clicked()
{
    if(adminWindow == NULL)
    {
        adminWindow = new AdministrativeWindow();
        adminWindow->show();
    }
    else if(adminWindow->isVisible())
    {
        adminWindow->activateWindow();
        adminWindow->showNormal();
    }
    else
    {
        adminWindow->show();
    }
}

void MainWindow::on_adminWindowClose()
{
    delete adminWindow;
    adminWindow = NULL;
}

2 个解决方案

#1


0  

You need to ensure that you're using valid adminWindow pointer on connect:

您需要确保在connect上使用了有效的adminWindow指针:

void MainWindow::on_ConfigureUsersBtn_clicked()
{
    if(adminWindow == NULL)
    {
        adminWindow = new AdministrativeWindow();
        connect(adminWindow, SIGNAL(windowClose()), this, SLOT(adminWindowClose()));
        adminWindow->show();
    }

#2


0  

be sure to do a connect after pointer has been initialized

在指针初始化后,请确保进行连接

connect(adminWindow, SIGNAL(windowClose()), this, SLOT(adminWindowClose()));
           ^
        valid ptr

also private slots are private if called as regular member functions but always public for connection. I think this is cleaner design to define slots as public since their purpose is communication and use private functions as usual when they are needed unless you really need such mixed concept as private slot (it might exist however and one can imagine some special circumstances in which this might have sense, I assume this is not the case here)

此外,如果作为常规成员函数调用,则私有槽是私有的,但对于连接,总是公共的。我认为这是更清洁的设计定义槽作为公共因为他们的目的是交流和使用私有函数时,像往常一样等混合的概念是必要的,除非你真的需要私人槽(不过可能存在,一个人可以想象一些特殊的情况下,这可能会有意义,我认为这不是这里的情况)

#1


0  

You need to ensure that you're using valid adminWindow pointer on connect:

您需要确保在connect上使用了有效的adminWindow指针:

void MainWindow::on_ConfigureUsersBtn_clicked()
{
    if(adminWindow == NULL)
    {
        adminWindow = new AdministrativeWindow();
        connect(adminWindow, SIGNAL(windowClose()), this, SLOT(adminWindowClose()));
        adminWindow->show();
    }

#2


0  

be sure to do a connect after pointer has been initialized

在指针初始化后,请确保进行连接

connect(adminWindow, SIGNAL(windowClose()), this, SLOT(adminWindowClose()));
           ^
        valid ptr

also private slots are private if called as regular member functions but always public for connection. I think this is cleaner design to define slots as public since their purpose is communication and use private functions as usual when they are needed unless you really need such mixed concept as private slot (it might exist however and one can imagine some special circumstances in which this might have sense, I assume this is not the case here)

此外,如果作为常规成员函数调用,则私有槽是私有的,但对于连接,总是公共的。我认为这是更清洁的设计定义槽作为公共因为他们的目的是交流和使用私有函数时,像往常一样等混合的概念是必要的,除非你真的需要私人槽(不过可能存在,一个人可以想象一些特殊的情况下,这可能会有意义,我认为这不是这里的情况)