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. Form constructors
Forum Update on Monday, May 27th 2025

Form constructors

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.5k 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.
  • A Offline
    A Offline
    An other french
    wrote on last edited by
    #1

    Hye,
    I tried to implement a form class with 2 constructors.

    Form::Form(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Form)
    {
        ui->setupUi(this);
    }
    
    Form::Form(Qstring & tmp, QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Form)
    {
        ui->setupUi(this);
        ui->textEdit->setText(tmp);
    }
    
    
    Form::~Form()
    {
        delete ui;
    }
    
    void Form::on_pushButton_clicked()
    {
        this->close();
    }
    
    

    I found any example with 2 constructors.
    I'm certain that i lost something somewhere....

    I want to open this form at the beginning in the main.cpp and open it when i clicked on a button on the main form.

    Ths for reading.

    1 Reply Last reply
    0
    • webzoidW Offline
      webzoidW Offline
      webzoid
      wrote on last edited by webzoid
      #2

      Why don't you just call the default Form constructor in your overloaded constructor:

      Form::Form(QString &tmp, QWidget *parent) :
          Form(parent) 
      {
          ui->textEdit->setText(tmp);
      }
      

      If you want to show the form, you can use the exec function - however, you need to change your base class to a QDialog:

      Form myForm;
      myForm.exec();
      
      1 Reply Last reply
      0
      • C Offline
        C Offline
        Charlie_Hdz
        wrote on last edited by
        #3

        If you want to present a "Widget" :

        In the main.cpp:

        SEView w(&IK);
         w.show();
        

        If you want to trigger the widgets showing with a click, I can use signals and slots:

        QPushButton* button=new QPushButton;
        Form* form= form("tmp",this);
        connect(buitton,SIGNAL(clicked()),form,SLOT(show()));
        

        The documentation is also helpful: http://doc.qt.io/qt-5/qwidget.html#show

        Kind Regards,
        Enrique Hernandez
        gearstech.com.mx
        chernandez@gearstech.com.mx

        1 Reply Last reply
        0
        • A Offline
          A Offline
          An other french
          wrote on last edited by An other french
          #4

          I think that the form was destroyed when the function who constructs it , was ended.

          void MainWindow::on_pushButton_3_clicked() 
          {
              QString Txt = "ca fonctionne";
              Form viewNumber(Txt,0);
              viewNumber.show();
          }
          
          1 Reply Last reply
          0
          • A Offline
            A Offline
            An other french
            wrote on last edited by An other french
            #5

            In fact, i need to pop up this window in certains conditions.
            The solution to connect events together , seem to be difficult.
            The automation isn't able to allow some logical purposes.

            for example, if good result, show a smily else show a red devil.

            In my case, when the com3 has some good setting, shows the form....

            I'm clear, am i?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              An other french
              wrote on last edited by
              #6

              Do you think it 's faithful to put the form declaration as a pointor in the Private class of MainForm.h?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                An other french
                wrote on last edited by
                #7

                So, it seems to workwith a private pointor in form.h.

                void MainWindow::on_pushButton_3_clicked() 
                {
                    QString Txt = "ca fonctionne";
                    viewNumber= new Form(Titre);
                    viewNumber->show();
                }
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  You are now leaking widgets each time you call that method.

                  What exactly do you want to do with it ? Is it a dialog ?

                  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
                  0

                  • Login

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