PDA

View Full Version : Emitting Signal from Dialog Button to MainWindow LineEdit Widget



jeffd589
1st March 2013, 16:36
I have a MainWindow object built with Qt Creator. It contains one pushButton and one lineEdit widget. When the pushButton is clicked, a modeless dialog is shown. The modeless dialog contains one pushButton. When the dialog pushButton is clicked, I want to send a text message QString back to the lineEdit widget in the MainWindow. After reviewing the other similar Qt forum issues, I think a signal should be emitted from the Dialog pushButton to a slot "on_lineEdit_textChanged(const QString &arg1)" I added in the MainWindow object. How do I implement the signal to connect the button push event in the dialog to the lineEdit in the MainWindow? Thanks in advance.

MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog_jeff.h"

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

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

void MainWindow::on_pushButton_clicked()
{
Dialog_jeff *my_dialog;

my_dialog = new Dialog_jeff;

my_dialog->show();
}

void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
ui->lineEdit->setText(arg1);
}


Dialog: dialog_jeff.cpp:

#include "dialog_jeff.h"
#include "ui_dialog_jeff.h"

Dialog_jeff::Dialog_jeff(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog_jeff)
.....

void Dialog_jeff::on_pushButton_clicked()
{
//set text message back to mainwindow widget


}

alizadeh91
1st March 2013, 17:30
you can simply define a signal in dialog class:

void sendString(QString str);
then in mainwindow, you initializing the dialog, you have to connect the signal in dialog to a slot:

connect(dialog,SIGNAL(sendString(QString)),this,SL OT(receiveStr(QString)));.
Now just emit this signal in form whenever you want.

Ashkan_s
1st March 2013, 17:40
If you want to set text in the lineEdit you should connect your signal to QLineEdit::setText

About implementing a signal and more about signals and slots, I recommend you to read http://qt-project.org/doc/qt-4.8/signalsandslots.html

vanduongbk
23rd June 2013, 16:01
you can simply define a signal in dialog class:

void sendString(QString str);
then in mainwindow, you initializing the dialog, you have to connect the signal in dialog to a slot:

connect(dialog,SIGNAL(sendString(QString)),this,SL OT(receiveStr(QString)));.
Now just emit this signal in form whenever you want.

hello you,
i also have a similar problem ,i read your reply and do it ,compiler nowith any error ,but the event is no change,please help me
in myform1.h ,i define:

signals:
void sendString(QString str);


and in myform1.cpp

void form1:on_button_clicked()
{
emit sendString("complete");
or emit(sendString("complete"));
}


in my mainform.h ,i have consists of myform1.h
and in mainform.cpp

myform1*myform;
myform = new myform1;
myform->show();
connect(myform,SIGNAL(sendString(QString)),this,SL OT(updatetext(QString)));
....
void mainform:updatetext(QString text)
{
ui->label1->setText(text);
}