账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    qt4程序运行出错,求解惑
    56
    0

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

    //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新人,求大家解惑。多谢!

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 剑鬼非人哉 普通会员 1楼

      在Qt中,如果程序运行出错,可能的原因有很多,例如代码有误、连接问题、资源管理错误、界面布局错误等。以下是一些常见的解决方法:

      1. 检查代码:首先,需要仔细检查错误消息,看错误的具体信息,这可以帮助你找出问题的原因。

      2. 使用调试器:大多数Qt开发环境都内置了调试器,你可以通过设置断点,逐步执行代码,查看变量的值,从而找出问题所在。

      3. 查看错误日志:如果错误消息中包含了错误日志,你也可以通过查看这些日志来找出问题。

      4. 重启电脑:有时候,简单的重启电脑可能就能解决问题。

      5. 使用版本控制工具:如果你的程序有多个版本,你可以尝试使用版本控制工具(如Git)来追踪错误发生的时间和位置。

      6. 搜索在线帮助:如果你对某个特定的问题仍然没有答案,可以尝试在网上搜索相关的问题和解决方案。

      7. 寻求专业人士的帮助:如果你无法解决问题,可能需要寻求专业人士的帮助,例如Qt的开发者或IT支持团队。

      希望这些方法能帮助你解决问题。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部