C++ Qt在表单之间传递变量
我想将由第一个窗体打开的窗体中的字符串传递给第一个窗体。 我是C++新手。 这是我的密码 Form1.h//main formC++ Qt在表单之间传递变量,c++,qt,qwidget,C++,Qt,Qwidget,我想将由第一个窗体打开的窗体中的字符串传递给第一个窗体。 我是C++新手。 这是我的密码 Form1.h//main form #include "dialog.h" namespace Ui { class Form1; } class Form1 : public QMainWindow { Q_OBJECT public: explicit Form1(QWidget *parent = 0); ~Form1(); void refresh(QString s
#include "dialog.h"
namespace Ui {
class Form1;
}
class Form1 : public QMainWindow
{
Q_OBJECT
public:
explicit Form1(QWidget *parent = 0);
~Form1();
void refresh(QString str_local);
private slots:
void on_pushButton_clicked();
private:
Ui::Form1 *ui;
Dialog *dialog1;
};
//表格1.cpp
void Form1::on_pushButton_clicked()
{
dialog1= new Dialog; //Create new form with other class
dialog1->show();
QObject::connect(dialog1, SIGNAL(change(str_remote)), this, SLOT(refresh(str_local))); //Connect when is emit signal cambia in the child form and pass the string to local function
}
void Form1::refresh(QString str_local)
{
qDebug() << "Back to main" << str_local;
ui->label->setText(str_local);
}
//dialog.cpp
我得到一个错误:
没有这样的信号对话框::change(str_remote)in../Format/form1.cpp:22。这里有一些奇怪的代码:
QObject::connect(dialog1, SIGNAL(change(str_remote)), this, SLOT(refresh(str_local)));
- 因为您的类已经间接继承了QObject,所以您可以简单地删除范围说明符
- 您的目标可能是使用新的编译时语法信号槽机制
- 您尚未在头文件中将插槽标记为插槽
- 您正在尝试使用旧的信号/插槽语法,将信号和插槽的变量名与类型相反
- 您的信号未使用常数T(即常数参考)的良好实践
- 您正在显式地指定
,而它可以被删除。这是公认的个人品味此
- 您不遵循Qt信号/插槽命名约定,例如,您的信号是动词而不是形容词。它也太笼统,而不是像良好实践那样“改变”
connect(dialog1, &Dialog::changed, (=)[const auto &myString] {
ui->label->setText(myString);
});
但是,由于这需要CONFIG+=c++14
,如果您的编译器不支持这一点(例如VS2010),您可以使用以下方法:
connect(dialog1, SIGNAL(change(const QString&)), this, SLOT(refresh(const QString&)));
您必须编写:
QObject::connect(对话框1,信号(更改(QString)),this,SLOT(刷新(QString))代码>改为。好的,我更改了它,现在我得到了:在../Format/Form1中没有这样的插槽Form1::refresh(QString)。cpp:23在构建项目之前是否运行了qmake?将其添加到主窗体:public slots:void refresh(QString s);解决了我的问题。谢谢你的帮助(瓦汉乔)Thnx为您提供提示:)
connect(dialog1, &Dialog::changed, (=)[const auto &myString] {
ui->label->setText(myString);
});
connect(dialog1, SIGNAL(change(const QString&)), this, SLOT(refresh(const QString&)));