Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';t在信号/插槽传输后保留数据_C++_Qt - Fatal编程技术网

C++ Can';t在信号/插槽传输后保留数据

C++ Can';t在信号/插槽传输后保留数据,c++,qt,C++,Qt,所以我尝试在两个类之间传递数据。 第一类具有发送QString的信号,第二类具有接收QString的插槽。问题是,接收QString的类没有保留它。我可以在插槽中显示,但不能在课堂的其他地方显示。这是我的密码: class1.h(发送方) class2.h(接收器) class1.cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWi

所以我尝试在两个类之间传递数据。 第一类具有发送QString的信号,第二类具有接收QString的插槽。问题是,接收QString的类没有保留它。我可以在插槽中显示,但不能在课堂的其他地方显示。这是我的密码:

class1.h(发送方)

class2.h(接收器)

class1.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    resolution = "";

    widget = new GLWidget(NULL);
    connect(this, SIGNAL(message(QString)), widget, SLOT(reception(QString)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_radioButton_clicked()
{
    resolution="0.2";
    emit message(resolution);
}

void MainWindow::on_radioButton_3_clicked()
{
    resolution="0.3";
    emit message(resolution);
}

void MainWindow::on_radioButton_2_clicked()
{
    resolution="0.5";
    emit message(resolution);
}
GLWidget::GLWidget(QWidget *parent) :
        QGLWidget(parent)
{
    zoomWindow=0.5;
}

void GLWidget::reception(QString resolution){

   qDebug()<<resolution;               //DISPLAY CORRECTLY

   zoomWindow = resolution.toFloat(); //TRYING TO STOCK THE VALUE OF RESOLUTION                                                .                                       IN ZOOMWINDOW

   qDebug()<<zoomWindow;              //DISPLAY CORRECTLY

}
class2.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    resolution = "";

    widget = new GLWidget(NULL);
    connect(this, SIGNAL(message(QString)), widget, SLOT(reception(QString)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_radioButton_clicked()
{
    resolution="0.2";
    emit message(resolution);
}

void MainWindow::on_radioButton_3_clicked()
{
    resolution="0.3";
    emit message(resolution);
}

void MainWindow::on_radioButton_2_clicked()
{
    resolution="0.5";
    emit message(resolution);
}
GLWidget::GLWidget(QWidget *parent) :
        QGLWidget(parent)
{
    zoomWindow=0.5;
}

void GLWidget::reception(QString resolution){

   qDebug()<<resolution;               //DISPLAY CORRECTLY

   zoomWindow = resolution.toFloat(); //TRYING TO STOCK THE VALUE OF RESOLUTION                                                .                                       IN ZOOMWINDOW

   qDebug()<<zoomWindow;              //DISPLAY CORRECTLY

}
GLWidget::GLWidget(QWidget*父项):
QGLWidget(父级)
{
zoomWindow=0.5;
}
void GLWidget::接收(QString分辨率){

qDebug()我无法重现您的问题。可能问题出在您未共享的部分代码中。但请避免使用与类成员同名的参数。阅读过程很混乱。

是否通过编程或使用表单编辑器将GLWidget添加到主窗口


我有一个类似的代码,我使用表单编辑器添加了GLWidget,要从主窗口访问GLWidget,我必须使用ui->GLWidget->…所以可能您有2个GLWidget对象。我没有在主窗口类上创建任何GLWidget变量。

您没有更改
GLWidget::Receignment中的
分辨率
成员(QString resolution)
。因此,在其他函数中,它绝对会恢复到其原始值。如果我的第一条评论不清楚,只需将
this->resolution=resolution;
添加到
void GLWidget::receipt(QString resolution)
。这与Qt信号和插槽无关。请提供一个完整、简洁的测试用例来再现问题。多个文件是不相关的-请将其最小化为一个
main.cpp
,并删除所有不会改变行为的内容。据推测,它甚至不必是gui应用程序。一个问题是的,你从来没有显示过你的GLWidget。它是故意的,是一个单独的窗口吗?