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
    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 

  2. #2
    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

  3. #3
    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.

  4. #4
    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

  5. #5
    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

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

    vanduongbk (22nd June 2013)

  7. #6
    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

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