PDA

View Full Version : How to get the "text" from checkbox?



Alan_K
18th March 2011, 17:52
Hi. My english is not good, but I hope you understand me. :p

I have many checkboxes, each with a different name (checkBox_1, checkBox_2, 3, 4 etc) and different text. I want get that text and then put it in the html file. Of course, depending on whether it is selected.


QFile fp("test.html");
fp.open(QIODevice::WriteOnly);
tresc = ui->checkMocne_1->text();
fp.write(tresc);
fp.close();
Not working. :<

I do not know how to do it, any ideas? :confused:

Santosh Reddy
18th March 2011, 18:05
You code looks correct, QCheckBox::text() should work for you.

Alan_K
18th March 2011, 18:11
Still does not work. :(

Zlatomir
18th March 2011, 18:28
You are not writing text in the file, the open code should look like:

file.open(QIODevice::WriteOnly | QIODevice::Text)
also you might want to check (with an if) if the file was opened successfully.
QFile documentation (http://doc.qt.nokia.com/latest/qfile.html)

Alan_K
18th March 2011, 20:14
Still does not work. I do not know how well I do it, can I get the text from the text label checkbox?

Zlatomir
18th March 2011, 20:31
Is tresc a QString or what type is it?
And please give us more information, define "doesn't work".

You can try to print (on screen using qDebug() ) the tresc and see if that is the problem there or in other parts, also did you check if the file is opened for writing?
And if tresc is a QString then use a QTextStream to write the QString to file (see the small example in the documentation link i gave you in the previous post)

Gokulnathvc
19th March 2011, 05:26
QString str=ui->chk1->currentItem()->text();

I think this will work definitely>.

wysota
19th March 2011, 09:11
Does the file get created? Does it contain anything? The code is definitely correct.

Alan_K
19th March 2011, 13:43
Okay, my errors look. :confused:
http://i52.tinypic.com/5af1fq.png
http://i51.tinypic.com/2lm5f7b.png


Is tresc a QString or what type is it?
Yep. QString

@wysota - zauważyłem, że jesteś z Warszawy. Być może mój angielski ogranicza komunikacje :p Chodzi o to, że chcę pobrać text label danego checkboxa do zmiennej, aby następnie umieścić go w pliku html. Przykład checkbox o nazwie checMocne_1 o label: "stosuje się do norm społecznych" - no i w zależności czy checkbox jest zaznaczony chcę pobrać ten label i umieścić go w w jakimś miejscu pliku html.

Octal
19th March 2011, 13:48
You can use QTextStream :



QFile f("test.html");
if (f.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream out(&f);
out << ui->checkMocne_1->text();
f.close();
}

Alan_K
19th March 2011, 13:58
@Octal Working. :o
http://stelfoxrecruitment.files.wordpress.com/2011/02/good_job_gold_ribbon2.png

Zlatomir
19th March 2011, 14:04
Alan_K, you do realize that the answer was just in front of you?
And that the problem was not what you said it was?
And that, next time, you need to say more information about what is not really working (compile/run error, implicated code, type of variables, etc), else everything we can do is guessing.