Results 1 to 18 of 18

Thread: button clicked to set text in another form

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default button clicked to set text in another form

    hello everyone
    i have a form1 and mainform,
    how when i click a button in form1 and a text was set to Qlabel in mainform
    examples as when click ok button in form1 and qlabel in mainform was set text "complete transfer"
    thank you

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: button clicked to set text in another form

    You will need to use signal and slot to do so. You can read about it here

    Connect the clicked() signal of the button in form1 to a slot of the widget/object managing the mainfrom, and in this slot set the text of the QLabel.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    Default Re: button clicked to set text in another form

    Quote Originally Posted by Santosh Reddy View Post
    You will need to use signal and slot to do so. You can read about it here

    Connect the clicked() signal of the button in form1 to a slot of the widget/object managing the mainfrom, and in this slot set the text of the QLabel.
    hello,
    i put in mainform.h
    public slots:
    void update_label1(QString string);

    in form1.h ,i have consist a include "mainform.h>
    and in form1.c ,i use command connect(ui->pushButton,SIGNAL(clicked()),what's here,SLOT(update_label1("text")));
    plz me,thank

  4. #4
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: button clicked to set text in another form

    Hi, you can try something like this.

    Create a new signal to form1 with
    Qt Code:
    1. signals:
    2. void transfertCompleted(QString);
    3.  
    4. private slots:
    5. void btnClicked();
    6.  
    7. void form1::btnClicked()
    8. {
    9. emit(transfertCompleted("your text");
    10. }
    To copy to clipboard, switch view to plain text mode 
    In form1 constructor connect the button's clicked signal to the btnClicked slot.

    Now in the mainform, after creating form1 add :
    Qt Code:
    1. connect(form1, SIGNAL(transfertCompleted(QString)), this, SLOT(update_label1(QString)))
    To copy to clipboard, switch view to plain text mode 

    If you donc have to send text from form1 to mainwindow you can avoid using the slot btnClicked() and just connecting both signals directly.

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

    Default Re: button clicked to set text in another form

    hi nix, i dont know what is emit
    and you can a demo code for void transfertCompleted(QString)

    in form1,i have a private slot as:
    void form1::on_pushButton_clicked()
    {
    //some a calculate and write a data ro uart here
    //i want to write text to mainform
    }

    i want to do that when i click button ,it will send data to uart and in mainform ,qlabel text is set as "sent complete"
    thank
    Last edited by vanduongbk; 20th June 2013 at 09:17.

  6. #6
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: button clicked to set text in another form

    First, you should really read some Qt doc about Signals and Slots, there is link is in a previous message. It's the most important thing in Qt, so be sure it's not a waste a time.

    transfertCompleted(QString) is not a method and doesn't have any code : it's a signal.
    emit() is a qt fonction that "emit" (or raise) the signal, then Qt will internaly search all slots connected to that signal and execute them.

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

    Default Re: button clicked to set text in another form

    hello,i execute as:

    in form1,i have a private slot as:
    void form1:n_pushButton_clicked()
    {
    //some a calculate and write a data ro uart here
    //i want to write text to mainform
    emit(transfertCompleted("your text");
    }

    in mainform ,i set slot as:
    public slots:
    void update_label1(const QString &string);

    void mainfor::update_label1(const QString &string)
    {
    ui->label_channel1->clear();
    ui->label_channel1->setText(string);
    }

    and in mainform, connect(form1, SIGNAL(transfertCompleted(QString)), this, SLOT(update_label1(QString)))
    when execute ,it not have error but release error as :The program has unexpectedly finished
    plz help me

  8. #8
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: button clicked to set text in another form

    Use Code Tag please, the code is just not readable the way you did.

    Can you run in debug mode with the debugger in order to get a backtrace.

    Can you post the complete code for those two classes.

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

    Default Re: button clicked to set text in another form

    this is my code ,please help me:
    mainform.h
    #ifndef MAINFORM_H
    #define MAINFORM_H

    #include <QMainWindow>
    #include "form1.h"

    namespace Ui {
    class mainform;
    }

    class mainform : public QMainWindow
    {
    Q_OBJECT

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

    public slots:
    void update_label(const QString &string);

    private:
    Ui::mainform *ui;
    };

    #endif // MAINFORM_H
    mainform.cpp
    #include "mainform.h"
    #include "ui_mainform.h"

    mainform::mainform(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mainform)
    {
    ui->setupUi(this);
    connect(form1, SIGNAL(transfertCompleted(QString)), this, SLOT(update_label(QString)))
    }

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

    void mainform::update_label(const QString &string)
    {
    ui->label->clear();
    ui->label->setText(string);
    }
    form1.h
    #ifndef FORM1_H
    #define FORM1_H

    #include <QDialog>

    namespace Ui {
    class form1;
    }

    class form1 : public QDialog
    {
    Q_OBJECT

    signals:
    void transfertCompleted(QString);
    public:
    explicit form1(QWidget *parent = 0);
    ~form1();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::form1 *ui;
    };

    #endif // FORM1_H
    form1.cpp
    #include "form1.h"
    #include "ui_form1.h"

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

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

    void form1:n_pushButton_clicked()
    {
    //write data
    //i want when click it send data and also send text to mainform
    emit(transfertCompleted("transfer complete");
    }

  10. #10
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: button clicked to set text in another form

    Some code is missing, where form1 is created and shown? I think you should move the line
    Qt Code:
    1. connect(form1, SIGNAL(transfertCompleted(QString)), this, SLOT(update_label(QString)))
    To copy to clipboard, switch view to plain text mode 
    just after you create the object, and not on the mainwindow constructor.

    This is my mainwindow.c code and it works, other files are identitcals too yours at the first look.
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <form1.h>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(openWindow()));
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::openWindow()
    20. {
    21. Form1 *form = new Form1(this);
    22. connect(form, SIGNAL(transfertCompleted(QString)), this, SLOT(updateLabel(QString)));
    23. form->show();
    24. }
    25.  
    26. void MainWindow::updateLabel(QString text)
    27. {
    28. ui->label->clear();
    29. ui->label->setText(text);
    30. }
    To copy to clipboard, switch view to plain text mode 

    Note : the code tag is not the one you used ;-)

  11. The following user says thank you to nix for this useful post:

    vanduongbk (20th June 2013)

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

    Default Re: button clicked to set text in another form

    thank you
    but my pushbutton is in form1 ,no mainwindow
    i only have qlabel with a name is lable in mainwindow
    can not use ui->pushButton in mainwindow

Similar Threads

  1. How to get rid of red outline on button last clicked?
    By briankeeney in forum Qt Programming
    Replies: 3
    Last Post: 23rd January 2013, 17:59
  2. Replies: 3
    Last Post: 11th June 2012, 15:21
  3. How to checked if button is clicked
    By stbb24 in forum Newbie
    Replies: 3
    Last Post: 5th June 2012, 06:04
  4. Replies: 10
    Last Post: 6th July 2008, 09:46
  5. Replies: 3
    Last Post: 13th May 2007, 20:55

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
  •  
Qt is a trademark of The Qt Company.