Results 1 to 5 of 5

Thread: Function acces in mainwindow class from a dialog class?

  1. #1
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Function acces in mainwindow class from a dialog class?

    Hi all,

    I have the following classes:

    MainWindow class:
    Qt Code:
    1. namespace Ui {
    2. class MainWindow;
    3. }
    4.  
    5. class MainWindow : public QMainWindow {
    6. Q_OBJECT
    7.  
    8. public:
    9. explicit MainWindow(QWidget *parent = 0);
    10. QString someData();
    11. };
    12.  
    13. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    14. ui->setupUi(this);
    15. }
    16.  
    17. QString MainWindow::someData() {
    18. return "...";
    19. }
    To copy to clipboard, switch view to plain text mode 

    Dialog class:
    Qt Code:
    1. namespace Ui {
    2. class Dialog;
    3. }
    4.  
    5. class Dialog : public QDialog {
    6. Q_OBJECT
    7.  
    8. void anyFunction();
    9.  
    10. public:
    11. explicit Dialog(QWidget *parent = 0);
    12. };
    13.  
    14. Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) {
    15. ui->setupUi(this);
    16. }
    17.  
    18. void Dialog::anyFunction() {
    19. qDebug() << MainWindow->someData();
    20. }
    To copy to clipboard, switch view to plain text mode 

    In the Dialog class (row 19.): how can I access the someData() function from MainWindow?

    Thanks in advance!
    Chris

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Function acces in mainwindow class from a dialog class?

    You need a pointer to MainWindow instance.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Function acces in mainwindow class from a dialog class?

    That is obvious to me, but how I surrender the pointer?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Function acces in mainwindow class from a dialog class?

    I assume you are calling the dialog from within the main window, so you can call some custom method that will feed this pointer to the dialog. However if the goal of your MainWindow::someData() is to provide data for the dialog, it is better to add a method to the dialog that allows to pass (and store) the data object directly from the window when the dialog gets called.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Function acces in mainwindow class from a dialog class?

    Ok, thanks for your help!

Similar Threads

  1. Replies: 12
    Last Post: 18th April 2011, 15:24
  2. Replies: 3
    Last Post: 12th April 2011, 10:58
  3. Replies: 4
    Last Post: 17th January 2011, 14:05
  4. Replies: 7
    Last Post: 2nd September 2010, 19:42
  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.