You are not logged in.

1

Thursday, September 8th 2011, 3:09pm

Window-Widget hide n show

hi,

as i m new to qt.I have to show 1 window with menu items,whenever one of the menu item triggered than hav to hide main window & show another widget with small functionality.
I m able to open another widget n hiding main window, but unable to show back the main window after exiting widget.
i m using 2 classes one for main window & another for widget class.

m attaching the code.

ParentChild1.cpp

ParentChild1::ParentChild1(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
QPixmap logo(":/Touch/onity_logo.png");
label_2->setPixmap(logo);
//SecondWindow *newWind = new SecondWindow;
//newWind->show();
connect(actionTS_Test,SIGNAL(triggered()),this,SLOT(hide()));
connect(actionTS_Test,SIGNAL(triggered()),this,SLOT(tsTest()));

}

void ParentChild1::tsTest()
{
SecondWindow *newWind = new SecondWindow;
newWind->show();
}


ParentChild1.h

class ParentChild1 : public QMainWindow,Ui::Man_test {
Q_OBJECT
public:
ParentChild1(QWidget *parent = 0);
~ParentChild1();

protected:
//void mousePressEvent(QMouseEvent *event);
void changeEvent(QEvent *e);

public slots:
void tsTest();
void back2Main();

private:
//Ui::Man_test *ui;
};

SecondWindow.cpp

SecondWindow::SecondWindow()
{
setupUi(this);
setGeometry(0, 0,480,272);
//ParentChild1 *parChild = new ParentChld1;
//connect(pushButton, SIGNAL(clicked()), parChild,SLOT(show()));
//connect(this,SIGNAL(destroyed),ParentChild1,SLOT(show()));
}

SecondWindow.h

class SecondWindow : public QWidget,Ui::Form
{
public:
SecondWindow();
public slots:
//void back2Main();
protected:
void mousePressEvent(QMouseEvent *event);

private slots:
void on_pushButton_clicked();
};


Main.cpp

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ParentChild1 w;
w.show();
a.setActiveWindow(&w);
//a.setMainWidget(&a);
//a.connect(&w,SIGNAL())
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(show()));
//a.connect(&a, SIGNAL(lastWindowClosed()),&a,SLOT(quit()));
return a.exec();
}

i m using 2 ui form from designer I think that is not required.

Thanx in advance,

2

Thursday, September 8th 2011, 7:05pm

Source code

1
2
3
4
5
void ParentChild1::tsTest()
{
SecondWindow *newWind = new SecondWindow;
newWind->show();
}


this make a new child every single time. You shouldnt be doing that.

In the ctor for the parent, make a child window and pass a pointer to the parent into the child window (new ChildClass)

illustrative (ie not proper c++)

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Parent
{
public:
Parent
{
m_child = new Child(this);
connect( buttonpress -> func);
}

private:

void func()
{
// hide this & show m_child
hide();
m_child->show();
}

Child* m_child;
};


Source code

1
2
3
4
5
6
7
8
9
10
11
class Child
{
explicit Child(QWidget* parent)
: m_parent(parent)
{
connect( Child::hide() -> parent::show) );
}

private:
QWidget* m_parent; 
};
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Monday, September 12th 2011, 9:09am

Window-Widget hide n show

Hi Amleto,

very first thanks 4 replying. n the Child code which u sent me


in this source code
explicit Child(QWidget* parent)
: m_parent(parent)

my parent is window class so shall i make it like

explicit Child(QWindow* parent)
: m_parent(parent)
private:
QWindow* m_parent;

n want to know is there any options by which i can know the destroy signal of child widget b'coz in our case there are two options one is after pressing button child widget we hav to enter into mainwindow, n another is after closing child window.
button is working well but that closing window closes the entire application.

can u suggest me the way of this problem

thanks

4

Monday, September 12th 2011, 7:41pm

QWidget* is more flexible - any QWindow is a QWidget, but not vice versa. If you only want QWindow to be a parent then you may use that.

In order to stop the app closing yo umay need to intercept the closeevent (http://doc.qt.nokia.com/stable/qcloseevent.html) in the child window. In the handler, show the main window and then hide the child. If the child is actually being destroyed, you should ignore() the event so the child is not actually destroyed. If the child is destroyed, then the pointer in the parent window is no longer valid and you will get access violation next time you try to show it (the child wont be at the end of the pointer anymore).
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

5

Wednesday, September 14th 2011, 4:43am

Thanks Amleto,

dont we have any chatting community where we can find immediate solution of our problem rather than waiting 12-24 hours.