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
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows
    Thanks
    5
    Thanked 3 Times in 2 Posts

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

    That's Wonderful .
    1/2 of my problem 's been solved .

    I've just noticed that when I drag & drop QStackedWidget using QT Design , Pages (instance of Widgets ) are created & added to the stacked widget .
    I can add any number of pages I want & add the controls into it . I can control which widget to be displayed .
    It works so great .

    The second half of my problem :
    - Whenever I try to declare instace of my .UI & add it to the QStackedWidget , Exception is appeared .
    I've tried :
    addWidget(WidgetObj);
    widget(index );
    insertWidget (index , WidgetObj);

    CustomSlot's code that implements slots of the .UI & also has the QStackedWidget :

    Qt Code:
    1. #include"CustomSlot2.h"
    2. #include "ui_Form2.h"
    3. #include "CustomSlot.h"
    4.  
    5.  
    6.  
    7. CustomSlot2::CustomSlot2(QWidget *parent): QWidget(parent)
    8. {
    9. formTwo.setupUi(this);
    10.  
    11. //CustomSlot is the class that implement the slots of the .UI that I want to add at QStackedWidget
    12. CustomSlot *CustomSlotObj ;
    13. //formTwo.stackedWidget->addWidget(CustomSlotObj);
    14.  
    15. // I've already just 2 pages at QStackedWidget , The third on is going to be in index 2
    16. formTwo.stackedWidget->insertWidget(2,CustomSlotObj);
    17.  
    18. QWidget *WidgetObj;
    19. WidgetObj= formTwo.stackedWidget->widget (2) ;
    20.  
    21.  
    22. WidgetObj->show();
    23.  
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 


    Thanks .

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

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

    "CustomSlot *CustomSlotObj;" produces nothing but a dangling pointer which points to some random memory garbage. You forgot to actually instantiate something with C++ operator new.
    J-P Nurmi

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

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

    Perfect , it works now .
    However I still have simple issues :

    1- When I add my .UI at the QStacked Widget , it successfully appears & I can just control it using Keyboard (i.e. the UI that I've added at QStacked Widget contains QLineEdit + Button I can't press the button using the mouse I should use my "Tab" in order to go to that control then press "Space" ) ?!!

    2- According to what I've have understood that only one widget at QStackedWidget can be displayed at once to the user .
    I just have one QWidget that is put by default at QStackedWidget , when I drag & drop the stack using QT Design . Then at this page I've added a button . Then using the code I 've added my .UI at the stack then show it .
    What 's weird : that both QWidgets that have the button + the other Widget that I 've add by code are displayed ?

    Thanks .

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

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

    Sounds like you created the form as a child of the stacked widget but you didn't actually add the widget to the stack widget. That's at least one reason why it wouldn't be managed by the stacked widget.
    J-P Nurmi

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

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

    How is that ?!!

    Part of Header files 's code generated to the form that contains the QStackedWidget :

    Qt Code:
    1. #include <QtGui/QStackedWidget>
    2. #include <QtGui/QWidget>
    3.  
    4.  
    5. class Ui_FormTwo
    6. {
    7. public:
    8. QStackedWidget *stackedWidget;
    9. QWidget *page;
    10.  
    11. void setupUi(QWidget *FormTwo)
    12. {
    13. stackedWidget = new QStackedWidget(FormTwo);
    14. stackedWidget->setObjectName(QString::fromUtf8("stackedWidget"));
    15. stackedWidget->setGeometry(QRect(460, 130, 551, 371));
    16. page = new QWidget();
    17. page->setObjectName(QString::fromUtf8("page"));
    18. page->setGeometry(QRect(0, 0, 551, 371));
    19. stackedWidget->addWidget(page);
    20.  
    21. retranslateUi(FormTwo);
    22.  
    23. stackedWidget->setCurrentIndex(0);
    24.  
    25. // ......................etc
    26.  
    27. } // setupUi
    To copy to clipboard, switch view to plain text mode 

    ---------------------------------------------------------------
    Temporarily , I've added the code that add my .UI to the stack at the constructor of the form that has the stack object :

    Qt Code:
    1. CustomSlot2::CustomSlot2(QWidget *parent): QWidget(parent)
    2. {
    3. formTwo.setupUi(this);
    4.  
    5. CustomSlot *CustomSlotObj=new CustomSlot() ;
    6.  
    7. formTwo.stackedWidget->insertWidget(1,CustomSlotObj);
    8.  
    9. QWidget *WidgetObj;
    10. WidgetObj= formTwo.stackedWidget->widget (1) ;
    11.  
    12. WidgetObj->show();
    13.  
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    Thanks .

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

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

    Aha, now I realized what's wrong.
    Quote Originally Posted by Fatla View Post
    Qt Code:
    1. QWidget *WidgetObj;
    2. WidgetObj= formTwo.stackedWidget->widget (1) ;
    3.  
    4. WidgetObj->show();
    To copy to clipboard, switch view to plain text mode 
    This is not the way you use QStackedWidget. You are supposed to use QStackedWidget::setCurrentIndex() or setCurrentWidget().
    J-P Nurmi

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

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

    Thanks , it works successfully .
    I 've one more question :

    - I've added many QWidgets into stack . Now I need when the turn comes to a particular widget to be shown , I can access its variables .

    Qt Code:
    1. #include "Container.h"
    2.  
    3.  
    4.  
    5. Container::Container(QWidget *parent): QWidget(parent)
    6. {
    7. ContainerUi.setupUi(this);
    8.  
    9. PageIndex=1;
    10.  
    11.  
    12. CustomSlot1 *CustomSlot1Obj =new CustomSlot1();
    13. CustomSlot2 *CustomSlot2Obj=new CustomSlot12();
    14. CustomSlot3 *CustomSlot3Obj=new CustomSlot3();
    15. //........................ to CustomSlot9
    16.  
    17.  
    18. // Push objects into the QStackedWidget
    19. ContainerUi.StackedWidget->addWidget(CustomSlot1);
    20. ContainerUi.StackedWidget->addWidget(CustomSlot2);
    21. ContainerUi.StackedWidget->addWidget(CustomSlot3);
    22. //.................. pust the other Widgets into the Stack
    23.  
    24.  
    25. ContainerUi.StackedWidget->setCurrentIndex(PageIndex);
    26. PageIndex++;
    27.  
    28. }
    29.  
    30. void Container :: NextPage()
    31. {
    32.  
    33.  
    34.  
    35. int CurrentIndex=ContainerUi.StackedWidget->currentIndex ();
    36. if(PageIndex <10)
    37. {
    38.  
    39. if(CurrentIndex==3 )
    40. {
    41. CustomSLot3 *widget ;
    42. widget=ContainerUi.StackedWidget->widget (3);
    43. // GrainSizeFlag is a public variable at CustomSlot3
    44. if(widget->GrainSizeFlag==true)
    45. ::PageIndex=7;
    46.  
    47.  
    48.  
    49. }
    50. ContainerUi.StackedWidget->setCurrentIndex(PageIndex);
    51. PageIndex++;
    52.  
    53. }
    54.  
    55.  
    56. }
    To copy to clipboard, switch view to plain text mode 

    By default the Stack contains one widget . So my "CustomSlot" 'll go to the index [1].
    As a result of that , CustomSlot3 'll go to index [3].

    I've an error that tells ms : Invalid Conversion QWidget* to CustomSlot3*

    Thanks .

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

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

    I suggest you keep all pointers as member variables of the containing class and access them directly instead of fetching the pointer through current widget of the stack (it is possible of course - you'd have to cast the pointer received to a proper type).

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

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

    It has been a long time since my last reply .
    Things were going so fine & the resources over web were extremely helpful .

    Currently , I was looking for property/function that DeActivate child widget
    (e.g. If I've a Widget that has a QPushButton . When the user press that button another widget 'll be shown & TOTALLY DeActivate the previouse widget .
    So the user won't be able even to close / maximize/minimize the old widget )

    I've tried :
    isActiveWindow():Just make the widget on top .
    setEnabledisable all the controls into widget but not the toolbar (i.e. the user will be able to close / maximize/minimize widget)

    Thanks .

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

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

    I've found a solution to the problem :

    Qt Code:
    1. this->setEnabled(false);
    2.  
    3. HMENU menu = ::GetSystemMenu(this->winId(), FALSE);
    4.  
    5. ::DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
    6.  
    7. ::EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
    To copy to clipboard, switch view to plain text mode 

    That of course after including : #include <qt_windows.h>

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

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

    I've solved the 2nd issue by :

    Qt Code:
    1. void MainWindow::EnableToolBar()
    2. {
    3. MainWindowMenu = ::GetSystemMenu(this->winId(), TRUE);
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

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

    Why would you want to disable maximizing/minimizing a window? Don't you think it is an extreme interference in the user's desktop? As for closing, you don't need any windows related functions, simply reimplement closeEvent() for your widget and ignore the event if you want to prevent the window from being closed. You can disable the maximize/minimize buttons if you really want to, by either hiding them using window flags or by removing the window from the window manager's control (by using window flags again). To disable the contents of a window simply disable() it (or setEnabled(false)).

  13. The following user says thank you to wysota for this useful post:

    Fatla (23rd July 2008)

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.