PDA

View Full Version : Parent and Child Window Communication



Harini
9th November 2013, 10:39
As i'm trying to create a connection b/w parent and child window facing a problem with the following errors & warnings


/home/stesalit/Desktop/tasks/multi-windw-back/info.cpp:8: error: invalid use of incomplete type ‘struct Ui::Info’
/home/stesalit/Desktop/tasks/multi-windw-back/info.h:7: error: forward declaration of ‘struct Ui::Info’

/home/stesalit/Desktop/tasks/multi-windw-back/info.cpp:14: warning: possible problem detected in invocation of delete operator:


Here are my .h and .cpp files in attachments.Can any one please help me in sorting out the issue????
:confused:

anda_skoa
9th November 2013, 14:48
Ui::Info should have been define in ui_info.h

This is a generated file, its source file should be info.ui
Check that the form declared in it is called Info.

Btw, posting screenshots of code is suboptimal. either attach the project as a ZIP or post the code in code tags

Oh and it helps if a thread's subject is actually about the problem :-)

Cheers,
_

Harini
11th November 2013, 07:57
Thanks a lot for ur suggestion :) AS I'm new to Qt,I dont know how to define Ui::Info in ui_info.h :confused: can u please explain me...

anda_skoa
11th November 2013, 10:54
I assume you have created the files using "New File" and selected "Designer Form Class".
That will usually generate the files and proper names.

My guess is that you have a Form file called info.ui but that you have changed the name of the top level item in it.
If so you can either change it back to "Info" or change the usage of "Info" in info.h and info.cpp to whatever name you have now chosen.

Cheers,
_

Harini
22nd November 2013, 12:30
Thank you sir @ anda_skoa

But here I'm getting only parent window am not getting any child.There is no communication b/w parent and child. Here is my code can please suggest me anything :confused:



#ifndef INFO_H
#define INFO_H

#include <QMainWindow>

namespace Ui {
class info;
}

class info : public QMainWindow {
Q_OBJECT
public:
info(QWidget *parent = 0);
~info();

protected:
void changeEvent(QEvent *e);

private:
Ui::info *ui;
};


#endif // INFO_H



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <info.h>
#include <QtGui>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

private:
Ui::MainWindow *ui;
void setSignals();
void process();
};

#endif // MAINWINDOW_H



#include <QtGui/QApplication>
#include "info.h"
#include "ui_info.h"

info::info(QWidget *parent) :
QMainWindow(parent)
//ui(new Ui::info)
{
ui->setupUi(this);
}

info::~info()
{
delete ui;
}

void info::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}




#include <QtGui/QApplication>
#include "mainwindow.h"
#include "info.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setSignals();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::setSignals(){
connect(ui->next,SIGNAL(clicked()),this,SLOT(process()));
//connect(ui->close,SIGNAL(clicked()),this,SLOT(close()));
}

void MainWindow::process(){
info *i;
i = new info;
this -> hide();
i -> show();
}

anda_skoa
22nd November 2013, 18:26
This probably not compiling (missing include of info.h in the main window source file, and probably crashing because info's source file does not create an instance of the UI class (commented out) but accesses it.

However, MainWindow::process() looks right though.

Do you get any runtime warning?

Cheers,
_

Harini
23rd November 2013, 08:34
@ anda_skoa

Thanks a lot for ur reply :)


I dint get any error or warning and now I included info.h in main window source file but still facing the same problem.

Harini
23rd November 2013, 08:54
Hii every one :)

I want to create a parent and child window. From the parent window i want to call the child window by using signal and slots so here in the ui form i want to customize the slot editor with process().
Here is my signal and slot connection code line which I want to do...


connect(ui->next,SIGNAL(clicked()),this,SLOT(process()));

How can i do this?? can any one please help me???

toufic.dbouk
23rd November 2013, 11:46
You add your intended slot in the header file where the slot will be invoked under public sots.

class nameQueryThread ...
{

public:
nameQueryThread(...);
...
signals:

public slots:
void process();
};


From the parent window i want to call the child window by using signal and slots
What do you mean ?
In your slot process create and call the * child window *
in your cpp file

void window::process()
{
// do your code here
// create and call your child window
}

anda_skoa
23rd November 2013, 12:37
Can you attach the project as ZIP file?

Cheers,
_

Harini
25th November 2013, 06:24
@ anda_skoa


yea sure :) here is my zip file in attachments :) please solve it :)

Harini
25th November 2013, 10:47
@ toufic.dbouk

Thank you :)


Here is my project in zip file can u please check it out and suggest me something if i have done any mistake???

saman_artorious
25th November 2013, 11:11
You have defined process() as a private member in your MainWindow class again. instead define it as a public slots as below:


class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();

protected:
void changeEvent(QEvent *e);

public slots:
void process();
private:
Ui::MainWindow *ui;
void setSignals();
};

Harini
25th November 2013, 11:44
@ saman_artorious


Thank you for u'r suggestion :)


I did as u suggested but still I'm not getting the process slot in the signal and slot area of the form.please help me.....

saman_artorious
25th November 2013, 12:53
@ saman_artorious


Thank you for u'r suggestion :)


I did as u suggested but still I'm not getting the process slot in the signal and slot area of the form.please help me.....


