recover a slot parameter
Unsolved
General and Desktop
-
Hello ,
In my code I have a slot that receive a type parameter QString , I want to assign the value for this parameter in another variable , or the use DIRECTLY , in another function of the same class .in my code the signal is emitted from another class
help me please and
thank youMyWidget.h
#ifndef MYWIDGET_H #define MYWIDGET_H #include"myclass.h" #include <QWidget> #include <QtWidgets> namespace Ui { class MyWidget; } class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); ~MyWidget(); private: Ui::MyWidget *ui; signals: void redirectData(QString data); public slots: void sendData(); private: MyClass *myClass; // the object to receive and output the data }; #endif // MYWIDGET_H
MyWidget.cpp
#include "mywidget.h" #include "ui_mywidget.h" MyWidget::MyWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MyWidget) { ui->setupUi(this); myClass = new MyClass(); connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(sendData())); connect(this, SIGNAL(redirectData(QString)), myClass, SLOT(outputData(QString))); } MyWidget::~MyWidget() { delete ui; } void MyWidget::sendData() { emit redirectData(ui->lineEdit->text()); myClass->show(); }
MyClass.h
#ifndef MYCLASS_H #define MYCLASS_H #include <QWidget> namespace Ui { class MyClass; } class MyClass : public QWidget { Q_OBJECT public: explicit MyClass(QWidget *parent = 0); ~MyClass(); QString x; private: Ui::MyClass *ui; signals: void send2(QString); public slots: void outputData(QString data); }; #endif // MYCLASS_H
MyClass.cpp
#include "myclass.h" #include "ui_myclass.h" MyClass::MyClass(QWidget *parent) : QWidget(parent), ui(new Ui::MyClass) { ui->setupUi(this); ui->plainTextEdit->insertPlainText(x); // show x in plainTextEdit } MyClass::~MyClass() { delete ui; } void MyClass::outputData(QString data){ x=data; // affect data a x }
here is a catch
I want to display x in qplaintext
this method is correct but does not fit with the rest of my code
I want to recover data in x
MyClass.cpp
#include "myclass.h" #include "ui_myclass.h" MyClass::MyClass(QWidget *parent) : QWidget(parent), ui(new Ui::MyClass) { ui->setupUi(this); } MyClass::~MyClass() { delete ui; } void MyClass::outputData(QString data){ ui->plainTextEdit->insertPlainText(data); }
here is a catch
catch 2 -
Hi,
You have to refresh x in your slot and update your QPlainTextEdit accordingly.