PDA

View Full Version : passing varying qstring value to QDialog from MAINWINDOW



mohit_king
9th February 2011, 11:59
Hi, m new to QT, i got a TEXTBROWSER in my MAINWINDOW which contains data that keeps on changing ...now i need to pass this changing data to my another TEXTBROWSER in QDIALOG....

Below is my code, using which i m only able to pass the first value of data, it's not updating itself.....HELP!

MainWindow.h


Private:
QString txtoutput; //this holds the data that keeps on updating
private slots:
void cmdOutputDialog();


MainWindow.cpp


connect(ui->buttonclicked, SIGNAL(clicked()), this, SLOT(cmdOutputDialog()));

void MainWindow::cmdOutputDialog()
{
CmdOutputDialog *dlg=new CmdOutputDialog(this);
dlg->display(txtoutput);
dlg->exec();
}


MyDialog.h


public slots:
void display(QString &text);
private:
QString m_disp;


MyDialog.cpp


void MyDialog::display(QString &text)
{
m_disp=text;
ui->textbrowser->setText(m_disp);
}

stampede
9th February 2011, 12:27
QString txtoutput; //this holds the data that keeps on updating
maybe post the update code too

mohit_king
9th February 2011, 15:32
Re: passing varying qstring value to QDialog from MAINWINDOW
Qt Code:


QString txtoutput; //this holds the data that keeps on updating

To copy to clipboard, switch view to plain text mode
maybe post the update code too


Actually I take the ffmpeg command line contents into that qstring

My code is below:


//mainwindow.h
private:
QProcess commandProcess ;

//mainwindow.cpp

QByteArray cmdoutput = commandProcess.readAllStandardOutput();
txtoutput.append(cmdoutput);

stampede
9th February 2011, 18:15
I assume that you call this once to show window:

void MainWindow::cmdOutputDialog()
{
CmdOutputDialog *dlg=new CmdOutputDialog(this);
dlg->display(txtoutput);
dlg->exec();
}
and you want the text to be constantly updated. To achieve this, you need to perform explicit update of the displayed text, the fact that you do:

txtoutput.append(cmdoutput);
not implies that string in 'dlg' will be updated, as this:

dlg->display(txtoutput);
assigns a copy of the original 'txtoutput' for display.
You need to update 'dlg' each time you receive new content:

QByteArray cmdoutput = commandProcess.readAllStandardOutput();
txtoutput.append(cmdoutput);
dlg->display(txtoutput);
So, make CmdOutputDialog object a member of your MainWindow class, or define a signal in MainWindow and connect it to 'dlg' when you create it:


CmdOutputDialog *dlg=new CmdOutputDialog(this);
connect( this, SIGNAL(textUpdate(const QString&)), dlg, SLOT(display(const QString&)) );
dlg->display(txtoutput);
dlg->exec();

//... and then when text changes:

txtoutput.append(cmdoutput);
emit textUpdate(txtoutput);

mohit_king
10th February 2011, 06:44
hey stampede very thanks for your reply.....

can you tell me when we emit the signal from mainwindow as


emit textUpdate(txtoutput);

then how do you receive that signal in CmdOutputDialog or how do you connect it......again thnx in advance...

stampede
10th February 2011, 08:22
how do you receive that signal in CmdOutputDialog or how do you connect it
Check the last code snippet from my last post, there is a connection between signal from MainWindow and CmdOutputDialog. 'display' method is declared as a slot already, so you can have signals connected to it, only thing left to do is declare the signal in MainWindow and call 'emit'.
Read Signals And Slots (http://doc.qt.nokia.com/latest/signalsandslots.html) documentation to know more about this mechanism.

mohit_king
10th February 2011, 11:10
Hey stampede thank u very much.....its working fine now....I was stuck with this problem since many days but now it is solved all due to you........you rock......:)