Results 1 to 4 of 4

Thread: How to access a method from outside of its class

  1. #1

    Default How to access a method from outside of its class

    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
    ----------------
    Qt Code:
    1. QDialog *dlg = new QDialog(this);
    2. dlg->show();
    To copy to clipboard, switch view to plain text mode 

    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
    ------------
    Qt Code:
    1. public slots:
    2.  
    3. void clearText();
    To copy to clipboard, switch view to plain text mode 


    MainWindow.cpp
    --------------
    Qt Code:
    1. void MainWindow::clearText()
    2. {
    3. ui->textEdit->clear();
    4.  
    5. //QMessageBox::information (this, "TITLE", "WORKING = OK");
    6. }
    To copy to clipboard, switch view to plain text mode 


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


    Dialog.h
    --------
    Qt Code:
    1. Private or Pulic slots: // even if I put it in public or private, it still doesn't work
    2. void clearClick();
    To copy to clipboard, switch view to plain text mode 


    Dialog.cpp
    --------
    Qt Code:
    1. //in the constructor
    2.  
    3. connect(ui->clickOk, SIGNAL(clicked()), this, SLOT(clearClick()));
    To copy to clipboard, switch view to plain text mode 


    ---

    Qt Code:
    1. void QDialog::clearClick()
    2. {
    3.  
    4. // I tried both methods below but nothing seems to happen
    5.  
    6.  
    7. // First Method
    8.  
    9. dlg.clearText();
    10.  
    11.  
    12.  
    13. // Second Method
    14.  
    15. QMainWindow *dlg = new QMainWindow();
    16.  
    17. dlg->cleanText();
    18.  
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by anda_skoa; 15th December 2015 at 10:14. Reason: missing [code] tags

  2. #2
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access a method from outside of its class

    Quote Originally Posted by freesix View Post
    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to access a method from outside of its class

    Quote Originally Posted by freesix View Post
    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,
    _

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access a method from outside of its class

    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.

Similar Threads

  1. Replies: 4
    Last Post: 2nd April 2013, 09:13
  2. Replies: 2
    Last Post: 2nd July 2011, 01:02
  3. Replies: 3
    Last Post: 15th April 2010, 13:26
  4. How to call super class method from sub class
    By mkkguru in forum Qt Programming
    Replies: 9
    Last Post: 4th February 2010, 05:29
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.