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. how to modify mainform.ui components from other form, and vice versa
Forum Updated to NodeBB v4.3 + New Features

how to modify mainform.ui components from other form, and vice versa

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 1.5k Views 1 Watching
  • 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.
  • mrjjM mrjj

    @Cuban-Akira
    Hi
    Instead of surfacing all the widgets from form2, you would add access slot function to be triggered from out
    side. the slot it self , will then ask the UI elements directly.

    So for Form2, you would add
    public slots:
    void HideSomething() {
    ui->button->hide();
    }

    and from form1, you connect to buttons clicked() to HideSomething()

    that way, form1 do not have know every widget that form2 has -
    but can use an interface to carry out
    any needed call to the actual widgets in form2.
    This a better design versus making UI public.

    this example seems a bit silly to hide a button and not its something you would need very often.
    However, imagine you had a form2, with a lineEdit.
    From form1, yo want a persons name to be put in that lineEdit.
    Then you add a new slot called
    void setName(QString name) {
    ui->lineEdit->setText(name);
    }

    and use that from form1 to make it display name.
    Say you later found out, you want to show name in a QLabel.
    All you need is to change setName slot to use this label, but
    rest of the code will work unmodified as form1, only knows
    setName, not that it was an lineEdit in form2.

    So this make application far more robust as small changes wont
    change how rest of the program uses form2.
    Rest of program , only knows the interface for displaying data you defined.
    Not how its was build internally.
    I hope this makes sense :)

    C Offline
    C Offline
    Cuban Akira
    wrote on last edited by
    #5

    @mrjj
    i understand your point, but still have errors on my code
    after create the signals and slot, i dont know how to write the 3rd argument in connect funtion

    connect(ui->pushButton, SIGNAL (click()) , information , SLOT(hideEditar()));

    the 3rd parameter (the name of the 2nd form, "information") is wrong,
    if i create> information *info= new information();, and putt "info" as the 3rd argument of connect the program launch an exeption, and stop

    mrjjM 1 Reply Last reply
    0
    • C Cuban Akira

      @mrjj
      i understand your point, but still have errors on my code
      after create the signals and slot, i dont know how to write the 3rd argument in connect funtion

      connect(ui->pushButton, SIGNAL (click()) , information , SLOT(hideEditar()));

      the 3rd parameter (the name of the 2nd form, "information") is wrong,
      if i create> information *info= new information();, and putt "info" as the 3rd argument of connect the program launch an exeption, and stop

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #6

      @Cuban-Akira
      hi
      "information" needs to be a pointer to a class instance. not classname.
      So if forms Type/Class is Information
      then
      information *info= new information(this); // make instance of information form
      connect(ui->pushButton, SIGNAL (click()) , info, SLOT(hideEditar()));

      C 1 Reply Last reply
      0
      • mrjjM mrjj

        @Cuban-Akira
        hi
        "information" needs to be a pointer to a class instance. not classname.
        So if forms Type/Class is Information
        then
        information *info= new information(this); // make instance of information form
        connect(ui->pushButton, SIGNAL (click()) , info, SLOT(hideEditar()));

        C Offline
        C Offline
        Cuban Akira
        wrote on last edited by Cuban Akira
        #7

        @mrjj
        thanks a lot men, but i dont know what happens
        when i put
        informacion *info = new informacion(this);

        connect(ui->pushButton, SIGNAL (click()) , info , SLOT(hideEditar()));

        the app stop, says, > crashed

        mrjjM 1 Reply Last reply
        0
        • C Cuban Akira

          @mrjj
          thanks a lot men, but i dont know what happens
          when i put
          informacion *info = new informacion(this);

          connect(ui->pushButton, SIGNAL (click()) , info , SLOT(hideEditar()));

          the app stop, says, > crashed

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @Cuban-Akira
          Hi
          Sounds like it crashes.
          Can you show the actual code ?
          The connect seems fine so its just something else.

          can it be in hideEditar it crashes ?

          Can it be you fun this code BEFORE the setupUI() in form1 ?

          C 1 Reply Last reply
          1
          • mrjjM mrjj

            @Cuban-Akira
            Hi
            Sounds like it crashes.
            Can you show the actual code ?
            The connect seems fine so its just something else.

            can it be in hideEditar it crashes ?

            Can it be you fun this code BEFORE the setupUI() in form1 ?

            C Offline
            C Offline
            Cuban Akira
            wrote on last edited by
            #9

            @mrjj

            i think the mistake is in " informacion *info = new informacion(this);"
            cause "this" reference to MainWindow class, not informacion class

            this is the constructor of MainWindow.cpp

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {

            ui->setupUi(this);
            

            QFontDatabase::addApplicationFont(":/fonts/imagenes/usuziv2.ttf");

             Aeropuerto *obj = new Aeropuerto();
             Avion *obj2 = new Avion();
            
             informacion *info = new informacion(this);
             connect(ui->pushButton, SIGNAL (click()) , info , SLOT(hideEditar()));
            
             obj->crearFlota();
             obj->flotaAviones.sort(obj2->mayorNombre);
            
             list<Avion>::iterator it;
             int i = 0;
            for(it=obj->flotaAviones.begin(); it!=obj->flotaAviones.end();  it++)
            {
            ui->comboBox->insertItem(i, (*it).getAerolinea());
            

            i++;
            }

             obj->xt = obj->flotaAviones.begin();
             obj->yt = obj->xt;
            

            ui->textBrowser->hide();
            ui->mainToolBar->hide();
            ui->statusBar->hide();
            }

            //this is informacion.h

            #ifndef INFORMACION_H
            #define INFORMACION_H

            #include <QDialog>
            #include <list>

            namespace Ui {
            class informacion;
            }

            class informacion : public QDialog
            {
            Q_OBJECT

            public:
            explicit informacion(QWidget *parent = 0);

            ~informacion();
            

            private slots:
            void on_pushButton_clicked();

            void on_pushButton_2_clicked();
            
            void on_editar_clicked();
            
            void on_ok_clicked();
            

            public slots:
            void hideEditar();

            private:
            Ui::informacion *ui;

            };

            //and this is definition of hideEditar() on information.cpp

            void informacion::hideEditar()
            {
            ui->editar->hide();
            }

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #10

              Hi
              Can you show implementation of
              informacion(QWidget *parent = 0);
              I suspect the UI is not newed and it crashes with
              ui->editar->hide(); as ui might not be setup via the normal
              setupUI()
              So check if you have code that initilize
              Ui::informacion *ui;
              like
              ui(new Ui::informacion)

              Note that
              informacion *info = new informacion(this);"
              "this" is used as parent as param name of the constructor suggests.

              C 1 Reply Last reply
              1
              • mrjjM mrjj

                Hi
                Can you show implementation of
                informacion(QWidget *parent = 0);
                I suspect the UI is not newed and it crashes with
                ui->editar->hide(); as ui might not be setup via the normal
                setupUI()
                So check if you have code that initilize
                Ui::informacion *ui;
                like
                ui(new Ui::informacion)

                Note that
                informacion *info = new informacion(this);"
                "this" is used as parent as param name of the constructor suggests.

                C Offline
                C Offline
                Cuban Akira
                wrote on last edited by
                #11

                @mrjj said in how to modify mainform.ui components from other form, and vice versa:

                spect

                //information.cpp constructor

                informacion::informacion(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::informacion)
                {
                ui->setupUi(this);

                Aeropuerto *obj = new Aeropuerto();
                list<Avion>::iterator it;

                //visual
                ui->aerolineaLE->hide();
                ui->aerolineaEditarLabel->hide();
                ui->codigoLE->hide();
                ui->asientosLE->hide();
                ui->equipajeLE->hide();
                ui->pilotoLE->hide();
                ui->editarHeadLine->hide();

                ui->ok->hide();

                obj->xt=obj->flotaAviones.begin();

                ui->aerolinealabel->setText((*obj->xt).getAerolinea());
                ui->asientosRed->setText(QString::number((*obj->xt).getCantidadAsientos()));
                ui->asientoslabel->setText(QString::number((*obj->xt).getCantidadAsientos()));
                ui->equipajelabel->setText(QString::number((*obj->xt).getCapacidadEquipajeRestante()));
                ui->codigolabel->setText((*obj->xt).getCodigo());
                ui->pilotolabel->setText((*obj->xt).getNombrePiloto());
                ui->equipajeNRed->setText(QString::number((*obj->xt).getCapacidadEquipajeRestante()));
                ui->aerolineaRed->setText((*obj->xt).getAerolinea());

                //mostrando
                ui->aerolinealabel->show();
                ui->codigolabel->show();
                ui->asientoslabel->show();
                ui->equipajelabel->show();
                ui->pilotolabel->show();
                ui->aerolinealabel->show();
                ui->aerolineaRed->show();
                ui->equipajeNRed->show();
                ui->pushButton->show();
                ui->pushButton_2->show();
                ui->editar->show();

                //numeros rojos
                if((*obj->xt).getCantidadAsientos()==0)
                {
                ui->aerolinealabel->hide();
                ui->label_4->hide();
                ui->asientoslabel->hide();
                }
                if((*obj->xt).getCapacidadEquipajeRestante()==0)
                {
                ui->aerolinealabel->hide();
                ui->label_3->hide();
                ui->equipajelabel->hide();
                }

                }

                mrjjM 1 Reply Last reply
                0
                • C Cuban Akira

                  @mrjj said in how to modify mainform.ui components from other form, and vice versa:

                  spect

                  //information.cpp constructor

                  informacion::informacion(QWidget *parent) :
                  QDialog(parent),
                  ui(new Ui::informacion)
                  {
                  ui->setupUi(this);

                  Aeropuerto *obj = new Aeropuerto();
                  list<Avion>::iterator it;

                  //visual
                  ui->aerolineaLE->hide();
                  ui->aerolineaEditarLabel->hide();
                  ui->codigoLE->hide();
                  ui->asientosLE->hide();
                  ui->equipajeLE->hide();
                  ui->pilotoLE->hide();
                  ui->editarHeadLine->hide();

                  ui->ok->hide();

                  obj->xt=obj->flotaAviones.begin();

                  ui->aerolinealabel->setText((*obj->xt).getAerolinea());
                  ui->asientosRed->setText(QString::number((*obj->xt).getCantidadAsientos()));
                  ui->asientoslabel->setText(QString::number((*obj->xt).getCantidadAsientos()));
                  ui->equipajelabel->setText(QString::number((*obj->xt).getCapacidadEquipajeRestante()));
                  ui->codigolabel->setText((*obj->xt).getCodigo());
                  ui->pilotolabel->setText((*obj->xt).getNombrePiloto());
                  ui->equipajeNRed->setText(QString::number((*obj->xt).getCapacidadEquipajeRestante()));
                  ui->aerolineaRed->setText((*obj->xt).getAerolinea());

                  //mostrando
                  ui->aerolinealabel->show();
                  ui->codigolabel->show();
                  ui->asientoslabel->show();
                  ui->equipajelabel->show();
                  ui->pilotolabel->show();
                  ui->aerolinealabel->show();
                  ui->aerolineaRed->show();
                  ui->equipajeNRed->show();
                  ui->pushButton->show();
                  ui->pushButton_2->show();
                  ui->editar->show();

                  //numeros rojos
                  if((*obj->xt).getCantidadAsientos()==0)
                  {
                  ui->aerolinealabel->hide();
                  ui->label_4->hide();
                  ui->asientoslabel->hide();
                  }
                  if((*obj->xt).getCapacidadEquipajeRestante()==0)
                  {
                  ui->aerolinealabel->hide();
                  ui->label_3->hide();
                  ui->equipajelabel->hide();
                  }

                  }

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  @Cuban-Akira
                  Ok, that looks all right as far as i can see.
                  You have to find out what line it crash on so its time to use the debugger and
                  single step through MainWindow and see what makes it crash.
                  Seems to be fine with the connect . So i assume its something else.

                  C 2 Replies Last reply
                  0
                  • mrjjM mrjj

                    @Cuban-Akira
                    Ok, that looks all right as far as i can see.
                    You have to find out what line it crash on so its time to use the debugger and
                    single step through MainWindow and see what makes it crash.
                    Seems to be fine with the connect . So i assume its something else.

                    C Offline
                    C Offline
                    Cuban Akira
                    wrote on last edited by
                    #13

                    @mrjj said in how to modify mainform.ui components from other form, and vice versa:
                    when i comment
                    /* informacion info = new informacion(this);
                    connect(ui->pushButton, SIGNAL (click()) , info , SLOT(hideEditar()));
                    /

                    debuging i found why crashes, and fix it, i testing right now

                    1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @Cuban-Akira
                      Ok, that looks all right as far as i can see.
                      You have to find out what line it crash on so its time to use the debugger and
                      single step through MainWindow and see what makes it crash.
                      Seems to be fine with the connect . So i assume its something else.

                      C Offline
                      C Offline
                      Cuban Akira
                      wrote on last edited by
                      #14

                      @mrjj
                      i test the signal slot system in my app, and i have gaps, thing i dont understand yet, i solved all the remaining problem and task taking other ways,
                      i have this questions unclear:
                      1 - if i want to connect a button.clicked() signal to a x slot, i have to declare it(the signal) in the class.h and class.cpp or the moc file create it ???
                      2 - in the funtion connect, i use 4 arguments, (object, signal, recObject, slot ), if the signal es from a button, the object may be the formclass or the button
                      3 - i see a lot examples with the word "emit", i have to use always?, all the bodys of slots funtion may have "emit"?
                      4 - how to create a signal, ?? cause i cannnot implemet a body in the class.cpp, just declare it in the class.h

                      mrjjM 1 Reply Last reply
                      1
                      • C Cuban Akira

                        @mrjj
                        i test the signal slot system in my app, and i have gaps, thing i dont understand yet, i solved all the remaining problem and task taking other ways,
                        i have this questions unclear:
                        1 - if i want to connect a button.clicked() signal to a x slot, i have to declare it(the signal) in the class.h and class.cpp or the moc file create it ???
                        2 - in the funtion connect, i use 4 arguments, (object, signal, recObject, slot ), if the signal es from a button, the object may be the formclass or the button
                        3 - i see a lot examples with the word "emit", i have to use always?, all the bodys of slots funtion may have "emit"?
                        4 - how to create a signal, ?? cause i cannnot implemet a body in the class.cpp, just declare it in the class.h

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #15

                        Hi
                        1:
                        Always in the .cpp and .h for the slots.
                        the moc files are generated and will override any changes you make for them.
                        Notice that a slot always have its header in .h and the body in .cpp
                        A custom signal on the other hand, only has header in .h as it has no body.
                        Terms used:
                        header = the signature of the function/method/slot like
                        void SomeSlot(int param);
                        Body = the actual implementation of the slot
                        void Classsname::SomeSlot(int param) {
                        ---code--
                        }
                        on the other hand a signal would just be
                        void SomeSignal(int param); in .h and nothing in .cpp.

                        2:
                        You may send a signal to any other class that inherit QObject and has Q_OBJECT macro and the slot that matches
                        class Someclass: public QObject {
                        Q_OBJECT

                        3:
                        emit is used to send the signal.
                        So if you create new signals, its how you "send" them
                        emit MySignal()

                        4:
                        a signal is just a method header/signature and listed under signals:
                        signals:
                        void MySignal(); // your custom signal.

                        Did you read
                        http://doc.qt.io/qt-5/signalsandslots.html
                        ?

                        1 Reply Last reply
                        1

                        • Login

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