Results 1 to 8 of 8

Thread: Calling a new form from current form

  1. #1
    Join Date
    Apr 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Calling a new form from current form

    I'm designing front-end for a compression project using QT.

    Here is a screen-shot of the forms:
    http://img147.imageshack.us/img147/7517/qtinfoib2.jpg

    My code starts from the form START. The use then has to select whether he wants to compress or decompress. I then have to invoke the forms compress or decompress accordingly. How can I call the compress form from start, when the COMPRESS BUTTON IS CLICKED?

    Someone please help me regarding this.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

  3. #3
    Join Date
    Apr 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Calling a new form from current form

    I want to somehow invoke the compression window when the COMPRESS button is clicked in the START form.
    Here's the code related to START form genereted by UIC

    Qt Code:
    1. #ifndef START_H
    2. #define START_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QMainWindow>
    9. #include <QtGui/QMenuBar>
    10. #include <QtGui/QPushButton>
    11. #include <QtGui/QStatusBar>
    12. #include <QtGui/QWidget>
    13.  
    14. class Ui_MainWindow
    15. {
    16. public:
    17. QWidget *centralwidget;
    18. QPushButton *pushButton;
    19. QPushButton *pushButton_2;
    20. QMenuBar *menubar;
    21. QStatusBar *statusbar;
    22.  
    23. void setupUi(QMainWindow *MainWindow)
    24. {
    25. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    26. centralwidget = new QWidget(MainWindow);
    27. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    28. pushButton = new QPushButton(centralwidget);
    29. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    30. pushButton->setGeometry(QRect(30, 50, 75, 23));
    31. pushButton_2 = new QPushButton(centralwidget);
    32. pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
    33. pushButton_2->setGeometry(QRect(150, 50, 75, 23));
    34. MainWindow->setCentralWidget(centralwidget);
    35. menubar = new QMenuBar(MainWindow);
    36. menubar->setObjectName(QString::fromUtf8("menubar"));
    37. menubar->setGeometry(QRect(0, 0, 262, 21));
    38. MainWindow->setMenuBar(menubar);
    39. statusbar = new QStatusBar(MainWindow);
    40. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    41. MainWindow->setStatusBar(statusbar);
    42. retranslateUi(MainWindow);
    43. QSize size(262, 154);
    44. size = size.expandedTo(MainWindow->minimumSizeHint());
    45. MainWindow->resize(size);
    46. QMetaObject::connectSlotsByName(MainWindow);
    47. } // setupUi
    48.  
    49. void retranslateUi(QMainWindow *MainWindow)
    50. {
    51. MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    52. pushButton->setText(QApplication::translate("MainWindow", "COMPRESS", 0, QApplication::UnicodeUTF8));
    53. pushButton_2->setText(QApplication::translate("MainWindow", "DECOMPRESS", 0, QApplication::UnicodeUTF8));
    54. Q_UNUSED(MainWindow);
    55. } // retranslateUi
    56.  
    57. };
    58.  
    59. namespace Ui {
    60. class MainWindow: public Ui_MainWindow {};
    61. } // namespace Ui
    62.  
    63. #endif // START_H
    To copy to clipboard, switch view to plain text mode 


    The main code:

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QMainWindow>
    4.  
    5. #include "start.cpp"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. Ui_MainWindow ui;
    12. ui.setupUi(&w);
    13.  
    14. w.show();
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 



    Code generated from COMPRESSION form:

    Qt Code:
    1. #ifndef COMPRESSION_H
    2. #define COMPRESSION_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QLabel>
    9. #include <QtGui/QLineEdit>
    10. #include <QtGui/QMainWindow>
    11. #include <QtGui/QMenu>
    12. #include <QtGui/QMenuBar>
    13. #include <QtGui/QPushButton>
    14. #include <QtGui/QSpinBox>
    15. #include <QtGui/QStatusBar>
    16. #include <QtGui/QWidget>
    17.  
    18. class Ui_CompWindow
    19. {
    20. public:
    21. QAction *action_Exit;
    22. QAction *action_Compress;
    23. QAction *action_Decompress;
    24. QWidget *centralwidget;
    25. QLineEdit *lineEdit;
    26. QLineEdit *lineEdit_2;
    27. QLineEdit *lineEdit_3;
    28. QLineEdit *lineEdit_4;
    29. QLineEdit *lineEdit_5;
    30. QLabel *label_2;
    31. QLabel *label_3;
    32. QLabel *label_4;
    33. QLabel *label_5;
    34. QLabel *label_6;
    35. QPushButton *pushButton;
    36. QPushButton *pushButton_2;
    37. QSpinBox *spinBox;
    38. QLabel *label;
    39. QMenuBar *menubar;
    40. QMenu *menu_Menu;
    41. QMenu *menu_About;
    42. QMenu *menu_File;
    43. QStatusBar *statusbar;
    44.  
    45. void setupUi(QMainWindow *CompWindow)
    46. {
    47. CompWindow->setObjectName(QString::fromUtf8("CompWindow"));
    48. action_Exit = new QAction(CompWindow);
    49. action_Exit->setObjectName(QString::fromUtf8("action_Exit"));
    50. action_Compress = new QAction(CompWindow);
    51. action_Compress->setObjectName(QString::fromUtf8("action_Compress"));
    52. action_Decompress = new QAction(CompWindow);
    53. action_Decompress->setObjectName(QString::fromUtf8("action_Decompress"));
    54. centralwidget = new QWidget(CompWindow);
    55. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    56. lineEdit = new QLineEdit(centralwidget);
    57. lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
    58. lineEdit->setGeometry(QRect(140, 70, 261, 20));
    59. lineEdit_2 = new QLineEdit(centralwidget);
    60. lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
    61. lineEdit_2->setGeometry(QRect(140, 100, 261, 20));
    62. lineEdit_3 = new QLineEdit(centralwidget);
    63. lineEdit_3->setObjectName(QString::fromUtf8("lineEdit_3"));
    64. lineEdit_3->setGeometry(QRect(140, 130, 261, 20));
    65. lineEdit_4 = new QLineEdit(centralwidget);
    66. lineEdit_4->setObjectName(QString::fromUtf8("lineEdit_4"));
    67. lineEdit_4->setGeometry(QRect(140, 160, 261, 20));
    68. lineEdit_5 = new QLineEdit(centralwidget);
    69. lineEdit_5->setObjectName(QString::fromUtf8("lineEdit_5"));
    70. lineEdit_5->setGeometry(QRect(140, 190, 261, 20));
    71. label_2 = new QLabel(centralwidget);
    72. label_2->setObjectName(QString::fromUtf8("label_2"));
    73. label_2->setGeometry(QRect(90, 70, 41, 16));
    74. label_3 = new QLabel(centralwidget);
    75. label_3->setObjectName(QString::fromUtf8("label_3"));
    76. label_3->setGeometry(QRect(90, 100, 46, 14));
    77. label_4 = new QLabel(centralwidget);
    78. label_4->setObjectName(QString::fromUtf8("label_4"));
    79. label_4->setGeometry(QRect(90, 130, 46, 14));
    80. label_5 = new QLabel(centralwidget);
    81. label_5->setObjectName(QString::fromUtf8("label_5"));
    82. label_5->setGeometry(QRect(90, 160, 46, 14));
    83. label_6 = new QLabel(centralwidget);
    84. label_6->setObjectName(QString::fromUtf8("label_6"));
    85. label_6->setGeometry(QRect(90, 190, 46, 14));
    86. pushButton = new QPushButton(centralwidget);
    87. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    88. pushButton->setGeometry(QRect(120, 250, 75, 23));
    89. pushButton_2 = new QPushButton(centralwidget);
    90. pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
    91. pushButton_2->setGeometry(QRect(230, 250, 75, 23));
    92. spinBox = new QSpinBox(centralwidget);
    93. spinBox->setObjectName(QString::fromUtf8("spinBox"));
    94. spinBox->setGeometry(QRect(140, 20, 44, 22));
    95. spinBox->setMaximum(9);
    96. spinBox->setValue(3);
    97. label = new QLabel(centralwidget);
    98. label->setObjectName(QString::fromUtf8("label"));
    99. label->setGeometry(QRect(30, 20, 111, 31));
    100. CompWindow->setCentralWidget(centralwidget);
    101. menubar = new QMenuBar(CompWindow);
    102. menubar->setObjectName(QString::fromUtf8("menubar"));
    103. menubar->setGeometry(QRect(0, 0, 438, 21));
    104. menu_Menu = new QMenu(menubar);
    105. menu_Menu->setObjectName(QString::fromUtf8("menu_Menu"));
    106. menu_About = new QMenu(menubar);
    107. menu_About->setObjectName(QString::fromUtf8("menu_About"));
    108. menu_File = new QMenu(menubar);
    109. menu_File->setObjectName(QString::fromUtf8("menu_File"));
    110. CompWindow->setMenuBar(menubar);
    111. statusbar = new QStatusBar(CompWindow);
    112. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    113. CompWindow->setStatusBar(statusbar);
    114.  
    115. menubar->addAction(menu_File->menuAction());
    116. menubar->addAction(menu_Menu->menuAction());
    117. menubar->addAction(menu_About->menuAction());
    118. menu_Menu->addAction(action_Compress);
    119. menu_Menu->addAction(action_Decompress);
    120. menu_File->addAction(action_Exit);
    121.  
    122. retranslateUi(CompWindow);
    123.  
    124. QSize size(438, 325);
    125. size = size.expandedTo(CompWindow->minimumSizeHint());
    126. CompWindow->resize(size);
    127.  
    128.  
    129. QMetaObject::connectSlotsByName(CompWindow);
    130. } // setupUi
    131.  
    132. void retranslateUi(QMainWindow *CompWindow)
    133. {
    134. CompWindow->setWindowTitle(QApplication::translate("CompWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    135. action_Exit->setText(QApplication::translate("CompWindow", "&Exit", 0, QApplication::UnicodeUTF8));
    136. action_Compress->setText(QApplication::translate("CompWindow", "&Compress", 0, QApplication::UnicodeUTF8));
    137. action_Decompress->setText(QApplication::translate("CompWindow", "&Decompress", 0, QApplication::UnicodeUTF8));
    138. label_2->setText(QApplication::translate("CompWindow", "File 1", 0, QApplication::UnicodeUTF8));
    139. label_3->setText(QApplication::translate("CompWindow", "File 2", 0, QApplication::UnicodeUTF8));
    140. label_4->setText(QApplication::translate("CompWindow", "File 3", 0, QApplication::UnicodeUTF8));
    141. label_5->setText(QApplication::translate("CompWindow", "File 4", 0, QApplication::UnicodeUTF8));
    142. label_6->setText(QApplication::translate("CompWindow", "File 5", 0, QApplication::UnicodeUTF8));
    143. pushButton->setText(QApplication::translate("CompWindow", "OK", 0, QApplication::UnicodeUTF8));
    144. pushButton_2->setText(QApplication::translate("CompWindow", "EXIT", 0, QApplication::UnicodeUTF8));
    145. label->setText(QApplication::translate("CompWindow", "Compression Level", 0, QApplication::UnicodeUTF8));
    146. menu_Menu->setTitle(QApplication::translate("CompWindow", "&Menu", 0, QApplication::UnicodeUTF8));
    147. menu_About->setTitle(QApplication::translate("CompWindow", "&About", 0, QApplication::UnicodeUTF8));
    148. menu_File->setTitle(QApplication::translate("CompWindow", "&File", 0, QApplication::UnicodeUTF8));
    149. } // retranslateUi
    150.  
    151. };
    152.  
    153. namespace Ui {
    154. class CompWindow: public Ui_CompWindow {};
    155. } // namespace Ui
    156.  
    157. #endif // COMPRESSION_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 8th April 2007 at 19:53. Reason: changed [quote] to [code]

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling a new form from current form

    I never tried to use two main windows in the same app, but if you want it this way, then use QMainWindow::show() to show the compression window.

    To do this, create a slot in the class in which you create the compress button.
    Let's call this function onCompressClicked(). You can make this slot protected, since it will be used only by your class.

    Then, right after creating the compress button, add:

    connect( compressButton, SIGNAL( clicked() ), this, SLOT( onCompressClicked() ) );

    Then:

    Qt Code:
    1. void StartClass::onCompressClicked()
    2. {
    3. CompressWindow cWindow; //create an instance of the compress window
    4. cWindow.show();
    5. }
    To copy to clipboard, switch view to plain text mode 
    I am almost sure this will work ( creating a main window from a main window). However, you may have problem with event passing and handling from the second main window. Anyway, try this and if it works leave it like this.

    Regards

  5. #5
    Join Date
    Apr 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Calling a new form from current form

    For some strange reason, my connect call fails every time.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling a new form from current form

    You must be doing something wrong. Could you post that part of the code?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling a new form from current form

    Quote Originally Posted by marcel View Post
    void StartClass::onCompressClicked()
    {
    CompressWindow cWindow; //create an instance of the compress window
    cWindow.show();
    }
    This won't work, because show() is a non-blocking call --- it only posts an event and returns, so onCompressClicked() returns immediately and cWindow gets destroyed before user has a chance to see it.

    This should work:
    Qt Code:
    1. void StartClass::onCompressClicked()
    2. {
    3. CompressWindow * cWindow = new CompressWindow();
    4. cWindow->setAttribute( Qt::WA_DeleteOnClose );
    5. cWindow->show();
    6. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling a new form from current form

    Quote Originally Posted by webgenius View Post
    QMainWindow w;
    Ui_MainWindow ui;
    ui.setupUi(&w);
    Try the single inheritance approach.

    http://doc.trolltech.com/4.2/designe...tance-approach

    Quote Originally Posted by webgenius View Post
    For some strange reason, my connect call fails every time.
    You have to declare onCompressClicked() as a slot to be able to connect a signal to it. This means also that onCompressClicked() has to be a method of a class derived from QObject (for example a widget).

    Don't forget to add Q_OBJECT macro to your class definition.

    http://www.qtcentre.org/forum/faq.ph...alslot_nodebug

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.