There is an error in the operation of the qt4 program.

A simple interface program that accepts username and password, slot function is temporarily empty, compilation can pass, runtime error.

//passwddialog.h
-sharpifndef PASSWDDIALOG_H
-sharpdefine PASSWDDIALOG_H

-sharpinclude <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;
};

-sharpendif

//passwddialog.cpp
-sharpinclude <QtGui>

-sharpinclude "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
-sharpinclude <QApplication>

-sharpinclude "passwddialog.h"

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

compilation is fine, running error:

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

qt newcomer, please solve the problem. Thanks!

Mar.05,2021
Menu