Results 1 to 4 of 4

Thread: Emitting Signal from Dialog Button to MainWindow LineEdit Widget

  1. #1
    Join Date
    Feb 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Emitting Signal from Dialog Button to MainWindow LineEdit Widget

    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


    }

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Emitting Signal from Dialog Button to MainWindow LineEdit Widget

    you can simply define a signal in dialog class:
    Qt Code:
    1. void sendString(QString str);
    To copy to clipboard, switch view to plain text mode 
    then in mainwindow, you initializing the dialog, you have to connect the signal in dialog to a slot:
    Qt Code:
    1. connect(dialog,SIGNAL(sendString(QString)),this,SLOT(receiveStr(QString)));
    To copy to clipboard, switch view to plain text mode 
    .
    Now just emit this signal in form whenever you want.

  3. #3
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Emitting Signal from Dialog Button to MainWindow LineEdit Widget

    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

  4. #4
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Emitting Signal from Dialog Button to MainWindow LineEdit Widget

    Quote Originally Posted by alizadeh91 View Post
    you can simply define a signal in dialog class:
    Qt Code:
    1. void sendString(QString str);
    To copy to clipboard, switch view to plain text mode 
    then in mainwindow, you initializing the dialog, you have to connect the signal in dialog to a slot:
    Qt Code:
    1. connect(dialog,SIGNAL(sendString(QString)),this,SLOT(receiveStr(QString)));
    To copy to clipboard, switch view to plain text mode 
    .
    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,SLOT(updatetext(QString)));
    ....
    void mainform:updatetext(QString text)
    {
    ui->label1->setText(text);
    }

Similar Threads

  1. Replies: 3
    Last Post: 25th August 2012, 12:18
  2. dialog button connected into mainwindow
    By ditsikts in forum Newbie
    Replies: 1
    Last Post: 27th October 2011, 00:17
  3. emitting a signal with no button?
    By kja in forum Newbie
    Replies: 3
    Last Post: 29th November 2010, 22:33
  4. Emitting signal from DLL to EXE
    By Miihkali in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2009, 09:32
  5. cost of emitting signal
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 29th November 2006, 09:53

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.