Better way to close old widget ui and open new widget ui
-
Hello,
I have a push button and I want to open new ui that has a lineedit when I click that button. I managed it ,but it feels wrong because it is C language approach. Basically I added new widget class header file to original widget.h file. Then I created static object in push button's slot function and called object's show() function. When I click push button I see a new ui that has lineedit.
- Is there a better way to achieve this ?
- How can I get original widget class' member (QString text = "hello") from new widget class so that I can set text lineedit with that member ?
widget.h:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QString> #include "newform.h" QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_pushButton_clicked(); private: Ui::Widget *ui; public: QString text = "hello"; }; #endif // WIDGET_H
widget.cpp:
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } void Widget::on_pushButton_clicked() { static newForm nf; this->hide(); nf.show(); }
I have newform.h and newform.cpp files. I didn't change them. I will be very appreciated if you could help me.
-
@TokaraForest
Consider using a QStackedWidget to swap between what widget shown.I don't know what you are doing with
static newForm nf
, but don't do it, and don't usestatic
. -
@TokaraForest
If you goal is to get a single text input, have a look at QInputDialog Class -
@TokaraForest said in Better way to close old widget ui and open new widget ui:
How can I get original widget class' member (QString text = "hello") from new widget class so that I can set text lineedit with that member ?
// pass text in c'tor and set it to your lineEdit in newForm.cpp newForm nf(text); // better show the window first before hiding the other nf.show(); this->hide();
-
Thank you all ! I think @JonB 's suggestion will do the job since my goal is more comprehensive.
-