Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Updating text on ui.label from another Widget?

    General and Desktop
    3
    6
    158
    Loading More Posts
    • 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
      BoGut last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 1
        • B
          BoGut last edited by

          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 Reply Quote 0
          • B
            BoGut last edited by BoGut

            Added the signal, how does the connect look like?

            form.h
            
            signals:
                void applyChanges();
            
            form.cpp
            
            void Form::submitButtonClick()
            {
                emit applyChanges();
                this->close();
            }
            
            mrjj 1 Reply Last reply Reply Quote 0
            • mrjj
              mrjj Lifetime Qt Champion @BoGut last edited by mrjj

              @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 Reply Quote 2
              • B
                BoGut last edited by

                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 Reply Quote 1
                • First post
                  Last post