Sunday, October 12th 2008, 12:24am UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

aarelovich

Beginner

Posts: 29

Location: Argentina

1

Saturday, April 26th 2008, 2:11am

Question about communicating from windows to dialogs

Hello:

I'm doing an app that when a button is pressed on the main window it opens a new window. This second window has to execute some code when a button is pressed. What I want to do is quite simple: my main window has a public function to append text to a QTextEdit. All I want is for the second window to call that function and append text. However I don't know how to access a function of the main window from the second window.

Can anyone help me with this?

Thank you very much
  • Go to the top of the page

kunalnandi

Beginner

Posts: 21

Location: India

Occupation: Programmer

2

Wednesday, April 30th 2008, 11:22am

RE: Question about communicating from windows to dialogs

Hello,

I m not getting your problem, if possible then please post your code.. it will make ur problem more clear.. n soon u will get solution for tht..
Regards
Kunal Nandi
  • Go to the top of the page

aarelovich

Beginner

Posts: 29

Location: Argentina

3

Wednesday, April 30th 2008, 11:54am

I can't post code about my problem because I don't know how to write it. But I can try clarify my problem:

Window-1 (Main Window) Opens Window-2 (Dialog). When I press a button on Window-2 I would like to modify a TextEdit on Window-1. How can I accomplish this?

I hope that clarifies the question.

Thank you
  • Go to the top of the page

4

Wednesday, April 30th 2008, 8:23pm

RE: Question about communicating from windows to dialogs

make the QtextEdit item public or create a friend class for the second window with the first. You can then post the stuff back to the first window OR let window 1 be parent to window 2 and post items from window 2 to the parent
  • Go to the top of the page

aarelovich

Beginner

Posts: 29

Location: Argentina

5

Wednesday, April 30th 2008, 8:34pm

Quoted

make the QtextEdit item public


If I do this it's the same as making a public function in Window 1 that writes to the TextEdit. My problem is that I don't know how to access Window 1's content, the QTextEdit or the function.

Quoted

create a friend class for the second window with the first.


I'm sorry but I don't know what a friend class is.

Quoted

let window 1 be parent to window 2 and post items from window 2 to the parent


How do I do this?

Thanks
  • Go to the top of the page

6

Wednesday, April 30th 2008, 8:48pm

look up friend class in c++
http://www.cplusplus.com/doc/tutorial/inheritance.html
http://www.codersource.net/cpp_tutorial_friend.html



When you create the widget, there is a param of Parent. Set the param of parent equal to the first window

QWidget ( QWidget * parent = 0, const char * name = 0, WFlags f = 0 )


explicit QWidget::QWidget ( QWidget * parent = 0, const char * name = 0, WFlags f = 0 )
Constructs a widget which is a child of parent, with the name name and widget flags set to f.
If parent is 0, the new widget becomes a top-level window. If parent is another widget, this widget becomes a child window inside parent. The new widget is deleted when its parent is deleted.

The name is sent to the QObject constructor.

The widget flags argument, f, is normally 0, but it can be set to customize the window frame of a top-level widget (i.e. parent must be 0). To customize the frame, set the WStyle_Customize flag OR'ed with any of the Qt::WidgetFlags.

If you add a child widget to an already visible widget you must explicitly show the child to make it visible.
  • Go to the top of the page

aarelovich

Beginner

Posts: 29

Location: Argentina

7

Wednesday, April 30th 2008, 8:52pm

Thank you very much this has been most helpfull.

As soon as I can I'll try it out.
  • Go to the top of the page

kunalnandi

Beginner

Posts: 21

Location: India

Occupation: Programmer

8

Thursday, May 1st 2008, 7:27am

Hello,

Here is the solution of your problem...

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <QObject>
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QWidget>
#include <QDialog>
#include <QTextEdit>

class MainWindow : public QWidget
{
	Q_OBJECT
public:
	QTextEdit *textEdit;
	MainWindow( QWidget *parent = 0 );
	
public slots:
	void setTextInMainWindow();
};

MainWindow::MainWindow( QWidget *parent):QWidget( parent )
{
	textEdit = new QTextEdit(this);
	//QDialog *dialog = new QDialog( 0 );
}
void MainWindow::setTextInMainWindow()
{
	//MainWindow tempWin;
	textEdit->setPlainText("My Text");
	qWarning( "My Text" );
}


class MyDialog : public QDialog
{
	Q_OBJECT
public:
	QPushButton *pb;
	MyDialog( QWidget *parent = 0 );
	
//public slots:
//	void setTextInMainWindow( const QString );
};
MyDialog::MyDialog( QWidget *parent):QDialog( parent )
{
	pb = new QPushButton("&Click", this);
	//QDialog *dialog = new QDialog( 0 );
}

#include "Hello.moc"

int main( int argc, char *argv[] )
{
	QApplication app( argc, argv );
	MainWindow tempWindow;
	MyDialog dialog;

	QObject::connect( dialog.pb, SIGNAL( clicked()), &tempWindow, SLOT( setTextInMainWindow() ) ) ;
	

	dialog.show();	
	tempWindow.show();	
	return app.exec();
}



jst use this application hope this will work for u.. :)
Regards
Kunal Nandi
  • Go to the top of the page

aarelovich

Beginner

Posts: 29

Location: Argentina

9

Thursday, May 1st 2008, 11:41am

Thank you very much. I'll try to use it.
  • Go to the top of the page

Rate this thread