Results 1 to 3 of 3

Thread: Change mainwindow ui label text using another class

  1. #1
    Join Date
    Apr 2020
    Posts
    4
    Qt products
    Qt5

    Default Change mainwindow ui label text using another class

    Hi,

    I have a problem, i created a mainwindow UI that has pushbutton and label "text" like in this picture https://prnt.sc/soz9tm
    and Rain ui that has pushbutton like in this picture https://prnt.sc/sozbaq

    When i compile program, first it opens mainwindow ui and when i clik on pushbutton it opens rain ui.
    I want when i click on rain ui's pushbutton to set mainwindow's label text to "It rains",
    but i don't know how to access mainwindow's label with Rain's class.

    here are my codes and what i tried

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "rain.h"
    6.  
    7. QT_BEGIN_NAMESPACE
    8. namespace Ui { class MainWindow; }
    9. QT_END_NAMESPACE
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. MainWindow(QWidget *parent = nullptr);
    17. ~MainWindow();
    18.  
    19. private slots:
    20. void on_pushButton_clicked();
    21.  
    22. public:
    23. Ui::MainWindow *ui;
    24. };
    25. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent)
    6. , ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16.  
    17. void MainWindow::on_pushButton_clicked()
    18. {
    19. this->hide();
    20. rain *test = new rain();
    21. test->show();
    22. }
    To copy to clipboard, switch view to plain text mode 
    rain.h
    Qt Code:
    1. #ifndef RAIN_H
    2. #define RAIN_H
    3.  
    4. #include <QWidget>
    5. #include "mainwindow.h"
    6.  
    7. namespace Ui {
    8. class rain;
    9. }
    10.  
    11. class rain : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit rain(QWidget *parent = nullptr);
    17. ~rain();
    18.  
    19. private slots:
    20. void on_pushButton_clicked();
    21.  
    22. private:
    23. Ui::rain *ui;
    24. };
    25.  
    26. #endif // RAIN_H
    To copy to clipboard, switch view to plain text mode 
    rain.cpp
    Qt Code:
    1. #include "rain.h"
    2. #include "ui_rain.h"
    3. #include "mainwindow.h"
    4.  
    5. rain::rain(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::rain)
    8. {
    9. ui->setupUi(this);
    10.  
    11. }
    12.  
    13. rain::~rain()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void rain::on_pushButton_clicked()
    19. {
    20. this->hide();
    21. MainWindow *ma = new MainWindow(this);
    22.  
    23. ma->show();
    24. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Change mainwindow ui label text using another class

    but i don't know how to access mainwindow's label with Rain's class.
    You don't. Implement a signal for your rain class that gets emitted when the user clicks the button. In your MainWindow class, implement a slot to receive this signal. When you create your rain class in MainWindow:: on_pushbutton_clicked(), connect rain's signal to MainWindow's slot.

    If you don't understand how to implement custom signals and slots, read the documentation.

    Your code as written has a memory leak: Each time your clicked slot is executed, you create a new rain instance. This instance is never destroyed, so it is a memory leak. What you should do instead is make a "rain *" member variable in your MainWindow class, create the rain instance in the MainWindow constructor, and connect the signal and slot. In the clicked slot, simply call show() on this rain instance.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2020
    Posts
    4
    Qt products
    Qt5

    Default Re: Change mainwindow ui label text using another class

    Took some time to learn signal and slot implementation, and i did everything as you said
    Thank you, it works perfectly

Similar Threads

  1. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 16:15
  2. Replies: 4
    Last Post: 23rd August 2011, 21:53
  3. Replies: 6
    Last Post: 21st August 2010, 21:09
  4. How to change Label text colour in Qt designer
    By sudhansu in forum Qt Programming
    Replies: 2
    Last Post: 6th January 2010, 12:31
  5. How to change the Label text
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 15th September 2009, 12:57

Tags for this Thread

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.