PDA

View Full Version : passing date from a dialog to another one



erdomester
23rd April 2010, 19:29
Hello,

I have two dialogs. In the first one (testprogram) i can get data from the other one (dialog2). So when dialog2 pops up and returns a value, 'testprogram' dialog can use it. But now I want to reverse it. I want some data of 'testprogram' appear in the dateEdit object of 'dialog2'.


void dialog2::dialog2_addbuttonClicked()
{
testprogram dialog;
if (dialog.exec() == QDialog::Accepted)
{
m_ui->dialog2_dateEdit->setDate(dialog.adddatestring(QDate));
}
}

Error in the if... row: 'class testprogram' has no member named 'exec'

I don't want to open the 'testprogram again, i was trying with these lines as these work fine in the other situation.
To make myself more understandable:

1. user launches program, 'testprogram' dialog opens.
2. user sets a date in the dateEdit object, then pushes a button.
3. That push makes 'dialog2' appear and in the dateEdit (or lineEdit, whatever) appears the date that user had set in the 'tesprogram' dialog.

I am stuck at this, any help is appreciated.

Luc4
23rd April 2010, 20:38
mmh... if I'm understanding correctly... I would just write some code in dialog2 which takes the value you need from testprogram. Let's say dateEdit is public, you may put in the constructor of dialog2 something like:

dateEdit.setText(testprogram.dateEdit.text);
I used setText and text, but you can use the correct method provided by the control you're using. Is this what you needed?

erdomester
23rd April 2010, 21:56
Thanks for the answer. I created an image as demonstration:
http://img707.imageshack.us/img707/2637/dialogo.png
Green arrow: When I click on the add button, the task text gets into the tablewidget of the larger (i called it 'testprogram') window.
Red arrow: When I change the date in the dateEdit i want that date to appear in the dateEdit of the smaller (I called it 'dialog2') dialog.

Your code seems strange. I use arrows instead of points, but i want to sg like you wrote, i just don't know how to write the proper command.

I wrote this: m_ui->dialog2_dateEdit->setText(?)

If I wrote m_ui->dialog2_dateEdit instead of your "dateEdit", what shall i wrote instead of your "testprogram.dateEdit.text"? I don't know how to refer to the testprogram dialog.

Luc4
24th April 2010, 01:07
In fact you should have a reference to the first dialog. I don't know how you created dialog2, but it is possible to set a parent for that dialog, and that could be testprogram. In this case, you may write in testprogram something like this:

dialog2 myDialog2(this);
myDialog2.exec();
this means you can refer to testprogram in dialog2 more or less as:

m_ui->dialog2_dateEdit->setText(((testprogram*)this->parent())->dateEdit->text());
Obviously the code is only to give an idea.

erdomester
24th April 2010, 07:50
At first I created a Qt4 Gui Application, with Mainwindow.->testprogram
Then I created a Qt Designer Form Class -> dialog2

When I click on the addbutton on the 'testprogram' dialog it opens the 'dialog2' dialog. Some basic code:

testprogram.cpp

void testprogram::addClicked()
{
dialog2 dialog;
if (dialog.exec() == QDialog::Accepted)
{
// here I get the data from dialog2 and put it into an textEdit. .e.g with a plaintext:
QString taskdescrp = dialog.adddescription();
ui->textEdit->setPlainText(taskdescrp);
}
else
{
return;
}
}

dialog2.h


class dialog2 : public QDialog {
Q_OBJECT
public:
dialog2(QWidget *parent = 0);
~dialog2();
QString adddescription();
public slots:
void dialog2_addbuttonClicked();


dialog2.cpp


void dialog2::dialog2_addbuttonClicked()
{
QString taskdesc = m_ui->dialog2_textEdit->toPlainText();
accept();
}

QString dialog2::adddescription()
{
return m_ui->dialog2_textEdit->toPlainText();
}


I just don't know how to do it reverse.
I think i should put something like you suggested into this part of the dialog2.cpp:



dialog2::dialog2(QWidget *parent) :
QDialog(parent),
m_ui(new Ui::dialog2)
{
m_ui->setupUi(this);
connect(m_ui->dialog2_addButton, SIGNAL(clicked()), this, SLOT(dialog2_addbuttonClicked()));
m_ui->dialog2_lineEdit2->setText(((testprogram*)this->parent())->dateEdit->text());
}


And I created this in the testprogram.cpp also:


QString testprogram::adddatestring()
{
return ui->lineEdit4->text(); //i want this to be passed to a lineEdit of dialog2
}


I hope you see something...

Luc4
25th April 2010, 02:32
Does this code work as expected? What I see is that you didn't pass the parent to the dialog2 instance in line 3 of testprogram.cpp. If dialog2 doesn't have a reference to its parent, then line 7 of dialog2.cpp cannot get the text.

erdomester
25th April 2010, 10:36
I created a small file for this, and uploaded it to here:

http://uploading.com/files/7deb9863/testprogram.rar/

I hope this is much more understandable.

what is working:
when program launches, click on the From button, then write sg into lineEdit near "Send to tesprogram" then click Send, and it appears in the lineEdit near "From dialog2".

what is not working:
I write sg to the lineEdit near "To dialog2" then click on the From button, then when i click on the Get button, the content of that lineEdit should appear in the lineEdit near "Get from testprogram".
(addbutton is not in use)

Luc4
25th April 2010, 14:10
Using this works:

m_ui->dialog2_lineEdit->setText(((testprogram*)this->parent())->addtext());