Results 1 to 12 of 12

Thread: How to get the "text" from checkbox?

  1. #1
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default How to get the "text" from checkbox?

    Hi. My english is not good, but I hope you understand me.

    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.

    Qt Code:
    1. QFile fp("test.html");
    2. fp.open(QIODevice::WriteOnly);
    3. tresc = ui->checkMocne_1->text();
    4. fp.write(tresc);
    5. fp.close();
    To copy to clipboard, switch view to plain text mode 
    Not working. :<

    I do not know how to do it, any ideas?
    Last edited by Alan_K; 18th March 2011 at 17:58.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to get the "text" from checkbox?

    You code looks correct, QCheckBox::text() should work for you.

  3. #3
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the "text" from checkbox?

    Still does not work.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get the "text" from checkbox?

    You are not writing text in the file, the open code should look like:
    Qt Code:
    1. file.open(QIODevice::WriteOnly | QIODevice::Text)
    To copy to clipboard, switch view to plain text mode 
    also you might want to check (with an if) if the file was opened successfully.
    QFile documentation

  5. #5
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the "text" from checkbox?

    Still does not work. I do not know how well I do it, can I get the text from the text label checkbox?

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get the "text" from checkbox?

    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)

  7. #7
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get the "text" from checkbox?

    Qt Code:
    1. QString str=ui->chk1->currentItem()->text();
    To copy to clipboard, switch view to plain text mode 

    I think this will work definitely>.
    Last edited by wysota; 19th March 2011 at 09:12. Reason: missing [code] tags

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to get the "text" from checkbox?

    Does the file get created? Does it contain anything? The code is definitely correct.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the "text" from checkbox?

    Okay, my errors look.



    Quote Originally Posted by Zlatomir View Post
    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 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.

  10. #10
    Join Date
    Nov 2010
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the "text" from checkbox?

    You can use QTextStream :

    Qt Code:
    1. QFile f("test.html");
    2. if (f.open(QIODevice::WriteOnly | QIODevice::Text))
    3. {
    4. QTextStream out(&f);
    5. out << ui->checkMocne_1->text();
    6. f.close();
    7. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to Octal for this useful post:

    Alan_K (19th March 2011)

  12. #11
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    19
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the "text" from checkbox?

    @Octal Working.

  13. #12
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get the "text" from checkbox?

    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.

Similar Threads

  1. Replies: 3
    Last Post: 8th December 2011, 19:21
  2. Replies: 4
    Last Post: 5th March 2010, 18:03
  3. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  4. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.