Results 1 to 7 of 7

Thread: how to get text from line edit after user clicks the submit button

  1. #1
    Join Date
    Jun 2018
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default how to get text from line edit after user clicks the submit button

    i have window with a label and a push button. When user clicks the button it takes them to another window with a line edit and a submit button.Now i want the text which user typed to replace the text of label in my first window.
    ]i have successfully made the program except that when user types the text in line edit no matter what the text of label is changed to blank(that is no text). I know this is because QString which i am using to set label of the text is initialized to null and as soon as the new window is created a null initialized QString is set to the text of label. I am aware of the fact that i need to use signals and slots but just couldn't figure out what to connect and to what?

    please if possible try to give code example

    any help will be seriously appreciated.
    Attached Images Attached Images

  2. #2
    Join Date
    Dec 2012
    Posts
    45
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: how to get text from line edit after user clicks the submit button

    Write a signal in your second window which emits the string and connect that signal to a slot in your main window.
    Ex :
    .
    .
    emit sinal_xyz("Your string from line edit ");
    connect (..;SIGNAL(sinal_xyz(QString),..;SLOT(slot_xyz(QSt ring));

  3. The following user says thank you to shivendra46d for this useful post:

    jshobhit (1st June 2018)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get text from line edit after user clicks the submit button

    Show how this "another window" is activated.

  5. #4
    Join Date
    Jun 2018
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: how to get text from line edit after user clicks the submit button

    thanx this was exactly what i needed

  6. #5
    Join Date
    Jun 2018
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: how to get text from line edit after user clicks the submit button

    but it is not working.

    these are my codes

    second.h

    #ifndef FILE_H
    #define FILE_H

    #include <QMainWindow>

    namespace Ui {
    class file;
    }

    class file : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit file(QWidget *parent = 0);
    ~file();
    QString path;

    private slots:
    void on_pushButton_clicked();
    signals:
    void emitSt(QString a);
    private:
    Ui::file *ui;
    };

    #endif // FILE_H


    second.cpp

    #include "second.h"
    #include "ui_file.h"

    file::file(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::file)
    {
    ui->setupUi(this);
    }

    file::~file()
    {
    delete ui;
    }

    void file:n_pushButton_clicked()
    {
    path = ui->lineEdit->text();
    emit emitSt(path);
    }


    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    #include "second.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();
    void setText(QString text);

    private:
    Ui::MainWindow *ui;
    file *fi;
    };

    #endif // MAINWINDOW_H



    mainwindow.cpp


    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::setText(QString text)
    {
    ui->label->setText(text);
    }

    void MainWindow:n_pushButton_clicked()
    {
    fi = new file(this);
    fi->show();

    connect(fi,SIGNAL(emitSt()),this,SLOT(setText()));
    }

  7. #6
    Join Date
    Jun 2018
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to get text from line edit after user clicks the submit button

    1. In second.h, Signals only have a type, no parameters
    2. In mainWindow.cpp, the connect function require the type of parameters

    1. void emitSt(QString);
    2. connect(fi,SIGNAL(emitSt(QString)),this,SLOT(setTe xt()));

    Still a beginner, but I think it should work with this

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to get text from line edit after user clicks the submit button

    I have no idea where this pushbutton lives - you have two QMainWindow classes. Do both of them have pushbuttons? Only one of them? In any case, whichever window is supposed to tell the other one about the text actually has to "emit" the signal with the new text (by calling "emitSt" or whatever the signal is called), and the connection to the window that is supposed to receive the signal has to be made -before- the signal is emitted. Calling connect() doesn't do anything more than set up the communication channel between signal and slot. The signal has to be emitted before anything will happen in the receiving slot.

    Look at the Signals and Slots tutorial in the Qt documentation or Google for it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QTableView line edit clears the text on edit
    By PlasticJesus in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2015, 20:06
  2. button to save line edit text to file
    By davidrhcp in forum Newbie
    Replies: 6
    Last Post: 7th May 2014, 11:41
  3. Clear button in line edit
    By MTK358 in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2010, 14:27
  4. clear button for line edit
    By jayreddy in forum Qt Programming
    Replies: 6
    Last Post: 3rd December 2009, 05:36
  5. Line edit and push button
    By dela in forum Newbie
    Replies: 1
    Last Post: 10th December 2008, 17:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.