Hello,
I've created an project (Qt Widget Application) with Qt Creator. Qt 5.6.1
My project structure:
myproject.pro
Headers
- dialogform.h
- mainwindow.h
Sources
- dialogform.cpp
- main.cpp
- mainwindow.cpp
Forms
- dialogform.ui
- mainwindow.ui
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;
}
{
Q_OBJECT
public:
explicit DialogForm
(QWidget *parent
= 0);
~DialogForm();
private slots:
void on_pbClearLineEdit_clicked();
private:
Ui::DialogForm *ui;
};
#endif // DIALOGFORM_H
#ifndef DIALOGFORM_H
#define DIALOGFORM_H
#include <QDialog>
namespace Ui {
class DialogForm;
}
class DialogForm : public QDialog
{
Q_OBJECT
public:
explicit DialogForm(QWidget *parent = 0);
~DialogForm();
private slots:
void on_pbClearLineEdit_clicked();
private:
Ui::DialogForm *ui;
};
#endif // DIALOGFORM_H
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void clear();
private slots:
void on_pbCallDialog_clicked();
private:
Ui::MainWindow *ui;
};
#endif // 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();
void clear();
private slots:
void on_pbCallDialog_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialogform.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pbCallDialog_clicked()
{
DialogForm *dialogForm = new DialogForm(this);
dialogForm->show();
// OR
// DialogForm d;
// d.exec();
}
void MainWindow::clear()
{
ui->lineEdit->clear();
}
#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();
// OR
// DialogForm d;
// d.exec();
}
void MainWindow::clear()
{
ui->lineEdit->clear();
}
To copy to clipboard, switch view to plain text mode
dialogform.cpp
#include "dialogform.h"
#include "ui_dialogform.h"
#include "mainwindow.h"
DialogForm
::DialogForm(QWidget *parent
) : ui(new Ui::DialogForm)
{
ui->setupUi(this);
}
DialogForm::~DialogForm()
{
delete ui;
}
void DialogForm::on_pbClearLineEdit_clicked()
{
MainWindow m;
m.clear(); // Here, is not clearing the MainWindow's lineEdit
}
#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()
{
MainWindow m;
m.clear(); // Here, is not clearing the MainWindow's lineEdit
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks