C++ 信号没有发出

C++ 信号没有发出,c++,qt,qt5.4,C++,Qt,Qt5.4,我对没有发出信号有意见。以下是该问题的最简单示例: main.cpp-标准main #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } 组合框 #ifndef COMBOBOX_H #defin

我对没有发出信号有意见。以下是该问题的最简单示例:

main.cpp-标准main

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
组合框

#ifndef COMBOBOX_H
#define COMBOBOX_H

#include <QComboBox>
#include <QDebug>

class combobox : public QWidget
{
    Q_OBJECT

public:
    combobox();
    ~combobox();

    QComboBox *box = new QComboBox;

public slots:
    void selection_changed();
};

#endif // COMBOBOX_H
#ifndef组合框
#定义组合框
#包括
#包括
类组合框:公共QWidget
{
Q_对象
公众:
组合框();
~combobox();
QCOMBOX*box=新的QCOMBOX;
公众时段:
无效选择_已更改();
};
#endif//组合框
combobox.cpp

#include "combobox.h"

combobox::combobox()
{
    QString string = "test 1";
    box->addItem(string);
    string = "test 2";
    box->addItem(string);
    box->setCurrentIndex(0);
    connect(box, SIGNAL(currentIndexChanged(int)), this, SLOT(selection_changed()));
}

combobox::~combobox()
{

}

void combobox::selection_changed()
{
    qDebug() << "Selection changed";
    box->setCurrentIndex(-1);
}
#包括“combobox.h”
combobox::combobox()
{
QString=“测试1”;
框->添加项(字符串);
string=“测试2”;
框->添加项(字符串);
框->设置当前索引(0);
连接(框,信号(currentIndexChanged(int)),此,插槽(选择_changed());
}
combobox::~combobox()
{
}
无效组合框::选择内容\u已更改()
{
qDebug()setCurrentIndex(-1);
}
您需要在编译之前运行qmake

当我运行此程序并更改组合框时,不会执行索引选择\u changed。为什么?

信号和插槽连接肯定可以工作,因为如果我添加box->setCurrentIndex(1);在combobox构造函数的末尾,选择_changed将执行一次

我在QtCreator中使用Qt5.4


提前谢谢

这个问题是因为您在堆栈中创建了
组合框
类对象。因此,如果您在此类的析构函数中写入一些调试信息,您可以看到对象在
main窗口
constructor超出范围后立即被销毁:

ComboBox::~ComboBox()
{
    qDebug() << Q_FUNC_INFO;
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setupUi(this);


    QWidget *mainWidget = new QWidget;
    setCentralWidget(mainWidget);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    ComboBox combo_box;
    mainLayout->addWidget(combo_box.box);
    mainWidget->setLayout(mainLayout);

    qDebug() << Q_FUNC_INFO;
}
ComboBox::~ComboBox()
{
qDebug()addWidget(combo_box.box);
mainWidget->setLayout(mainLayout);

qDebug()检查connect语句的返回。另外,在调试器中运行此操作,在connect上放置一个断点,并在跨过连接行时检查应用程序输出窗口,以确保正确建立连接。最后,使用,它将在编译期间验证连接。
#include "combobox.h"

combobox::combobox()
{
    QString string = "test 1";
    box->addItem(string);
    string = "test 2";
    box->addItem(string);
    box->setCurrentIndex(0);
    connect(box, SIGNAL(currentIndexChanged(int)), this, SLOT(selection_changed()));
}

combobox::~combobox()
{

}

void combobox::selection_changed()
{
    qDebug() << "Selection changed";
    box->setCurrentIndex(-1);
}
ComboBox::~ComboBox()
{
    qDebug() << Q_FUNC_INFO;
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setupUi(this);


    QWidget *mainWidget = new QWidget;
    setCentralWidget(mainWidget);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    ComboBox combo_box;
    mainLayout->addWidget(combo_box.box);
    mainWidget->setLayout(mainLayout);

    qDebug() << Q_FUNC_INFO;
}