QLineEdit :: text返回非字符串

问题描述

获取QLineEdit的文本值时遇到一个奇怪的问题。

在QWidget上有一个QLineEdit和QDialogBu​​ttonBox。单击“确定”按钮后,将读取并保存QLineEdit的值。

这是我的代码

properties.h

#ifndef PROPERTIES_H
#define PROPERTIES_H
#include "config.h"
#include <QtWidgets/QDialog>

namespace Ui {
          class properties;
}

class properties : public QDialog {
      Q_OBJECT

    public:
        explicit properties(QWidget *parent = nullptr);
        ~properties();

    private slots:
        void on_buttonBox_accepted();

    private:
        Ui::properties *pProp;

};

#endif // PROPERTIES_H

properties.cpp

#include <cstdio>
#include "properties.h"
#include "Form.h"
#include "ui_properties.h"
#include <QtWidgets/QFileDialog>

properties::properties(QWidget *parent) : QDialog(parent),pProp(new Ui::properties) {
    pProp->setupUi(this);
    pProp->NameEdit->setText(QString("test"));
}

properties::~properties() { delete pProp; }

void properties::on_buttonBox_accepted()     {
     QString qstr = pProp->NameEdit->text();
     std::string stdstr = qstr.toStdString();
     const char *cstr = stdstr.c_str();

     qDebug() << "qDebug: " << pProp->NameEdit->text();
     std::cout << "cout stdstr: " << stdstr;

     this->close();
}

输出

qDebug:  "test"
cout stdstr:

,它崩溃并显示为“运行时检查失败#2-变量'stdstr'周围的堆栈已损坏”。离开方法时。

当我使用Visual Studio调试器检查stdstr时,其内容类似于“à

我想念什么?

谢谢您的建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)