Results 1 to 7 of 7

Thread: How to get a reference for a control of the main form?

  1. #1
    Join Date
    Mar 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to get a reference for a control of the main form?

    Using the QT Designer I have made the following things:
    1-a main form called MainFormDlg (QMainWindow) with a Tree Widget control (treeDownloads);
    2-a dialog box called DownloadDlg (QDialog) containing OK and CANCEL buttons.

    When the user presses the OK button in the DownloadDlg dialog, I wanna clear the content of the treeDownloads. But I don't know how to get a reference to treeDownloads which is from the main form.

    My code below compiles fine, but when I press the OK button on the DownloadDlg dialog, the program simply crashes. (Please, look at my questions at the commented lines **):

    //downloaddlg.cpp:
    Qt Code:
    1. #include "downloaddlg.h"
    2.  
    3. DownloadDlg::DownloadDlg(QWidget *parent) :
    4. QDialog(parent){
    5. setupUi(this);
    6.  
    7. connect(buttonBox, SIGNAL(accepted()), this, SLOT(addUrlsToTree()));
    8.  
    9. setAttribute(Qt::WA_DeleteOnClose);
    10. pMainForm = (Ui_MainFormDlg*)parent; // ** Is this correct? **
    11. }
    12.  
    13. void DownloadDlg::addUrlsToTree(){
    14. pMainForm->treeDownloads->clear(); // ** Is this correct? **
    15. }
    To copy to clipboard, switch view to plain text mode 

    //downloaddlg.h:
    Qt Code:
    1. #ifndef DOWNLOADDLG_H
    2. #define DOWNLOADDLG_H
    3.  
    4. #include <QtGui/QDialog>
    5. #include <QtGui/QMainWindow>
    6. #include "ui_downloaddlg.h"
    7. #include "ui_mainform.h"
    8. //#include "mainform.h"
    9.  
    10. class DownloadDlg : public QDialog, private Ui::DownloadDlg {
    11. Q_OBJECT
    12. Q_DISABLE_COPY(DownloadDlg)
    13. public:
    14. explicit DownloadDlg(QWidget *parent = 0);
    15.  
    16. private:
    17. Ui_MainFormDlg *pMainForm;
    18.  
    19. private slots:
    20. void addUrlsToTree();
    21.  
    22. protected:
    23. virtual void changeEvent(QEvent *e);
    24. };
    25.  
    26. #endif // DOWNLOADDLG_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by thiforums; 20th March 2009 at 04:16.

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get a reference for a control of the main form?

    what is the significance of this:

    Qt Code:
    1. pMainForm = (Ui_MainFormDlg*)parent
    To copy to clipboard, switch view to plain text mode 

    i believe what u are trying to do(or should do) is

    Qt Code:
    1. this->setParent(pMainForm)
    To copy to clipboard, switch view to plain text mode 

    my guess is with your statement, pMainForm is getting a NULL value, which causes the crash when u try to access that variable in addUrlsToTree().

  3. #3
    Join Date
    Mar 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get a reference for a control of the main form?

    Quote Originally Posted by talk2amulya View Post
    what is the significance of this:

    Qt Code:
    1. pMainForm = (Ui_MainFormDlg*)parent
    To copy to clipboard, switch view to plain text mode 
    I thought that the 'parent' argument of DownloadDlg constructor was a reference to the parent window (in this case to the MainFormDlg). But I hope it's wrong.

    Quote Originally Posted by talk2amulya View Post
    i believe what u are trying to do(or should do) is
    Qt Code:
    1. this->setParent(pMainForm)
    To copy to clipboard, switch view to plain text mode 

    my guess is with your statement, pMainForm is getting a NULL value, which causes the crash when u try to access that variable in addUrlsToTree().
    I tested the instruction you passed, it compiles, but when I try to create the DownloadDlg the app crashes.

    //downloaddlg.cpp
    Qt Code:
    1. DownloadDlg::DownloadDlg(QWidget *parent) :
    2. QDialog(parent){
    3. setupUi(this);
    4. connect(buttonBox, SIGNAL(accepted()), this, SLOT(addUrlsToTree()));
    5. setAttribute(Qt::WA_DeleteOnClose);
    6. this->setParent((QWidget*)pMainForm); //** problem here, only way I found to compile it **
    7. }
    To copy to clipboard, switch view to plain text mode 

    I'm creating the dialog using this code:

    //mainform.cpp
    Qt Code:
    1. void MainForm::newDownloadDlg(){
    2. DownloadDlg *dlg=new DownloadDlg(this);
    3. if(dlg->exec()){
    4. //ok pressed
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if you understood what I need, but I just wanna have access to the treeDownloads control through DownloadDlg dialog, then I'll be able to play with this control (like adding,clearing,deleting items etc)

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get a reference for a control of the main form?

    when using this:

    Qt Code:
    1. DownloadDlg *dlg=new DownloadDlg(this);
    To copy to clipboard, switch view to plain text mode 

    u wont need to call setParent as i told, cuz the above statement will achieve that functionality..so remove the setParent() call and run again

  5. #5
    Join Date
    Mar 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get a reference for a control of the main form?

    If I remove that line, the code will come back to the start and nothing changes.

    Look below the code. I'm trying to access the treeDownloads control (which is located at the MainFormDlg class) through DownloadDlg class. But doing that way occurs an error:
    Qt Code:
    1. void DownloadDlg::addUrlsToTree(){
    2. treeDownloads->clear(); // ** error: "treeDownloads was not declared on this scope **
    3. }
    To copy to clipboard, switch view to plain text mode 
    I wanna know how can I avoid this error.

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get a reference for a control of the main form?

    (First of all - read this post to the end, although it's so long :])

    Try this way:
    Qt Code:
    1. #ifndef DOWNLOADDLG_H
    2. #define DOWNLOADDLG_H
    3.  
    4. #include <QtGui/QDialog>
    5. #include <QtGui/QMainWindow>
    6. #include "ui_downloaddlg.h"
    7. #include "ui_mainform.h"
    8. //#include "mainform.h"
    9.  
    10. class MainFormDlg; // let DownloadDlg know about MainFormDlg without includeing file here
    11.  
    12. class DownloadDlg : public QDialog, private Ui::DownloadDlg {
    13. Q_OBJECT
    14. Q_DISABLE_COPY(DownloadDlg)
    15. public:
    16. explicit DownloadDlg(MainFormDlg*parent = 0); // change QWidget to MainFormDlg as you this would be the parent for sure
    17.  
    18. private:
    19. MainFormDlg *pMainForm; // don use the UI_, just make one more method in MainFormDlg (see below)
    20.  
    21. private slots:
    22. void addUrlsToTree();
    23.  
    24. protected:
    25. virtual void changeEvent(QEvent *e);
    26. };
    27.  
    28. #endif // DOWNLOADDLG_H
    To copy to clipboard, switch view to plain text mode 

    And then:
    Qt Code:
    1. #include "downloaddlg.h"
    2. #include "mainform.h" // the file with MainFormDlg
    3.  
    4. DownloadDlg::DownloadDlg(MainFormDlg*parent) :
    5. QDialog(parent),
    6. pMainForm(parent) // pMainForm points to the parent = MainFormDlg
    7. {
    8. setupUi(this);
    9.  
    10. connect(buttonBox, SIGNAL(accepted()), this, SLOT(addUrlsToTree()));
    11.  
    12. setAttribute(Qt::WA_DeleteOnClose);
    13. }
    14.  
    15. void DownloadDlg::addUrlsToTree(){
    16. // but to get to the ui parts you have make them public or make some other methods
    17. }
    To copy to clipboard, switch view to plain text mode 

    For example you can do:
    Qt Code:
    1. public:
    2. Ui::MainFormDlg * ui;
    To copy to clipboard, switch view to plain text mode 
    in MainFormDlg class, or make some public method like void clearTree(); which will clear the tree.

    But this is the way you want to do and it's not good i think. I think the better way is to use signals and slots. I dont know what the buttonBox is in your dialog, but if the signal is emitted outside the DownloadDlg you can just connect accepted() to the clear() slot of the tree in MainFormDlg just below the DownloadDlg creation. If it's not possible to get the signal accepted() in MainFormDlg, make your own signal like acceptedSignal() and connect it to clear(). Example:
    Qt Code:
    1. #ifndef DOWNLOADDLG_H
    2. #define DOWNLOADDLG_H
    3.  
    4. #include <QtGui/QDialog>
    5. #include <QtGui/QMainWindow>
    6. #include "ui_downloaddlg.h"
    7. #include "ui_mainform.h"
    8.  
    9. class DownloadDlg : public QDialog, private Ui::DownloadDlg {
    10. Q_OBJECT
    11. Q_DISABLE_COPY(DownloadDlg)
    12. public:
    13. explicit DownloadDlg(QWidget *parent = 0);
    14.  
    15. signals:
    16. void acceptedSignal();
    17.  
    18. protected:
    19. virtual void changeEvent(QEvent *e);
    20. };
    21.  
    22. #endif // DOWNLOADDLG_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "downloaddlg.h"
    2.  
    3. DownloadDlg::DownloadDlg(QWidget *parent) :
    4. QDialog(parent){
    5. setupUi(this);
    6.  
    7. connect(buttonBox, SIGNAL(accepted()), this, SIGNAL(acceptedSignal())); // first signal emits second one
    8.  
    9. setAttribute(Qt::WA_DeleteOnClose);
    10. }
    To copy to clipboard, switch view to plain text mode 

    and in MainFormDlg:
    Qt Code:
    1. void MainForm::newDownloadDlg(){
    2. DownloadDlg *dlg=new DownloadDlg(this);
    3. connect(dlg, SIGNAL(acceptedSignal()), ui->treeDownloads, SLOT(clear())); // connects accepted with clear, and that's what you want, right?
    4. if(dlg->exec()){
    5. //ok pressed
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    That is better way i think :] good luck! :]
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    Join Date
    Mar 2009
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get a reference for a control of the main form?

    faldzip, thank you very much, your code was very useful for me to get my program working.
    About your sugestion, using signals/slots, I'm still a begginer on qt framework and I was just trying to do a simple task which was to have access to a UI control through another dialog.

    In main form I'm able to access the UI control directly, for example:

    MainForm::MainForm(QWidget *parent)
    : QMainWindow(parent)
    {
    setupUi(this);
    createActions();
    createToolBars();

    treeDownloads->clear(); // ** direct access to the tree widget control **
    }
    I'm not sure but I think this works because I used Multiple Inheritance concept when declaring MainForm as public of Ui_MainFormDlg.

    Then, inside the DownloadDlg class I was trying to do something similar, like this:
    void DownloadDlg::addUrlsToTree(){
    pMainForm->treeDownloads->clear();
    }
    I'm more confortable using this way than creating many methods to do something with the tree widget.

    Bye

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  3. MS Sql native driver??
    By LordQt in forum Qt Programming
    Replies: 4
    Last Post: 9th October 2007, 13:41
  4. error undefined reference ...............
    By amit_pansuria in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 14:28
  5. how to correctly compile threads support?
    By srhlefty in forum Installation and Deployment
    Replies: 9
    Last Post: 25th June 2006, 19:15

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.