0

OK, let's start again...

I've created an project (Qt Widget Application) with Qt Creator.

My project structure:

  • myproject.pro

    • Headers

      • dialogform.h

      • mainwindow.h

    • Sources

      • dialogform.cpp

      • main.cpp

      • mainwindow.cpp

    • Forms

      • dialogform.h

      • mainwindow.h

When I click on DialogForm pushbutton, I need to call the clear() function from MainWindow

In my code below, my project is running, but, the clear() function does not clear the lineedit.

Do anyone known how can i fix this?

Thank you very much!

dialogform.h

#ifndef DIALOGFORM_H
#define DIALOGFORM_H

#include <QDialog>

namespace Ui {
class DialogForm;
}

class DialogForm : public QDialog
{
    Q_OBJECT

signals:
    void clearMainWindow();

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

private slots:
    void on_pbClearLineEdit_clicked();

private:
    Ui::DialogForm *ui;
};

#endif // DIALOGFORM_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public slots:
    void clear();

private slots:
    void on_pbCallDialog_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

dialogform.cpp

#include "dialogform.h"
#include "ui_dialogform.h"
#include "mainwindow.h"

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

DialogForm::~DialogForm()
{
    delete ui;
}

void DialogForm::on_pbClearLineEdit_clicked()
{
    connect(); // need help here. I'm using Qt 5.6.1
}

main.cpp

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

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

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialogform.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pbCallDialog_clicked()
{
    DialogForm *dialogForm = new DialogForm(this);
    dialogForm->show();
}

void MainWindow::clear()
{
    ui->lineEdit->clear();
}
Juliano Gomes
  • 57
  • 1
  • 11
  • 3
    You can't access a member of a private member of a class from outside the class itself. – tambre Jan 20 '17 at 19:56
  • 1
    Possible duplicate of [Private and Protected Members : C++](http://stackoverflow.com/questions/224966/private-and-protected-members-c) – tambre Jan 20 '17 at 19:56
  • `myfunction` should be a slot in `myfile`. Not a free function. – drescherjm Jan 20 '17 at 19:59
  • I've already tried, declaring _myfunction_ as public member of _myfile.h_ and and creating an instance to call _myfunction_ in _myfile2.cpp_ . The result: I don't get error message anymore, but myfunction does not work `myfile m; m.myfunction();` – Juliano Gomes Jan 20 '17 at 20:23
  • You need to get the existing instance. Not create a new one. Without understanding how these 2 forms interact I really can't give advice on how to get the pointer to the instance of `myFile` when in `myFile2`. – drescherjm Jan 20 '17 at 21:27
  • Hello @drescherjm , i edited all text. Now it have the full code. Can you help me? thank you very much. – Juliano Gomes Jan 21 '17 at 12:29
  • Add a clearMainWindow signal in DialogForm. Add a clear slot in MainWindow. In on_pbCallDialog_clicked() make DialogForm dynamically allocated with MainWindow as its parent. After you allocate your instance of DialogForm connect the clearMainWindow signal to the clear slot in MainWindow. – drescherjm Jan 21 '17 at 13:47
  • Ok, I made the changes, but I dont understand how to make the _connect_, even reading the [documentation](http://doc.qt.io/qt-4.8/signalsandslots.html). can you help me with the connect? Thanks – Juliano Gomes Jan 23 '17 at 16:09

2 Answers2

1

myfunction accesses an attribute of class myfile. Therefore it either needs to be a method of myfile as well, or it could be a friend function of the class myfile. In the latter case, however, you would need to give the relevant instance of myfile to myfunction as an argument. All usages of myfunction would need to be updated in either case.

ilim
  • 4,477
  • 7
  • 27
  • 46
  • I've already tried, declaring _myfunction_ as public member of _myfile.h_ and and creating an instance to call _myfunction_ in _myfile2.cpp_ . The result: I don't get error message anymore, but myfunction does not work `myfile m; m.myfunction();` – Juliano Gomes Jan 20 '17 at 20:20
  • 1
    As you did not share relevant portions of your code(e.g. declaration of lineedit1, definition of the parent class of myfile, etc.) I am not sure as to why your code does not work. I can not even clearly understand what you mean by not working before seeing more of your code. That being said, your question was regarding the error you receive in the state of your code provided in your question. The scope of my response is, naturally, limited by the scope of your question. – ilim Jan 20 '17 at 20:34
0

The solution:

DialogForm::DialogForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogForm)
{
    ui->setupUi(this);
    connect(ui->pbClearLineEdit, &QPushButton::clicked, static_cast<MainWindow*>(parent), &MainWindow::clear);
}
Juliano Gomes
  • 57
  • 1
  • 11