[SOLVED] QLineEdit and connect problem.



  • Hello!
    Few days ago I've started learning Qt and found some problems.
    I am making small program with QLineEdit and QPushButton. I want to show text of QlineEdit in QMessageBox after clicking a button but the program crashes. What's wrong? Here is code:

    dialog.h

    @#ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>
    #include <QtCore>
    #include <QtGui>
    #include <QtWidgets>

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);

    private slots:
    void on_pushButton_clicked();

    private:
    QLineEdit *lineEdit;
    QPushButton *button;
    QVBoxLayout *layout;
    };

    #endif // DIALOG_H@

    dialog.cpp
    @#include "dialog.h"

    Dialog::Dialog(QWidget *parent) : QDialog(parent)
    {
    QLineEdit *lineEdit = new QLineEdit;
    QPushButton *button = new QPushButton("Click");
    QVBoxLayout *layout = new QVBoxLayout;
    setLayout(layout);
    layout->addWidget(lineEdit);
    layout->addWidget(button);
    connect(button,SIGNAL(clicked()),this,SLOT(on_pushButton_clicked()));
    }

    void Dialog::on_pushButton_clicked()
    {
    QMessageBox::information(this,"Message",lineEdit->text());
    }
    @

    main.cpp
    @#include "dialog.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec(&#41;;
    

    }
    @

    How should it be correct?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are shadowing your class member variables in your constructor. Correct that and you'll be good to go



  • Thank you very much! Now I see.


  • Lifetime Qt Champion

    You're welcome !

    If that answers your question, please update the thread title prepending [solved] so other forum users may know solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.