PDA

View Full Version : Return to main widget



3nc31
30th November 2007, 05:31
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


//main.cpp
#include <QApplication>
#include "window.h"


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
window window;
app.setMainWidget(&window);
window.show();
return app.exec();
}


#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>


class QPushButton;

class principal;


//window.h
class window : public QWidget
{
Q_OBJECT

public:
window();

public slots:

void showNew();

public:
//....
QPushButton *enter;
principal *prin;
//....
};
#endif


//window.cpp
#include <QtGui>

#include "principal.h"
#include "window.h"


window::window()
{
enter = new QPushButton(tr("a"));
connect(enter, SIGNAL(clicked()), this, SLOT(showNew()));
}

void window :: showNew(){

this -> close();
prin = new principal;
prin-> show();

}



//principal.h
#ifndef PRINCIPAL_H
#define PRINCIPAL_H
#include <QWidget>


class QPushButton;


class principal : public QWidget
{
Q_OBJECT

public:
principal();

public slots:

void returnWindow();

public:
//....
QPushButton *return;
//....
};
#endif


//principal.cpp
#include <QtGui>

#include "principal.h"
#include "window.h"


principal:: principal()
{
return = new QPushButton(tr("b"));
connect(return, SIGNAL(clicked()), this, SLOT(returnWindow()));
}

void principal :: returnWindow(){

this -> close();
//Here I want to return to the main widget
}

Thanks!!!

mchara
30th November 2007, 07:51
hello
[


void window :: showNew(){

this -> close();
prin = new principal(this); /// set parent of second widget to first one
prin->show();

}
then you'll be able to


void principal::goBack()
{
this -> close();
parent()->show();
}


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.

3nc31
30th November 2007, 20:21
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

:confused:

Gopala Krishna
30th November 2007, 20:35
Have you tried QWizard ?

wysota
30th November 2007, 21:23
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.

3nc31
30th November 2007, 21:39
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???

wysota
30th November 2007, 21:55
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()).

3nc31
30th November 2007, 22:13
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!!!!!!

mchara
3rd December 2007, 08:01
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

:confused:

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();

jpn
3rd December 2007, 08:55
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()