how to modify objects in a form, from another form, QT creator

0

How can I modify the components of a mainform.ui (Qlabels, comboBox, etc.) from another form, and vice versa? ex: 1-mainform.ui has a button (called "button1"), when I click on button1 I want to disable a button located in otherform.ui (called "button2") 2- when I click on the otherform.ui 2 button, I want to delete a comboBox, located in mainform.ui

In example # 1: I do not know how to reference otherform.ui, using the form > otherform obj; obj.ui.button2.disabled (); The compiler shows me an error!.

in the ej n. ° 2: in otherform.cpp I refer to mainform.ui, creating a type of object mainform, (mainform obj;), and then obj.ui.comboBox.clear ();, when I run the application, an error appears > mainform * ui is private, so I go to mainform.h and make a public ui, then no errors happen, but nothing happens either.

    
asked by HOLDTHEDOOR 26.10.2018 в 15:29
source

2 answers

0

I do not know if this is the doubt, but with the SIGNALS / SLOTS of Qt you should be able to solve that question. It is about creating SLOTS that relate the widget and perform the function you are looking for. An example (without forms): class widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
class QPushButton;
class QHBoxLayout;

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

public slots:
    void DesactivarBoton();

private:    
    QHBoxLayout* lienzo;
    QPushButton* bt1, *bt2;
};

#endif // WIDGET_H

class widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QHBoxLayout>

Widget::Widget(QWidget *parent) : QWidget(parent)
{
    lienzo = new QHBoxLayout(this);
    bt1 = new QPushButton("Boton 1");
    bt2 = new QPushButton("Boton 2");
    lienzo->addWidget(bt1);
    lienzo->addWidget(bt2);
    connect (bt1,SIGNAL(clicked(bool)),this,SLOT(DesactivarBoton()));
}

Widget::~Widget()
{
}

void Widget::DesactivarBoton()
{
    if (bt2->isEnabled())
    {
        bt2->setEnabled(false);
    }
    else
    {
        bt2->setEnabled(true);
    }
}

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

If the widgets are created in forms, you will have to create the slot (and maybe the signal) in the class that defines them and make the SIGNAL / SLOT connection in the widget that hosts them.

    
answered by 28.10.2018 в 07:06
0

The first thing you have to do is capture the signal clicked() of button1 . You can do this directly from the graphical environment or using a code similar to this:

class MainForm
{
private slots:
  void button1_clicked();
};

MinForm::MainForm()
{
  // ...
  connect(button1,SIGNAL(clicked()),SLOT(button1_clicked()));
}

void MainForm::button1_clicked()
{
}

With this code you have already gotten every time you click in button1 you call the function button1_click .

From this point, the resolution of the problem can be done in different ways:

1. In a traditional way

In this case you need that MainForm have, in some way, a pointer to OtherForm :

class MainForm
{
private:
  OtherForm* otherForm;
};

Once that is achieved, the way to disable the button will depend on whether the button2 object is public or private. If it is public, deactivation can be done directly and, if not, we will have to prepare a function that allows us to modify the activation status of the button:

void MainForm::button1_clicked()
{
  // Si button2 es public
  otherForm->button2->setEnabled(false);

  // Si es private
  otherForm->setButton2Enabled(false);
}

2. With signals and slots

Another way to solve the problem is not to capture the signal in the main form but directly on the one we are going to act on.

A simple way would be to take advantage of the moment in which otherForm is created to link the signal clicked of button1 with this slot :

class OtherForm
{
public slots:
  void CambiarEstadoBoton()
  {
    button2->setEnabled(!button2->isEnabled());
  }
};

void MainForm::CrearOtherForm()
{
  OtherForm* otherForm = new OtherForm;
  connect(button1,SIGNAL(clicked()),
          otherForm,SLOT(CambiarEstadoBoton()));
}

Keep in mind that QPushButton does not have a slot that permutes the activation status. We have setEnabled , but to make it work we have to tell you if we want to enable or disable the button ... it does not work for us unless we want that button2 the MainForm button will be pressed. If this were the case we could save the function CambiarEstadoBoton and link the signal of button1 with the slot of button2 :

void MainForm::CrearOtherForm()
{
  OtherForm* otherForm = new OtherForm;
  connect(button1,SIGNAL(toggled(bool)),
          otherForm,SLOT(setEnabled(bool)));
}
    
answered by 31.10.2018 в 08:37