Results 1 to 20 of 27

Thread: Allow Many QWidgets to be Shown into One QWidget ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Question Allow Many QWidgets to be Shown into One QWidget ?

    I've created Many (.UI) , made a (Header File + .cpp) to each .UI in order to implement their own slots , & Linked them by making a widget to be shown using :
    Qt Code:
    1. // CustomSlot2 implements the slots inside the 2nd .UI
    2. CustomSlot2* widget = new CustomSlot2();
    3. widget->setAttribute(Qt::WA_DeleteOnClose);
    4. widget->show();
    To copy to clipboard, switch view to plain text mode 

    CustomSlot2 Constructor's Code is :
    Qt Code:
    1. CustomSlot2::CustomSlot2(QWidget *parent): QWidget(parent)
    2. {
    3. formTwo.setupUi(this);
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    So far so Good . However , by making this approach there's a flicker when you Open & Hide/Close Windows .
    I've thought in an alternative way ;that I'll Drag & Drop a QWidget into my Main Window using QT Design.
    The Problem : How Could I open my .UIs into the QWidget that I've added @ MainWindow .

    The MainWindow's header file generated by "make" :

    Qt Code:
    1. class Ui_MainForm
    2. {
    3. public:
    4. QPushButton *btnProcess;
    5. QLineEdit *txtInput;
    6. QLineEdit *txtResult;
    7. QPushButton *btnNext;
    8. QWidget *widget;
    9. //........................ etc
    To copy to clipboard, switch view to plain text mode 

    Thanks .

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    The ui must be creating classes for you, isnt it ?
    well add those classes/windows into ur main window as u would add a widget...
    hope i understand what you meant

  3. #3
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    well add those classes/windows into ur main window as u would add a widget...
    How could I make all the .UIs show in the same Widget @ Main Window ?

    hope i understand what you meant
    Actually , It's My mistake that I didn't clarify my Problem well .
    So , Let me clarify it again ;
    Now , I'm @ QT Design then I create a normal widget ,add ctrls into it.
    I make the previous step 4 times . So now I've 4 .UIs that have there own ctrls into it .
    To Each UI, I create its (.h + .cpp ) to implement my own slots .
    Well now I open ,using QT Design ,the 1st .ui I 've made & Drag & Drop Widget into it , which is located into Container Section .
    The Problem : is how to make other .UIs open into the widget I created @ the 1st .ui I 've made .

    Thanks .

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    You can add a plain QWidget on the form and promote it as a custom widget (which loads another ui).
    J-P Nurmi

  5. #5
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    Thanks aamer4yu.
    Perfect jpn .

    It works now after using Promoting .
    @ the MainWindow I've added a plain QWidget into it .
    I've noticed that its header file generated by "make" creates instance of CustomSlot (which implements the slots of the form appears into the plain widget @ MainWindow form .

    The Problem :now I want to replace the .UI that opened into the plain widget @ MainWindow with another .UI @ SAme plain Widget


    Header File's code generated of MainWindow that Contains the plain widget :

    Qt Code:
    1. class Ui_MainForm
    2. {
    3. public:
    4. CustomSlot *MiniWidget; // MiniWidget = The Plain widget
    5.  
    6. void setupUi(QWidget *MainForm)
    7. {
    8. if (MainForm->objectName().isEmpty())
    9. MainForm->setObjectName(QString::fromUtf8("MainForm"));
    10. MainForm->resize(530, 402);
    11. MiniWidget = new CustomSlot(MainForm);
    12. MiniWidget->setObjectName(QString::fromUtf8("MiniWidget"));
    13. MiniWidget->setGeometry(QRect(39, 29, 451, 301));
    14.  
    15. retranslateUi(MainForm);
    16.  
    17. QMetaObject::connectSlotsByName(MainForm);
    18. } // setupUi
    19.  
    20. void retranslateUi(QWidget *MainForm)
    21. {
    22. MainForm->setWindowTitle(QApplication::translate("MainForm", "Main Form", 0, QApplication::UnicodeUTF8));
    23. Q_UNUSED(MainForm);
    24. } // retranslateUi
    25.  
    26. };
    27.  
    28. namespace Ui {
    29. class MainForm: public Ui_MainForm {};
    30. } // namespace Ui
    31.  
    32. QT_END_NAMESPACE
    33.  
    34. #endif // UI_MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Main.cpp 's code :

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

    The MainApp's code that implements slots of the MainWindow :
    Qt Code:
    1. MainApp::MainApp(QWidget *parent): QWidget(parent)
    2. {
    3. ui.setupUi(this);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    What do u mean replace ? If you mean to show another widget in same area, you can have a look at QStackedWidget. It allows you to show one widget at a time in the same area

  7. #7
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    So , do you mean that there 's NO Way to use the plain widget to solve my problem ?!!
    Mmmm what about using many plain widgets @ the MainWindow & specify each one of them to a .ui ?!!
    If I follow that way , how could I ctrl in showing & closing/hiding the plain widgets ?

    Thanks

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    Just place a stacked widget on your form as proposed. Then make each page of the stack contain a different custom widget (which all load their own ui files). Switching from one custom widget to another is only a matter of switching the current widget in the stack.
    J-P Nurmi

  9. #9
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    Quote Originally Posted by jpn View Post
    Just place a stacked widget on your form as proposed. Then make each page of the stack contain a different custom widget (which all load their own ui files). Switching from one custom widget to another is only a matter of switching the current widget in the stack.
    Actually I put set of plain widgets over each other @ my MainWindow .
    Then I promote each one to the proper .ui .
    Then @ my main : I've created instance of QStackedWidget & put into it the plain widgets that I've just created .

    Problems :
    - Program doesn't run even an .exe is generated + Exception is appeared
    - I was thinking that after creating my instance of StackedObject, I'll use it to ctrl which UI is going to be appeared , How could I achieve smth like that ?!!

    My Main 's code :
    Qt Code:
    1. #include <QApplication>
    2. #include "ui_MainWindow.h"
    3. #include "MainApp.cpp"
    4. #include <QStackedWidget>
    5.  
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QStackedWidget *StackedWidget = new QStackedWidget;
    12. Ui_MainForm ui;
    13.  
    14. StackedWidget->addWidget(ui.MiniWidget);
    15. StackedWidget->addWidget(ui.MiniWidget2);
    16.  
    17. MainApp Widget;
    18. Widget.show();
    19. return app.exec();
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    -------------------------------------------------------------------------
    MainWindow's generated code that contains the plain widgetS :

    Qt Code:
    1. #ifndef UI_MAINWINDOW_H
    2. #define UI_MAINWINDOW_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QWidget>
    9. #include <customslot2.h>
    10. #include "customslot.h"
    11.  
    12. QT_BEGIN_NAMESPACE
    13.  
    14. class Ui_MainForm
    15. {
    16. public:
    17. // These are the Plain Widgets I created
    18. CustomSlot *MiniWidget;
    19. CustomSlot2 *MiniWidget2;
    20.  
    21. void setupUi(QWidget *MainForm)
    22. {
    23. if (MainForm->objectName().isEmpty())
    24. MainForm->setObjectName(QString::fromUtf8("MainForm"));
    25. MainForm->resize(530, 402);
    26. MiniWidget = new CustomSlot(MainForm);
    27. MiniWidget->setObjectName(QString::fromUtf8("MiniWidget"));
    28. MiniWidget->setGeometry(QRect(39, 29, 451, 301));
    29. MiniWidget2 = new CustomSlot2(MiniWidget);
    30. MiniWidget2->setObjectName(QString::fromUtf8("MiniWidget2"));
    31. MiniWidget2->setGeometry(QRect(0, 110, 461, 301));
    32.  
    33. retranslateUi(MainForm);
    34.  
    35. QMetaObject::connectSlotsByName(MainForm);
    36. } // setupUi
    37.  
    38. void retranslateUi(QWidget *MainForm)
    39. {
    40. MainForm->setWindowTitle(QApplication::translate("MainForm", "Main Form", 0, QApplication::UnicodeUTF8));
    41. Q_UNUSED(MainForm);
    42. } // retranslateUi
    43.  
    44. };
    45.  
    46. namespace Ui {
    47. class MainForm: public Ui_MainForm {};
    48. } // namespace Ui
    49.  
    50. QT_END_NAMESPACE
    51.  
    52. #endif // UI_MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Thanks

  10. #10
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    I've tried many times to use QStackedWidget . However , I'm facing a few difficulties with it .
    Problem : How Could I use it into my app ,mentioned b4 ?!!

  11. #11
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    I've made a little progress .
    I've just noticed that there's @ QT Design smth called "stacked Widget" .

    I 've promoted to the Stacked Widget one of my .UI .

    Problem :
    - I got an error : 'class CustomSlot' has no member named 'addWidget' ?!!
    - I 've noticed that I can add any number of pages @ Stacked Widget , so How Can I promote different .UI , to each page I 've ?

    Header File 's code that has the custom Widget

    Qt Code:
    1. #ifndef UI_FORM2_H
    2. #define UI_FORM2_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QGroupBox>
    9. #include <QtGui/QLabel>
    10. #include <QtGui/QLineEdit>
    11. #include <QtGui/QPushButton>
    12. #include <QtGui/QRadioButton>
    13. #include <QtGui/QTableView>
    14. #include <QtGui/QWidget>
    15. #include <customslot.h>
    16.  
    17. QT_BEGIN_NAMESPACE
    18.  
    19. class Ui_FormTWo
    20. {
    21. public:
    22. QPushButton *btnGender;
    23. QGroupBox *gpBoxGender;
    24. QRadioButton *rdBtnMale;
    25. QRadioButton *rdBtnFemale;
    26. QLabel *lblSelectedFilePath;
    27. QPushButton *btnBrowse;
    28. QPushButton *pushButton;
    29. QLineEdit *txtFileData;
    30. QPushButton *pushButton_2;
    31. QPushButton *btnReadImage;
    32. QTableView *TableViewData;
    33. QPushButton *btnSaveAs;
    34. CustomSlot *stackedWidget;
    35. QWidget *Form1;
    36. QWidget *Form2;
    37.  
    38. void setupUi(QWidget *FormTwo)
    39. {
    40. stackedWidget = new CustomSlot(FormTwo);
    41. stackedWidget->setObjectName(QString::fromUtf8("stackedWidget"));
    42. stackedWidget->setGeometry(QRect(540, 230, 171, 131));
    43. Form1 = new QWidget();
    44. Form1->setObjectName(QString::fromUtf8("Form1"));
    45. Form1->setGeometry(QRect(0, 0, 171, 131));
    46. stackedWidget->addWidget(Form1);
    47. Form2 = new QWidget();
    48. Form2->setObjectName(QString::fromUtf8("Form2"));
    49. Form2->setGeometry(QRect(0, 0, 171, 131));
    50. stackedWidget->addWidget(Form2);
    51.  
    52. retranslateUi(FormTwo);
    53.  
    54. stackedWidget->setCurrentIndex(0);
    55.  
    56.  
    57.  
    58. } // setupUi
    59.  
    60. void retranslateUi(QWidget *FormTanya)
    61. {
    62. // ...........................etc
    63. } // retranslateUi
    64.  
    65. };
    66.  
    67. namespace Ui {
    68. class FormTwo: public Ui_FormTwo {};
    69. } // namespace Ui
    70.  
    71. QT_END_NAMESPACE
    72.  
    73. #endif // UI_FORM2_H
    To copy to clipboard, switch view to plain text mode 

    ---------------------------------------------------------------------------------
    My CustomSlot 's code :
    Qt Code:
    1. #include"CustomSlot.h"
    2. #include "ui_Test.h"
    3.  
    4. #include "ui_Form2.h"
    5. #include"CustomSlot2.cpp"
    6.  
    7.  
    8.  
    9. CustomSlot::CustomSlot(QWidget *parent): QWidget(parent)
    10. {
    11. ui.setupUi(this);
    12. }
    13. // ......................etc
    To copy to clipboard, switch view to plain text mode 

    Thanks .

  12. #12
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Allow Many QWidgets to be Shown into One QWidget ?

    Now I've 2 issues :
    1- How could I also disable "Maximize" & "Minimize" ?
    2- How could I enable them after I've just disabled them ?


    Thanks .

Similar Threads

  1. Error in put one QWidget in another QWidget
    By xjtu in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2008, 16:05
  2. Replies: 2
    Last Post: 1st January 2008, 13:31

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.