I am checking your code, it is possible to do that:


void MainWindow::setSignals(){
connect(ui->next,SIGNAL(clicked()),this,SLOT(process()));


so what is the problem!? you have access to your SLOT inside connect. (don't forget to include QObject)

toufic.dbouk
25th November 2013, 14:40
Your code is okay.
your application crashes upon clicking the next button because you are creating an info object and showing it but in your info.cpp you dont initailse the UI.

info::info(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::info)
{
ui->setupUi(this);
}.

The slot process() gets executed upon clicking next so your connect statement works.
Now do whatever you want in that slot.
Something like:

void MainWindow::process()
{
// this->hide(); /*if you wanna use the hide function you have to take care of info's parent.*/
info *i = new info(this);
i->show();
}


Good Luck.

anda_skoa
25th November 2013, 19:14
MainWindow::process() is a slot, but it is not declared in a slots section



private slots:
void process();


Which of course the application tells you when you start it.

In programming it is generally a good idea to fix errors that are reported by the build and the program.

Cheers,
_

anda_skoa
25th November 2013, 19:29
Oh great, I just wasted time on a double poster.

Merging thread as an educational example and making sure the perpertators is recognized as such.

Harini
26th November 2013, 07:31
@ anda_skoa

Sorry sir ,I dint get u :)

toufic.dbouk
26th November 2013, 08:07
My previous post shows how to solve the problem. And as mentioned above by anda_skoa your slot process() is declared in the header file not as a Slot. You should declare it under the public or private slots.


Good Luck.

Harini
26th November 2013, 08:56
@ toufic.dbouk

I declared it under the public slot but still i dint get it.


public slots:
// void on_next_clicked();
void process();

toufic.dbouk
26th November 2013, 09:07
...but still i dint get it.
What do you mean ? What is it that you dont get ? Whats the problem that your facing ?

Harini
26th November 2013, 10:16
@ toufic.dbouk

Actually, in my application when i click the next button i have to get the child window open but, here in this case I'm not getting any child window and in the ui form i'm unable to access the process slot in the signal and slot area.

Added after 15 minutes:

In the child window I have a button-back when i click the button control must go to parent window. How can I do this ???
can anyone please help me???

anda_skoa
26th November 2013, 21:28
Have you fixed the bug that crashes your program?

Do you get any more warnings at build or runtime?

If both your windows are QMainWindows, why don't you just use a single one and change its content?

Do I have to add more question marks???

Cheers,
_

toufic.dbouk
26th November 2013, 22:23
Actually, in my application when i click the next button i have to get the child window open but, here in this case I'm not getting any child window and in the ui form i'm unable to access the process slot in the signal and slot area.
We already went over this issue and solved it , i actually posted a code too to help you.
Fix that as anda_skoa mentioned and come back to the thread if you need any further help.

Good Luck.

Harini
27th November 2013, 07:00
@anda_skoa

Every thing are cleared no warnings and no errors but,my superior ask me to do the application so. I need two Qmainwindows here and by clicking back it have to move to parent window

@ toufic.dbouk

yea it got cleared sir but i need help for the following one...
In the child window I have a button-back when i click the button control must go to parent window.

toufic.dbouk
27th November 2013, 13:18
Do the same as you did with the next button ?

anda_skoa
27th November 2013, 19:29
Or define a "backClicked()" signal in info.h and connect the back buttons clicked() signal to it. Then connect the info's backClicked() signal in mainwindow.cpp to a slot that hides the info instance and re-shows the main window.

Cheers,
_

toufic.dbouk
27th November 2013, 20:32
@ anda_skoa I was saving this reply for the next post of how to do "Do the same as you did with the next button ?" :P
Eventually that's how it will be done, i cant think of any other simple way to do such thing.
Any other options around ?

I have a question, when the info instance is created in the mainwindow.cpp , should it have mainwindow as a parent ?

i = new info;
i->show();
i->setFocus();
or

i = new info(this);
i->show();
i->setFocus();

Harini
29th November 2013, 08:25
Or define a "backClicked()" signal in info.h and connect the back buttons clicked() signal to it. Then connect the info's backClicked() signal in mainwindow.cpp to a slot that hides the info instance and re-shows the main window.

Cheers,
_


Hello!! :)

I did as u suggested but, getting the following error...

error: ‘class Ui::info’ has no member named ‘back’

toufic.dbouk
29th November 2013, 09:56
error: ‘class Ui::info’ has no member named ‘back’
Thats because you dont have a button named back. Back is the name of your button that you defined. You have to use proper naming in your code. Change back to whatever name you gave to your button that should go back from mainwindo to another.

Good Luck.

Harini
29th November 2013, 10:22
I have a button named back....but,still its raising an error.

toufic.dbouk
29th November 2013, 10:41
Show some code where the error is occurring.

anda_skoa
30th November 2013, 01:31
I have a question, when the info instance is created in the mainwindow.cpp , should it have mainwindow as a parent ?


I would say this depends on the intention of info. If it is a "sub window" of MainWindow, i.e. its lifetime is restricted to the life time of MainWindow, then yes.
If it is another main window, i.e. more like a "sibling" then no.


Cheers,
_