当使用新的信号槽语法时,你需要在类声明中添加Q_OBJECT
宏,并包含对应的头文件。以下是一个完整的示例代码:
// myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include <QComboBox>
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = nullptr);
public slots:
void onCurrentIndexChanged(int index);
};
#endif // MYCLASS_H
// myclass.cpp
#include "myclass.h"
MyClass::MyClass(QObject *parent) : QObject(parent)
{
QComboBox *comboBox = new QComboBox();
// 使用新的信号槽连接语法
connect(comboBox, &QComboBox::currentIndexChanged, this, &MyClass::onCurrentIndexChanged);
}
void MyClass::onCurrentIndexChanged(int index)
{
// 处理索引变化事件
}
请注意,以上代码只是示例,具体实现和逻辑可能根据你的需求而有所不同。同时,请确保在.pro文件中添加了QT += core gui
以及其他所需模块,以正确编译运行Qt代码。
希望对你有所帮助!如有任何进一步问题,请随时提问。
内容由零声教学AI助手提供,问题来源于学员提问