[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_OBJECTpublic:
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();
}
@How should it be correct?
-
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.
-
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 :)