首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >比较QString时出错

比较QString时出错
EN

Stack Overflow用户
提问于 2013-05-16 02:16:03
回答 1查看 175关注 0票数 0

我正在试着用Qt写一个简单的登录表单。如果用户名和密码正确,它应该会打开另一个表单。但它的行为非常奇怪,下面是我的代码:

代码语言:javascript
运行
复制
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语句中。我不知道为什么,但当它运行时,它会导致我的窗口崩溃。这也不起作用:

代码语言:javascript
运行
复制
void login::finduser()
{

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

    return;
}

我不知道我做错了什么,这里还有调试消息:次等停止,因为它收到了来自操作系统的信号名称: sigsegv信号含义:分段故障

EN

回答 1

Stack Overflow用户

发布于 2013-05-16 02:23:15

代码语言:javascript
运行
复制
QLineEdit * username  = new QLineEdit;

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16572227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档