Results 1 to 18 of 18

Thread: button clicked to set text in another form

  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

  13. #12
    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

    No, i'm in the same case. The pushbutton in the mainwindow just shows the second window (form1). The second window has its own pushbutton just like you.
    How does you second window is created and shown?

    Complete code :
    mainwindow.c
    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 

    form1.c
    Qt Code:
    1. #include "form1.h"
    2. #include "ui_form1.h"
    3.  
    4. Form1::Form1(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Form1)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Form1::~Form1()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Form1::on_pushButton_clicked()
    17. {
    18. //write data
    19. //i want when click it send data and also send text to mainform
    20. emit(transfertCompleted("transfer complete"));
    21. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef FORM1_H
    2. #define FORM1_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class Form1;
    8. }
    9.  
    10. class Form1 : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit Form1(QWidget *parent = 0);
    16. ~Form1();
    17. signals:
    18. void transfertCompleted(QString);
    19.  
    20. private:
    21. Ui::Form1 *ui;
    22. private slots:
    23. void on_pushButton_clicked();
    24. };
    25.  
    26. #endif // FORM1_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. Ui::MainWindow *ui;
    20. private slots:
    21. void openWindow();
    22. void updateLabel(QString text);
    23.  
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

  14. #13
    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 ,i compiler and have error as:
    mainscreen.cpp:180: error: C2039: 'pushButton' : is not a member of 'Ui::MainScreen'
    how to resolve it

  15. #14
    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

    Im my case I use a mainwindow with a pushbutton. A press on that button creates the form1 and shows it. Another click in pushbutton from the form1 change the content of the label in mainwindow. I supposed that it was the way you did it, but it may be not.
    Maybe you do not need this behavior.

    I still don't get how you create your form1, the problem is probably around here. How does your mainwindow know about form1 in you code???
    Are you creating both windows in your main function? In that case move the connect between both windows to the main fonction too.

  16. #15
    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 ,
    i creat form1 only with pushButton to send data and i want to do when i click pushbutton in form1 ,it also simultaneously display with content a text that "transfer complete" on Qlabel in mainwindow
    plz help me in a demo code

  17. #16
    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

    This is quick & dirty, just demo, it is how i understand your problem.
    testqt.tar.gz

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

    vanduongbk (22nd June 2013)

  19. #17
    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 nix
    your code is run ok
    but i want to ask how i do not need show form1 ,only click in form1 then back to mainform and a text on mainform is changed without show form1
    thank

  20. #18
    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

    Quote Originally Posted by vanduongbk View Post
    but i want to ask how i do not need show form1 ,only click in form1 then back to mainform and a text on mainform is changed without show form1
    I'm sorry but I don't understand your problem.
    You want click on form1, so it have to be shown. If you just want form1 to not be visible after you have pressed the button and change the text into the mainwindow, just connect the clicked() signal of your form1 button to the close() slot of form1.
    But this is probably not what you meant, in that case please try to give a complete description of what you are trying to do, especially when windows are created, shown and hide.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.