PDA

View Full Version : How to print[display] some text on a dialog?



untamed
29th May 2007, 10:24
hello all

Could anybody please give me a code snippet which has a text input field and a button. If we enter some text into the text field and press the button, it should pop a new window with the same text inside. Urgent. please. :(

I am using Qt 4.2.2 on windows.

thanks in advance.

Michiel
29th May 2007, 10:34
Have you looked at the QMessageBox class? Maybe even one of the static functions (like QMessageBox::information()) is enough.

high_flyer
29th May 2007, 11:19
@untamed:
you should know that these kind of requests are not seen favorably on forums such as this one.
We gladly help, but you should show that you are trying to solve your problem your self.
So, show us what it is you have tried, and where exaclty your problem leis.
We will help you then to solve your specific problem.

Michiel
29th May 2007, 14:33
In general, you are right. You don't ask someone else to write code for you.

But I can understand that if you're in a real hurry, you don't have time to do much research yourself. (Though you shouldn't disguise every question as such. :p)

This forum is not here for nothing. And he did post in the newbie section. You should just point him in the right direction (as I did). The Qt library is quite big. I've been known to ask for directions myself.

high_flyer
29th May 2007, 14:45
but what exactly did you answer to in my post?
Yes, this forum is not here for nothing, but also not for people to get thier "home work" done for them.
Even if someone is in a hurry, he/she should at least TRY SOMETHING.
Being in a hurry is no reason not to do ANYTHING.

Since the question is SO general, I could not see where to forward him to.
You suggested QMessageBox.
That is a valid oppinion, but I see no reason to bring QMessageBox up at all.
If anything then:
QDialog,QPushBUtton,QTextEdit and general reading about signals/slots.
There - is that better? ;)

Michiel
29th May 2007, 19:38
Sure. You should know how these things work. But if you quickly want to display a message in a popup window, that's what QMessageBox is for. Why reinvent the wheel?

high_flyer
29th May 2007, 23:35
Why reinvent the wheel?
because he wanted something which you can't do with a QMessageBox (unless you subclass it) - QMessageBox has no input fields.

Could anybody please give me a code snippet which has a text input field and a button. If we enter some text into the text field and press the button, it should pop a new window with the same text inside.

Michiel
30th May 2007, 00:35
Yes, of course. I was just assuming (for some reason) that the popup-window was the thing he had trouble with. Not the textfield and the button. :p Mea culpa.

untamed
5th June 2007, 06:24
I am really sorry to have asked such a question. :(
I was in a hurry then, and i was thinking like if I happen to get some code, I can read and understand from that. I dint mean anybody to do my homework.

hmm, I am quite new into this thing and even C++ too.

So, I have created the first dialog with a text input filed and two buttons, ok and cancel. I havent used designer. I want to pop another dialog[or an alert] with the text copied from the first dialog's text input field when OK button is pressed. I know that i can pass the text through a string literal. How do i create that second dialog, thats my issue now. Will QMessageBox do?

QMessageBox doesnt take inputs?
I got this info from the documentation:
QMessageBox::information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )

so, I think the third field, const QString & text can be used to pass the text. Am I correct?
so I can avoid creating a second dialog. My application[the actual assignment given] requires the use of alertboxes more, and if QMessageBox can take variables[strings] as inputs, it will be easy. Could anybody please clarify on this?

Thank you. :)

untamed
5th June 2007, 08:54
I think this chunk of code is the core:
void textdialog::pop()
{
QString text = textedit->toHtml();

QMessageBox::StandardButton ok;
ok = QMessageBox::information(this, tr("Popped!"), tr("<p>You had entered:</p>""<p>%1</p>") .arg(text));
}

QMessageBox can take inputs.
when the OK button is pressed, it calls the pop() function.
:)

Thanks everyone, I did it. Thanx Michiel for the understanding you showed. Thanks a lot to Sunil.Thaha (http://www.qtcentre.org/forum/u-sunilthaha-25.html) for his patience when i disturbed him with silly doubts and for guiding me through each n every step. :)

untamed
5th June 2007, 08:57
I think this chunk of code is the core:

void textdialog::pop()
{
QString text = textedit->toHtml();

QMessageBox::StandardButton ok;
ok = QMessageBox::information(this, tr("Popped!"), tr("<p>You had entered:</p>""<p>%1</p>") .arg(text));
}

QMessageBox can take inputs.
when the OK button is pressed, it calls the pop() function.
:)

Thanks everyone, I did it. Thanx Michiel for the understanding you showed. Thanks to high_flyer for challenging me ;).
Thanks a lot to Sunil.Thaha (http://www.qtcentre.org/forum/u-sunilthaha-25.html) for his patience when i disturbed him with silly doubts and for guiding me through each n every step. :)

high_flyer
5th June 2007, 09:06
QMessageBox can take inputs.
No it can't.
The FUNCTION (in this case a static function) takes "input" parameters.
But not the DIALOG.
Its not the same.
The input came from the QTextEdit widget, which you passed to the message box as a parameter.


Thanks everyone, I did it. Thanx Michiel for the understanding you showed. Thanks to high_flyer for challenging me .
Thanks a lot to Sunil.Thaha for his patience when i disturbed him with silly doubts and for guiding me through each n every step.
Looks like end titels of a movie... or an oscar winner speach :p