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.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();
}
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.




Reply With Quote
Bookmarks