Updating text on ui.label from another Widget?
-
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
-
Hi,
Give that widget a signal that you will emit when the button is clicked and connect it to your label setText slot.
-
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(); }
-
Added the signal, how does the connect look like?
form.h signals: void applyChanges();
form.cpp void Form::submitButtonClick() { emit applyChanges(); this->close(); }
@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.