qt4程序运行出错,求解惑

一个简单的接受用户名和密码的界面程序,槽函数暂时为空,编译可以通过,运行时出错。

//passwddialog.h
#ifndef PASSWDDIALOG_H
#define PASSWDDIALOG_H

#include <QDialog>

class QLabel;
class QLineEdit;
class QPushButton;

class PasswdDialog : public QDialog{
    Q_OBJECT

public:
    PasswdDialog(QWidget* parent = 0);

private slots:
    void okClicked();
    void onLineEditChanged(const QString& str);
    
private:
    QLabel*        usernameLabel;
    QLabel*        passwdLabel;
    QLineEdit*    usernameLineEdit;
    QLineEdit*    passwdLineEdit;
    QPushButton*    okButton;
    QPushButton*    cancelButton;
};

#endif

//passwddialog.cpp
#include <QtGui>

#include "passwddialog.h"

PasswdDialog::PasswdDialog(QWidget* parent)
    : QDialog(parent)
{
    usernameLabel = new QLabel(tr("&Username:"));
    usernameLineEdit = new QLineEdit;
    usernameLabel->setBuddy(usernameLineEdit);

    passwdLabel = new QLabel(tr("&Password:"));
    passwdLineEdit = new QLineEdit;
    passwdLabel->setBuddy(passwdLineEdit);

    okButton = new QPushButton(tr("&Ok"));
    okButton->setDefault(true);
    okButton->setEnabled(false);
    cancelButton = new QPushButton(tr("&Cancel"));

    connect(okButton, SIGNAL(clicked()),
            this, SLOT(okClicked()));
    connect(cancelButton, SIGNAL(clicked()),
            this, SLOT(close()));
    connect(usernameLineEdit, SIGNAL(textChanged(const QString&)),
            this, SLOT(onLineEditChanged(const QString&)));
    connect(passwdLineEdit, SIGNAL(textChanged(const QString&)),
            this, SLOT(onLineEditChanged(const QString&)));

    QHBoxLayout* topLayout = new QHBoxLayout;
    topLayout->addWidget(usernameLabel);
    topLayout->addWidget(usernameLineEdit);

    QHBoxLayout* middleLayout = new QHBoxLayout;
    middleLayout->addWidget(passwdLabel);
    middleLayout->addWidget(passwdLineEdit);

    QHBoxLayout* bottomLayout = new QHBoxLayout;
    bottomLayout->addWidget(okButton);
    bottomLayout->addWidget(cancelButton);
    bottomLayout->addStretch();

    QVBoxLayout* layout = new QVBoxLayout;
    layout->addLayout(topLayout);
    layout->addLayout(middleLayout);
    layout->addLayout(bottomLayout);
    setLayout(layout);

    setWindowTitle(tr("Sign in"));
    setFixedHeight(sizeHint().height());
}

void PasswdDialog::okClicked(){
    ;
}

void PasswdDialog::onLineEditChanged(const QString& str){
    ;
}
//main.cpp
#include <QApplication>

#include "passwddialog.h"

int main(int argc, char** argv){
    QApplication app(argc, argv);
    PasswdDialog* passwdDialog;
    passwdDialog->show();
    return app.exec();
}

编译没问题,运行出错:

➜ passwd ./passwd
Maximum number of clients reached: cannot connect to X server :0
[1] 4766 segmentation fault (core dumped) ./passwd

qt新人,求大家解惑。多谢!

阅读 1.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题