QLineEdit 访问其文本的错误

QLineEdit bug in access to its text

请指导我在阅读 qlineedit 的文本时找到这个简化代码的问题。我的代码在 editUser->text() 行中退出。当我删除此行时,其他一切都正常。

#include ...
QString USERID_LOG="SomeThing";
logDialog::logDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::logDialog)
{
    ui->setupUi(this);
    QLineEdit* editUser= new QLineEdit( this );
    QPushButton* okButton = new QPushButton(tr("OK"));
    connect(okButton, SIGNAL(clicked()), this,SLOT(okSlot()));
     ...
}
void logDialog::okSlot()
{  USERID_LOG=editUser->text();  //////// Error is here
   logDialog::accept();
   return; }

QString logDialog::GetUser()
{
return(USERID_LOG);
}
////////////////////////////logdialog.h/////////////////////////
QT_BEGIN_NAMESPACE
namespace Ui { class logDialog; }
QT_END_NAMESPACE

class logDialog : public QDialog
{
    Q_OBJECT
 public:
    logDialog(QWidget *parent = nullptr);
    ~logDialog();
     QString GetUser();
 public slots:
    void okSlot();
private:
    QLineEdit* editUser;
    QPushButton* okButton;
    Ui::logDialog *ui;
};

考虑你的构造函数...

logDialog::logDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::logDialog)
{
    ui->setupUi(this);
    QLineEdit* editUser= new QLineEdit( this );
    QPushButton* okButton = new QPushButton(tr("OK"));
    connect(okButton, SIGNAL(clicked()), this,SLOT(okSlot()));
     ...
}

线条...

QLineEdit* editUser= new QLineEdit( this );
QPushButton* okButton = new QPushButton(tr("OK"));

redeclare/redefine 两个局部作用域变量 editUserokButton 隐藏同名成员变量。相反,您应该简单地自己初始化成员变量...

editUser= new QLineEdit( this );
okButton = new QPushButton(tr("OK"));

或者,也许更好,在 ctor 的初始化列表中执行初始化...

logDialog::logDialog(QWidget *parent)
    : QDialog(parent)
    , editUser(new QLineEdit(this))
    , okButton(new QPushButton(tr("OK")))
    , ui(new Ui::logDialog)
{
    ui->setupUi(this);
    connect(okButton, SIGNAL(clicked()), this,SLOT(okSlot()));
    ...