0

我试图在 Qt 中编写一个简单的登录表单。如果用户名和密码正确,它应该打开另一个表单。但它的行为真的很奇怪这是我的代码:

login::login(QWidget *parent) :
    QDialog(parent)
{
    QPushButton * login_button = new QPushButton;
    QPushButton * exit = new QPushButton;
    login_button->setText("LOGIN");
    exit->setText("EXIT");

    QLineEdit * username  = new QLineEdit;
    QLineEdit * password  = new QLineEdit;
    QVBoxLayout * login_layout = new  QVBoxLayout ;
    QHBoxLayout * button_layout = new  QHBoxLayout ;
    username->setText("Enter Username ...");
    password->setText("Enter Password ... ");

    exit->connect(exit,SIGNAL(pressed()),this , SLOT(close()));
    login_layout->addWidget(username);
    login_layout->addWidget(password);

    button_layout->addWidget(login_button);
    button_layout->addWidget(exit);

    login_layout->addLayout(button_layout);
    this->setLayout(login_layout);

    this->connect(login_button,SIGNAL(clicked()),this,SLOT(finduser()));
}

void login::finduser()
{

    if (username->text().compare("admin")) //  <---- problem !!
    emit showmanage() ;

    return;
}

finduser 是我的对话类的一个插槽。它发出“showmanage”信号,打开我愿意打开的表单。实际问题出在 if 语句中。我不知道为什么,但是当它运行时会导致我的窗口崩溃。这也不起作用:

void login::finduser()
{

    if (username->text()=="admin") //  <---- problem !!
    emit showmanage() ;

    return;
}

我不知道我做错了什么也继承了调试消息:下级停止,因为它收到了来自操作系统信号名称的信号:sigsegv 信号含义:分段错误

4

1 回答 1

3
QLineEdit * username  = new QLineEdit;

鉴于您没有收到编译错误,我假设您有一个未初始化的成员变量username。但是在构造函数中,您声明了一个具有相同名称的新局部变量。块作用域变量username不同于成员变量。

于 2013-05-15T18:23:14.643 回答