Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QLineEdit and connect problem.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QLineEdit and connect problem.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pawelm
    wrote on last edited by
    #1

    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?

    Sorry for my English.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pawelm
        wrote on last edited by
        #3

        Thank you very much! Now I see.

        Sorry for my English.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved