PDA

View Full Version : How to access a method from outside of its class



freesix
14th December 2015, 18:21
Hello, please I need help.


I have my MainWindow and my Dialog, both created with QDesigner.

My MainWindow possesses a QTextEdit entitled 'textEdit' and my Dialog has a

QPushButton named 'clickOk'


My concern is when I click on that QPushButton of my Dialog that he clears everything I wrote in the textEdit using

'clear()' method of QTextEdit.

Of course I use signal and slot but it's not working, I mean when I click on QPushButton nothing occurs in the textEdit

of my MainWindow.

by the way, I use modeless to show my Dialog using 'show()' method and it's works perfectly:

this is the code
----------------


QDialog *dlg = new QDialog(this);
dlg->show();


I don't want to use modal with '.exec()' function as I want the user to still use both at the same time.



Below's my implementation for everything (the two Forms) :


IMPLEMENTATION
==============

MainWindow.h
------------


public slots:

void clearText();



MainWindow.cpp
--------------



void MainWindow::clearText()
{
ui->textEdit->clear();

//QMessageBox::information (this, "TITLE", "WORKING = OK");
}



****************************
****************************


Dialog.h
--------


Private or Pulic slots: // even if I put it in public or private, it still doesn't work
void clearClick();



Dialog.cpp
--------


//in the constructor

connect(ui->clickOk, SIGNAL(clicked()), this, SLOT(clearClick()));



---



void QDialog::clearClick()
{

// I tried both methods below but nothing seems to happen


// First Method

QMainWindow dlg;
dlg.clearText();



// Second Method

QMainWindow *dlg = new QMainWindow();

dlg->cleanText();


}


PS : I tried to test if my ' void MainWindow::clearText() ' of the MainWindow works by inserting a QMessageBox in it,

when I click on QPushButton of the QDialog, and of course it really goes through that function, because the QMessageBox

appears when I execute the project... but 'ui->textEdit->clear();' doesn't work.


Thanks for your help in advance

Vikram.Saralaya
15th December 2015, 03:36
QMainWindow dlg;
dlg.clearText();

// Second Method
QMainWindow *dlg = new QMainWindow();
dlg->cleanText();
}

PS : I tried to test if my ' void MainWindow::clearText() ' of the MainWindow works by inserting a QMessageBox in it,


I see that you tried to abstract away things.. but QMainWindow does not have a clearText() or cleanText(). May be you are referring to your custom MainWindow. In any case there isn't anything wrong with what you said in general, so if you can post your actual code it might be better to get some help.

anda_skoa
15th December 2015, 10:15
appears when I execute the project... but 'ui->textEdit->clear();' doesn't work.

Does the textedit in your newly created main window contain text?

Also: wouldn't you rather want to clear the textEdit in the existing main window?

Cheers,
_

d_stranz
15th December 2015, 17:23
void QDialog::clearClick()
{

// I tried both methods below but nothing seems to happen


// First Method

QMainWindow dlg;
dlg.clearText();



// Second Method

QMainWindow *dlg = new QMainWindow();

dlg->cleanText();

}

Neither one of these is the actual MainWindow that holds the text edit that you want to clear. In both cases, your code is creating *new* instances of the QMainWindow class (not your derived MainWindow), which are never made visible because you don't call show() on them. In the first case, the QMainWindow is created on the stack and it goes away as soon as the slot exits, and in the second case, you create a parentless QMainWindow which results in a memory leak.

The code you posted can't be your actual code, because as written it won't compile - QMainWindow has no methods called "clearText" or "cleanText". How do you expect us to help you diagnose a problem when you don't post the real code that causes it?

Your design problem is that your MainWindow class needs to know when a user has clicked a button in the Dialog class. MainWindow doesn't need to know anything about that button (like its name or pointer address or anything else) except to be notified when it is clicked. The MainWindow *does* know the Dialog instance's pointer address, because it creates it. So, one solution to the design problem is to use the Dialog as a way to provide the notification that MainWindow needs. This is called "abstraction", because hides the details of the Dialog implementation, but results in the same effect.

In your Dialog class, create a private slot to handle the button click. In the Dialog constructor, connect the ui button pointer to the Dialog's slot.
Also in the Dialog class, create a signal method - call it whatever you want. In the button click slot, emit this signal.

In your MainWindow class, create a slot to respond to the Dialog's signal. It can be private or public, it doesn't matter. In that slot, you will call the method to clear the text in your text edit.

In MainWindow, when you construct and show the Dialog class instance, connect the Dialog's signal to the MainWindow's slot. Now, when the button is clicked in the Dialog, the Dialog fires its signal, the MainWindow receives it, and the text edit is cleared.