Results 1 to 3 of 3

Thread: how do you calling a function from MainWindow from a widget.

  1. #1
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question how do you calling a function from MainWindow from a widget.

    Hello,

    I have only just started programming both C++ and Qt and have a question about accessing functions in classes.

    I have a main function with a MDI and have a widget i created for the documents to open. I also have a Wizard called newWizard which currently opens shows some labels and is supposed to create a default child object in the Mdi.

    the MainWindow code used to create the Mdi and the children is shown below,

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "mainwindow.h"
    4. #include "newwizard.h"
    5. #include "groupbox.h"
    6.  
    7. MainWindow::MainWindow()
    8. {
    9. mdiArea = new QMdiArea;
    10. mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    11. mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    12. mdiArea->setViewMode(QMdiArea::TabbedView);
    13. setCentralWidget(mdiArea);
    14.  
    15. createActions();
    16. createMenu();
    17. createToolbars();
    18.  
    19. readSetting();
    20.  
    21. setWindowIcon(QIcon(":images/images/cegu.ico"));
    22. //setCurrentFile("");
    23.  
    24. createchild();
    25. }
    26.  
    27. void MainWindow::createchild()
    28. {
    29. GroupBox *child = new GroupBox;
    30. mdiArea->addSubWindow(child);
    31. child->show();
    32. }
    33.  
    34.  
    35. void MainWindow::fileNew()
    36. {
    37. newWizard wizard;
    38. wizard.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

    the headerfile for mainwindow is shown below:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class QAction;
    7. class QLabel;
    8. class QMdiArea;
    9. class newWizard;
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14. public:
    15. MainWindow();
    16. void createchild();
    17. ...
    18. private:
    19. void createActions();
    20. void createMenu();
    21. void createToolbars();
    22. void readSetting();
    23. void writeSetting();
    24. void loadFile(const QString &fileName);
    25. bool saveFile(const QString &fileName);
    26. void setCurrentFile(const QString &fileName);
    27. QString strippedName(const QString &fullFileName);
    28.  
    29. QStringList recentFiles;
    30. QMdiArea *mdiArea;
    To copy to clipboard, switch view to plain text mode 

    The newWizard code is shown below

    Qt Code:
    1. #include <QtGui>
    2. #include "newwizard.h"
    3. #include "mainwindow.h"
    4.  
    5.  
    6. newWizard::newWizard(QWidget *parent) :
    7. QWizard(parent)
    8. {
    9. addPage(new introPage);
    10. addPage(new fileTypePage);
    11. addPage(new whichVariablesPage);
    12. addPage(new conclusionPage);
    13.  
    14.  
    15. setWindowTitle(tr("New File Wizard"));
    16. }
    17.  
    18. void newWizard::accept(QWidget *parent) : QMainWindow(parent)
    19. {
    20. parent->
    21. createchild();
    22.  
    23. }
    24.  
    25. introPage::introPage(QWidget *parent) : QWizardPage(parent)
    26. {
    27. setTitle(tr("Introduction"));
    28.  
    29. label = new QLabel(tr("This wizard will create a new"
    30. "File either header or Footer"
    31. "with the variable you choose"
    32. ));
    33. label->setWordWrap(true);
    34.  
    35. QVBoxLayout *layout = new QVBoxLayout;
    36. layout->addWidget(label);
    37. setLayout(layout);
    38. }
    39.  
    40. fileTypePage::fileTypePage(QWidget *parent) : QWizardPage(parent)
    41. {
    42. setTitle(tr("File Type Selection"));
    43. setSubTitle(tr("Specify which File type either"
    44. "header or footer and the name."));
    45.  
    46. headerRadioButton = new QRadioButton(tr("&Header File: "));
    47. headerNameLineEdit = new QLineEdit;
    48. connect(headerRadioButton, SIGNAL(toggled(bool)), headerNameLineEdit, SLOT(setEnabled(bool)));
    49. headerRadioButton->setChecked(true);
    50.  
    51. footerRadioButton = new QRadioButton(tr("&Footer File: "));
    52. footerNameLineEdit = new QLineEdit;
    53. connect(footerRadioButton, SIGNAL(toggled(bool)), footerNameLineEdit, SLOT(setEnabled(bool)));
    54. footerRadioButton->setChecked(false);
    55. footerNameLineEdit->setEnabled(false);
    56.  
    57. registerField("HeaderName", headerNameLineEdit);
    58. registerField("headerSelect", headerRadioButton);
    59. registerField("FooterName", footerNameLineEdit);
    60. registerField("footerSelect", footerRadioButton);
    61.  
    62. QVBoxLayout *layout = new QVBoxLayout;
    63. layout->addWidget(headerRadioButton);
    64. layout->addWidget(headerNameLineEdit);
    65. layout->addWidget(footerRadioButton);
    66. layout->addWidget(footerNameLineEdit);
    67. setLayout(layout);
    68. }
    69.  
    70. whichVariablesPage::whichVariablesPage(QWidget *parent) : QWizardPage(parent)
    71. {
    72. setTitle(tr("Variables Selection"));
    73. setSubTitle(tr("Specify which Variables you would like to include."));
    74.  
    75.  
    76. }
    77.  
    78. conclusionPage::conclusionPage(QWidget *parent) :QWizardPage(parent)
    79. {
    80.  
    81. }
    To copy to clipboard, switch view to plain text mode 

    and the header file for that is

    Qt Code:
    1. #ifndef NEWWIZARD_H
    2. #define NEWWIZARD_H
    3.  
    4. #include <QWizard>
    5.  
    6. QT_BEGIN_NAMESPACE
    7. class QCheckBox;
    8. class QGroupBox;
    9. class QLabel;
    10. class QLineEdit;
    11. class QMdiArea;
    12. QT_END_NAMESPACE
    13.  
    14.  
    15. class newWizard : public QWizard
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. newWizard(QWidget *parent = 0);
    21.  
    22.  
    23. void accept();
    24.  
    25. };
    26.  
    27. class introPage : public QWizardPage
    28. {
    29. Q_OBJECT
    30.  
    31. public:
    32. introPage(QWidget *parent = 0);
    33.  
    34. private:
    35. QLabel *label;
    36. };
    37.  
    38. class fileTypePage : public QWizardPage
    39. {
    40. Q_OBJECT
    41.  
    42. public:
    43. fileTypePage(QWidget *parent = 0);
    44.  
    45. private:
    46. QLabel *headerNameLabel;
    47. QLabel *footerNameLabel;
    48. QLineEdit *headerNameLineEdit;
    49. QLineEdit *footerNameLineEdit;
    50. QRadioButton *headerRadioButton;
    51. QRadioButton *footerRadioButton;
    52.  
    53. };
    54.  
    55. class whichVariablesPage : public QWizardPage
    56. {
    57. Q_OBJECT
    58.  
    59. public:
    60. whichVariablesPage(QWidget *parent = 0);
    61.  
    62. private:
    63. QLabel *label;
    64. };
    65.  
    66.  
    67. class conclusionPage : public QWizardPage
    68. {
    69. Q_OBJECT
    70.  
    71. public:
    72. conclusionPage(QWidget *parent = 0);
    73.  
    74. private:
    75. QLabel *label;
    76. };
    77.  
    78. #endif // NEWWIZARD_H
    To copy to clipboard, switch view to plain text mode 

    This code is not the finished article and is still a work in progress. As can be seen in the newWizard code at lines 18 to 23 i am trying to access the createchild() function from the mainwindow in that function when the wizard closes.

    Any help on this would be great.

    Thanks

    James Parsons

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how do you calling a function from MainWindow from a widget.

    This question was asked several times, so please use the search function of this board.

    Short answer: use signal and slots mechanism or pass a pointer of your main window to the dialog. Better use the first version.

  3. The following user says thank you to Lykurg for this useful post:

    parsnips146 (30th July 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how do you calling a function from MainWindow from a widget.

    Thank you for the quick reply.

    The hint to use signal and slot mechanism was extreamly useful and enabled me to get this part of the code to work.

Similar Threads

  1. QAbstractItemView not calling mouse move event function
    By sagirahmed in forum Qt Programming
    Replies: 0
    Last Post: 5th July 2010, 10:26
  2. QtConncurrent - calling function within the class
    By jacek_ in forum Qt Programming
    Replies: 5
    Last Post: 28th October 2009, 17:37
  3. [QThread] Function calling after thread.stop()
    By Macok in forum Qt Programming
    Replies: 4
    Last Post: 7th February 2009, 13:33
  4. QDialog - Calling Main Window Function
    By Preeteesh in forum Newbie
    Replies: 4
    Last Post: 20th June 2007, 18:41
  5. Problems calling C function in C++/Qt class
    By Rayven in forum General Programming
    Replies: 2
    Last Post: 2nd June 2006, 21:32

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.