一个简单的接受用户名和密码的界面程序,槽函数暂时为空,编译可以通过,运行时出错。
//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新人,求大家解惑。多谢!