Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Updating text on ui.label from another Widget?

Updating text on ui.label from another Widget?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 712 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BoGut
    wrote on last edited by
    #1

    Hello, I'm about to start a new personal project and have one question still lingering if you pros don't mind helping me answer. Plan is to have a mainWindow with just a QLabel in it and a QPushButton that will open another widget. This other widget will have a lineEdit and submit button. How do I go about taking the lineEdit text and having it show on the mainWindow label after pressing the submit button? I'll start coding it now. Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Give that widget a signal that you will emit when the button is clicked and connect it to your label setText slot.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • B Offline
        B Offline
        BoGut
        wrote on last edited by
        #3

        Perfect, thanks for the quick reply. May I show you what I have right now?

        form.h
        
        #ifndef FORM_H
        #define FORM_H
        
        #include <QWidget>
        
        namespace Ui {
        class Form;
        }
        
        class Form : public QWidget
        {
            Q_OBJECT
        
        public:
            explicit Form(QWidget *parent = nullptr);
            ~Form();
        
        private:
            Ui::Form *ui;
            void submitButtonClick();
        };
        
        #endif // FORM_H
        
        form.cpp
        
        #include "form.h"
        #include "ui_form.h"
        
        Form::Form(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Form)
        {
            ui->setupUi(this);
        
            connect(ui->submitButton, &QPushButton::clicked, this, &Form::submitButtonClick);
        }
        
        Form::~Form()
        {
            delete ui;
        }
        
        void Form::submitButtonClick()
        {
            this->close();
        }
        
        mainwindow.h
        
        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        
        QT_BEGIN_NAMESPACE
        namespace Ui { class MainWindow; }
        QT_END_NAMESPACE
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
        private:
            Ui::MainWindow *ui;
            void pushButtonClick();
        };
        #endif // MAINWINDOW_H
        
        mainwindow.cpp
        
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "form.h"
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::pushButtonClick);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::pushButtonClick()
        {
            Form *form = new Form();
            form->show();
        }
        
        1 Reply Last reply
        0
        • B Offline
          B Offline
          BoGut
          wrote on last edited by BoGut
          #4

          Added the signal, how does the connect look like?

          form.h
          
          signals:
              void applyChanges();
          
          form.cpp
          
          void Form::submitButtonClick()
          {
              emit applyChanges();
              this->close();
          }
          
          mrjjM 1 Reply Last reply
          0
          • B BoGut

            Added the signal, how does the connect look like?

            form.h
            
            signals:
                void applyChanges();
            
            form.cpp
            
            void Form::submitButtonClick()
            {
                emit applyChanges();
                this->close();
            }
            
            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @BoGut
            Hi
            Like the normal connect.

            but i think you want like

            signals:
            void applyChanges(const QString &Text);

            so we can emit the text with the signal.

            we need to do the connect where you create the Form object

            void MainWindow::pushButtonClick()
            {
                Form *form = new Form();
                form->show();
                connect(  form,   &Form::applyChanges, ui->thelabel,  &QLabel::setText  );
               // From what object, ClassName::SignalName, to what Object, ClassName::SlotName)
            }
            

            and we need to emit the text also

            void Form::submitButtonClick()
            {
                emit applyChanges( ui->LineEditName->text());
                this->close();
            }
            

            Something like this. Its just brained compiled so might have some errors but thats the overall idea.
            The Form that has the lineEdit emits the text to MainWin where we connected the labels setText.

            -May I show you what I have right now?
            Yes ! please always show the code you have and any errors as then
            its much easier to help. We will help with almost anything if the question
            is carefull written and effort shown.

            1 Reply Last reply
            2
            • B Offline
              B Offline
              BoGut
              wrote on last edited by
              #6

              Thank you so much, it works! Read the signals and slots QT docs and got maybe 75% of what they were talking about :-) You guys are amazing, virtual beers on me!

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved