连接信号(Qstring)白色插槽(字符串)的2个不同类别
connect signal (Qstring) whit slot (String) of 2 different class
>i 有 2 个类:类 MaFentre 和 Code
代码.h :
class Code : public QObject {
public :
explicit Code(Q3DScatter *scatter);
public slots:
std::vector<point> readingData(std::string inputFileName);
}
MaFenetre.h :
class MaFenetre : public QWidget
{ Q_OBJECT
public:
MaFenetre();
private:
QLineEdit *entry1;
}
代码.cpp :
std::vector<point> Code::readingData(std::string inputFileName){
// i read a file here
}
我在类 MaFenetre 的构造函数中创建 Code 类对象
Code *modifier = new Code(graph);
用于在插槽和信号之间建立连接
QObject::connect(entry1, SIGNAL(textChanged(QString)),modifier, SLOT(readingDara(std::string inputFileName)))
我知道参数必须是相同的类型,为此我尝试编码:
QObject::connect(entry, SIGNAL(textChanged(QString.toStdString)),modifier, SLOT(readingDara(std::string inputFileName)))
但它不起作用
您的信号和时隙参数不兼容。
您可以使用lambda
函数执行此解决方法
Code *modifier = new Code();
MaFenetre * poMaFenetre = new MaFenetre();
connect(poMaFenetre->Entry(), &QLineEdit::textChanged,
[modifier](const QString & oText)
{
std::vector<int> data = modifier->readingData(oText.toStdString());
// Handle data here...
});
在MaFenetre
class MaFenetre : public QWidget
{
Q_OBJECT
public:
MaFenetre() {entry1.reset(new QLineEdit());}
QLineEdit *Entry() {return entry1.data();}
private:
QScopedPointer<QLineEdit> entry1;
};
使用信号和插槽与调用函数和传递参数不同。
首先,信号和时隙必须具有相同的参数类型,这意味着它们必须使用相同的参数定义。在您的情况下,您必须更改插槽以适应可能的信号。另请注意,在槽调用的情况下,返回值是无用的,所以更好的方法是保持你按原样读取函数,将其移动到私有区域,并创建包装槽:
void Code::readingDataSlot(QString inputFileName)
{
std::vector<point> result = readingData( inputFileName.toStdString() );
// Do what ever you need with result vector
}
并将其连接到信号。
connect(entry1, SIGNAL(textChanged(QString)),modifier, SLOT(readingDataSlot(QString)));
相关文章:
- 在C++中使用具有相同类的多个模板
- 如何在它们之间分配和使用 2 个不同类的函数指针?
- 如何设置一个 std::vector 与另一个,其中两个是不同类的向量?
- 从两个不同类继承的非虚拟基类的访问成员
- C++ I/O 流重载:如何实现同一标头中相同 4 个 I/O 函数的 3 个不同类的友元重载?
- 使用转换运算符相互转换两个不同类的对象
- 两个不同类的运行时多态性
- 不同类别的 2 个对象之间的信号/插槽
- 如何比较两个不同类的对象
- 具有两个不同类的两个不同构造函数
- 读取包含2个不同类的对象的数据文件
- 如何在具有相同基类的两个不同类中获取向量
- 两个不同类的泛型函数
- 指向从 1 个基类继承的不同类对象的指针数组
- 存储多个插件(不同类的对象)并相互访问
- C++来自不同类的多个回调成员函数,而无需 std 和 boost
- 检查来自不同类的两个Rect之间的冲突
- c++中包含两个不同类的对象的列表
- 比较来自两个不同类的对象
- c++ Boost信号连接模板中两个不同类的两个函数