Results 1 to 6 of 6

Thread: Multiple Forms Handling

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Multiple Forms Handling

    hi all,

    previously I posted a question on multiple forms handling. However no one replied. Anyway, I hope this time someone would reply me.

    I am using qt creator and designer to work out my application. I have 2 forms (f1,f2) I want to load f2 when f1 button is pressed and also pass a variable to f2. How can I do it?

    below is my codes. when I clicked on the button. nothing happen.

    Form1.h
    Qt Code:
    1. #ifndef FORM1_H
    2. #define FORM1_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class form1;
    8. }
    9.  
    10. class form1 : public QWidget {
    11. Q_OBJECT
    12. public:
    13. form1(QWidget *parent = 0);
    14. ~form1();
    15.  
    16. protected:
    17. void changeEvent(QEvent *e);
    18.  
    19. private slots:
    20. void load_form2();
    21.  
    22. private:
    23. Ui::form1 *ui;
    24. };
    25.  
    26. #endif // FORM1_H
    To copy to clipboard, switch view to plain text mode 

    form1.cpp
    Qt Code:
    1. #include "form1.h"
    2. #include "ui_form1.h"
    3. #include "form2.h"
    4. #include "ui_form2.h"
    5. #include <QString>
    6.  
    7. form1::form1(QWidget *parent) :
    8. QWidget(parent),
    9. ui(new Ui::form1)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. form1::~form1()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void form1::changeEvent(QEvent *e)
    20. {
    21. QWidget::changeEvent(e);
    22. switch (e->type()) {
    23. case QEvent::LanguageChange:
    24. ui->retranslateUi(this);
    25. break;
    26. default:
    27. break;
    28. }
    29. }
    30.  
    31. void form1::load_form2()
    32. {
    33. QString str = "to be pass over to f2";
    34. Form2 f2;
    35. f2.show();
    36. }
    To copy to clipboard, switch view to plain text mode 

    from2.h
    Qt Code:
    1. #ifndef FORM2_H
    2. #define FORM2_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class Form2;
    8. }
    9.  
    10. class Form2 : public QWidget {
    11. Q_OBJECT
    12. public:
    13. Form2(QWidget *parent = 0);
    14. ~Form2();
    15.  
    16. protected:
    17. void changeEvent(QEvent *e);
    18.  
    19. private slots:
    20. void load_form1();
    21.  
    22. private:
    23. Ui::Form2 *ui;
    24. };
    25.  
    26. #endif // FORM2_H
    To copy to clipboard, switch view to plain text mode 

    form2.cpp
    Qt Code:
    1. #include "form2.h"
    2. #include "ui_form2.h"
    3. #include "form1.h"
    4. #include "ui_form1.h"
    5.  
    6. Form2::Form2(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::Form2)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. Form2::~Form2()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void Form2::changeEvent(QEvent *e)
    19. {
    20. QWidget::changeEvent(e);
    21. switch (e->type()) {
    22. case QEvent::LanguageChange:
    23. ui->retranslateUi(this);
    24. break;
    25. default:
    26. break;
    27. }
    28. }
    29.  
    30. void Form2::load_form1()
    31. {
    32. form1 a;
    33. a.show();
    34. }
    To copy to clipboard, switch view to plain text mode 

    Main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "form1.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. form1 w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    When I tried to new form1 like the below code

    Form2 *f2 = new Form2(this) <- under button click on form 1
    f2.show();

    I got error
    error: request for member ‘show’ in ‘a’, which is of non-class type ‘form1*’

    if I remove the pointer I got this error
    error: ‘QWidget::QWidget(const QWidget&)’ is private
    within this context (points to class form1)

    Can someone help me out? I also need to know how to pass the str string over to f2.

    Thanks in advance.

  2. #2
    Join Date
    Oct 2009
    Posts
    105
    Thanked 4 Times in 2 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Multiple Forms Handling

    Try like this...
    create a pointer to form2 in form1 class. allocate memory in constructor.
    inside button_on_click slot

    call pointer->show();

  3. #3
    Join Date
    Jan 2010
    Posts
    39
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Multiple Forms Handling

    It works. I also found out the I did a mistake. there is no on clicked events.

    Anway I am one more doubt on multiple forms. How can I pass variables of one forms to another.

    ie form 1->label 1->text (lbl1) to form 2's->QString str

  4. #4
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Multiple Forms Handling

    I have the same problem.
    Have you found the way to access to form1 variables from form2?

  5. #5
    Join Date
    Nov 2010
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Multiple Forms Handling

    To pass a variable you should create a public pointer of the same tipe as the widget from the form you want to get the data from, and, in the constructor of the class you initialize them to the ui element.
    Qt Code:
    1. pt = ui->widget
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Multiple Forms Handling

    When I tried to new form1 like the below code

    Form2 *f2 = new Form2(this) <- under button click on form 1
    f2.show();

    I got error
    error: request for member ‘show’ in ‘a’, which is of non-class type ‘form1*’
    The error message tells you exactly what is wrong here (or at least it would if it matched the code snippet). "a" is "f2" and "form1*" is "Form2 *" in your code snippet. "f2" is a pointer to an object. "f2.show()" tries to to treat f2 as an instance of the class Form2.

    To your original question regarding sharing something between two forms you have several obvious options:
    • Pass the value into the constructor of Form2
    • Create a member variable inside Form2 and a public setter function. Call the setter after creating an instance of Form2.
    • If the value can change in Form1 while Form2 is open, and the change should be reflected in Form2, then use a signal in Form1 and slot in Form2.
    • Rethink your design if items inside Form1 do not logically belong there.

Similar Threads

  1. Multiple Forms and vertical layout in forms
    By eva2002 in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2010, 05:05
  2. Handling multiple forms
    By msmihai in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2008, 13:41
  3. PyQt - Mapping multiple forms to a database
    By peterjb31 in forum Qt Programming
    Replies: 4
    Last Post: 2nd May 2008, 21:31
  4. Layouting "Windows.Forms" like Qt forms
    By Boron in forum Qt Tools
    Replies: 1
    Last Post: 29th April 2008, 22:01
  5. Handling multiple UDP sockets
    By ecphora in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:01

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
  •  
Qt is a trademark of The Qt Company.