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. Better way to close old widget ui and open new widget ui

Better way to close old widget ui and open new widget ui

Scheduled Pinned Locked Moved Solved General and Desktop
ui formshowhide
5 Posts 4 Posters 545 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.
  • T Offline
    T Offline
    TokaraForest
    wrote on 5 Jun 2023, 11:49 last edited by
    #1

    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.

    1. Is there a better way to achieve this ?
    2. 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.

    J M P 3 Replies Last reply 5 Jun 2023, 11:54
    0
    • T TokaraForest
      5 Jun 2023, 11:49

      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.

      1. Is there a better way to achieve this ?
      2. 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.

      J Offline
      J Offline
      JonB
      wrote on 5 Jun 2023, 11:54 last edited by
      #2

      @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 use static.

      1 Reply Last reply
      1
      • T TokaraForest
        5 Jun 2023, 11:49

        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.

        1. Is there a better way to achieve this ?
        2. 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.

        M Offline
        M Offline
        mpergand
        wrote on 5 Jun 2023, 12:18 last edited by
        #3

        @TokaraForest
        If you goal is to get a single text input, have a look at QInputDialog Class

        1 Reply Last reply
        3
        • T TokaraForest
          5 Jun 2023, 11:49

          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.

          1. Is there a better way to achieve this ?
          2. 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.

          P Online
          P Online
          Pl45m4
          wrote on 5 Jun 2023, 12:23 last edited by
          #4

          @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();
          
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TokaraForest
            wrote on 5 Jun 2023, 12:37 last edited by
            #5

            Thank you all ! I think @JonB 's suggestion will do the job since my goal is more comprehensive.

            1 Reply Last reply
            0
            • T TokaraForest has marked this topic as solved on 5 Jun 2023, 12:37

            2/5

            5 Jun 2023, 11:54

            • Login

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