PDA

View Full Version : how to get text from line edit after user clicks the submit button



jshobhit
1st June 2018, 09:25
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.

shivendra46d
1st June 2018, 10:03
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));

Lesiok
1st June 2018, 10:54
Show how this "another window" is activated.

jshobhit
1st June 2018, 18:03
thanx this was exactly what i needed :)

jshobhit
8th June 2018, 11:21
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::on_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::on_pushButton_clicked()
{
fi = new file(this);
fi->show();

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

superpomax
8th June 2018, 21:23
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

d_stranz
9th June 2018, 00:09
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.