Results 1 to 10 of 10

Thread: Return to main widget

  1. #1
    Join Date
    Oct 2007
    Location
    Caracas - Venezuela
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Return to main widget

    Hi!!!

    I have an application that goes to Widget 1 ---> Widget 2

    I want to know how I can return to the main widget, Widget 1---> Widget 2 ---> Widget 1

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

    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3. #include <QWidget>
    4.  
    5.  
    6. class QPushButton;
    7.  
    8. class principal;
    9.  
    10.  
    11. //window.h
    12. class window : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. window();
    18.  
    19. public slots:
    20.  
    21. void showNew();
    22.  
    23. public:
    24. //....
    25. QPushButton *enter;
    26. principal *prin;
    27. //....
    28. };
    29. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //window.cpp
    2. #include <QtGui>
    3.  
    4. #include "principal.h"
    5. #include "window.h"
    6.  
    7.  
    8. window::window()
    9. {
    10. enter = new QPushButton(tr("a"));
    11. connect(enter, SIGNAL(clicked()), this, SLOT(showNew()));
    12. }
    13.  
    14. void window :: showNew(){
    15.  
    16. this -> close();
    17. prin = new principal;
    18. prin-> show();
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //principal.h
    2. #ifndef PRINCIPAL_H
    3. #define PRINCIPAL_H
    4. #include <QWidget>
    5.  
    6.  
    7. class QPushButton;
    8.  
    9.  
    10. class principal : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. principal();
    16.  
    17. public slots:
    18.  
    19. void returnWindow();
    20.  
    21. public:
    22. //....
    23. QPushButton *return;
    24. //....
    25. };
    26. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //principal.cpp
    2. #include <QtGui>
    3.  
    4. #include "principal.h"
    5. #include "window.h"
    6.  
    7.  
    8. principal:: principal()
    9. {
    10. return = new QPushButton(tr("b"));
    11. connect(return, SIGNAL(clicked()), this, SLOT(returnWindow()));
    12. }
    13.  
    14. void principal :: returnWindow(){
    15.  
    16. this -> close();
    17. //Here I want to return to the main widget
    18. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!!!
    Last edited by wysota; 30th November 2007 at 17:37. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Return to main widget

    hello
    [
    Qt Code:
    1. void window :: showNew(){
    2.  
    3. this -> close();
    4. prin = new principal(this); /// set parent of second widget to first one
    5. prin->show();
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    then you'll be able to
    Qt Code:
    1. void principal::goBack()
    2. {
    3. this -> close();
    4. parent()->show();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Note also that you're creating a instance of principal every time you enters it and you're not removing it after going back to main widget.
    You should create a single instance of principal in window's constructor and memorize a pointer to it. Then show/hide this instance by stored pointer and destroy it in window's destructor.
    See GrEEn (Graphics Effects Environment)
    http://sourceforge.net/project/platf...roup_id=232746
    a qt-based plugins oriented MDI image processing application(contains also qt plugins like styles & imageformats).

  3. #3
    Join Date
    Oct 2007
    Location
    Caracas - Venezuela
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Return to main widget

    I did it...but it didn't work.....this was the error:

    principal.cpp: In member function ‘void principal::goBack()’:
    principal.cpp:205: error: ‘class QObject’ has no member named ‘show’
    make: *** [principal.o] Error 1


  4. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Return to main widget

    Have you tried QWizard ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

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

    Default Re: Return to main widget

    I'm not sure what you are trying to do, but your issue is simply related to C++ skills and neither QWizard or any other Qt class won't help you with that. If you want to access some method of an object, you need to have a reference or a pointer to the object available and it needs to be cast to a proper type. Again, I don't know what you are trying to do, but you might want to take a look at QStackedWidget although you'll still have to manipulate pointers and scopes.

  6. #6
    Join Date
    Oct 2007
    Location
    Caracas - Venezuela
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Return to main widget

    I'm doing an application that: show a window (QWidget 1) where you fill the form and sign in....when you are in, show a window (QWidget 2) where you can do some activities that aren't relevant for this topic,well this QWidget 2 has a "Disconnect" button, when it's clicked the application must return to QWidget 1....How can I do this???

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

    Default Re: Return to main widget

    Stop treating it as "returning from widget2 to widget1". Just open a new instance of widget1 like you did the first time (probably in main()).
    Last edited by wysota; 30th November 2007 at 21:01.

  8. #8
    Join Date
    Oct 2007
    Location
    Caracas - Venezuela
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Return to main widget

    New instance?? It doesn't work or there is something that I'm doing wrong, for that reason I write here a code example for someone could tell me what and where I should put the correct code.

    Thanks!!!!!!

  9. #9
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Return to main widget

    Quote Originally Posted by 3nc31 View Post
    I did it...but it didn't work.....this was the error:

    principal.cpp: In member function ‘void principal::goBack()’:
    principal.cpp:205: error: ‘class QObject’ has no member named ‘show’
    make: *** [principal.o] Error 1

    as wysota said, you should cast a pointer.
    parent() returns QObject,
    show() is a method of QWidget,
    QWidget inherits QObject, so...

    try
    ((QWidget*)parent())->show();
    See GrEEn (Graphics Effects Environment)
    http://sourceforge.net/project/platf...roup_id=232746
    a qt-based plugins oriented MDI image processing application(contains also qt plugins like styles & imageformats).

  10. #10
    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: Return to main widget

    Quote Originally Posted by mchara View Post
    as wysota said, you should cast a pointer.
    parent() returns QObject,
    show() is a method of QWidget,
    QWidget inherits QObject, so...

    try
    ((QWidget*)parent())->show();
    Notice that there is a specialized method for this: QWidget::parentWidget()
    J-P Nurmi

Similar Threads

  1. Background image for main window widget using css.
    By Enygma in forum Qt Programming
    Replies: 8
    Last Post: 23rd August 2007, 15:40
  2. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  3. Replies: 4
    Last Post: 10th March 2007, 18:01
  4. closing dialog in hidden main Widget
    By Lele in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2006, 10:35
  5. Replies: 4
    Last Post: 24th March 2006, 22:50

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.