0

I would like to make some modifications of the main window from another file.

  • I created another ui file Form1Window (which open when a button is cliked in the MainWindow).
  • I want to call from the class Form1Window a function named test() of the MainWindow class

I succeed in calling function test() but I can't execute the whole content of the function (I can display a message but can't execute the part where I want to clear an edittext)

MainWindow.h

 #include "form1window.h"

        public slots:
            void nettoyer();

        private slots:
            void openFrom1();

        private:
            Ui::MainWindow *ui;
            From1Window *uiFrom1;

};

MainWindow.cpp

void MainWindow::openFrom1()
{
    uiFrom1 = new From1Window(this);
    uiFrom1->show();
}


void MainWindow::nettoyer(){

     QMessageBox msgBox;
     msgBox.setText("test");
     msgBox.setIcon(QMessageBox::Information);
     msgBox.setStandardButtons(QMessageBox::Ok);
     msgBox.exec();

     ui->auteur->clear();  
          //THIS LINE HAS NO EFFECT WHEN CALLED FROM THE OTHER CLASS
    }

form1window.cpp

#include "mainwindow.h"
#include "ui_form1window.h"

void From1Window::on_supprimer_clicked()
{

MainWindow *a=new MainWindow ();
a->test();

close();
}

I've read about the role of the pointer of MainWindow class (C++ /Qt Proper way to access ui from another class in qt //Edited) and I've also tried connect()

Thank for your help

Ben W
  • 23
  • 10

1 Answers1

0

//THIS LINE HAS NO EFFECT WHEN CALLED FROM THE OTHER CLASS

this->ui->auteur->clear();

The line will never executed unless you dismiss QMessageBox. This is because you triggered QMessageBox with exec() function. This function has its own event queue and does not return until finishes. You may set QMessageBox as modal and display it with show() method. In that case QMessageBox will not block execution of the the flow.

This problem can also happen with QDialog(s) if you display them with exec().

I provide you a simple two window signal/slot example:

main.cpp

#include "mainwindow.h"
#include "form.h"
#include <QApplication>

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

    return a.exec();
}

form.h

#ifndef FORM_H
#define FORM_H

#include <QWidget>
#include <QPushButton>

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = 0);
    ~Form();
    
private:
    QPushButton *pb;

};

#endif // FORM_H

form.cpp

#include "form.h"
#include "mainwindow.h"

#include <QDebug>

Form::Form(QWidget *parent) : QWidget(parent)
{
    MainWindow *mw = new MainWindow();
    pb = new QPushButton("clickME", this);

    QObject::connect(pb, SIGNAL(clicked()), mw, SLOT(test()));

    mw->show();
}

Form::~Form()
{
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public slots:
    void test();

private:
    QLabel *l;

};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    l = new QLabel("test", this);
}

MainWindow::~MainWindow()
{
}

void MainWindow::test() {
    qDebug() << "test called!" << endl;
    l->setText("text changed");
}

This works for me.

output

Community
  • 1
  • 1
Hayati Gonultas
  • 151
  • 2
  • 7
  • In fact I added the QMessageBox just to test if the function was called. And when I've just "ui->auteur->clear();" it's also doesn't work. But thanks for the information – Ben W Jul 13 '17 at 01:26
  • I've added `qDebug() << "test called!" << endl; ` at the beginning of the function test() and yes it appears "test called!" but `ui->auteur->clear();` has **still no effect**. In fact all operations that don't modify ui work – Ben W Jul 13 '17 at 02:23
  • Hi @ben-w, this code sample works for me. Since your slots called, you have something other than signals/slots. In fact, there are only two important issues with updating GUI. First one is the exec() call (that may block other gui events), and the other one is multi-threaded and busy applications that may block GUI thread to update components. – Hayati Gonultas Jul 13 '17 at 04:34
  • Ok I've **found**. I emit a signal from my child window (FormWindow) and connect it to my function in MainWindow using `connect(childObj,SIGNAL(return_to_patent()),parent,SLOT(returned_from_Child()));`. If someone want to learn more about that: [Using child Widget Signal to close Parent Widget](http://www.qtforum.org/article/28253/using-child-widget-signal-to-close-parent-widget.html). In my case for _childObj_ I used the `MyFromclass *pointer` that helped to open the child window (and not a new pointer like `MyFromclass *pointer=new MyFromclass()` ) – Ben W Jul 14 '17 at 00:31