Results 1 to 4 of 4

Thread: What am I doing wrong??

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question What am I doing wrong??

    Ok this is the (relevant) code i have:

    quiz.cpp
    Qt Code:
    1. void Quiz::on_NextButton_clicked()
    2. {
    3. QuizDialog question;
    4. question.myfile.open("C:\\questions.txt");
    5. if(question.myfile.is_open()){
    6. question.next_question();
    7. }
    8.  
    9. void Quiz::next_question(){
    10. while(!(line=="<EOQ>")){
    11. getline(myfile, line);
    12. if(line == "<QUESTION_TEXT>"){
    13. getline(myfile, line);
    14. ui->QuestionLabel->setText(line.c_str());
    15. qDebug() << "Question: " << line.c_str();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    quiz.h
    Qt Code:
    1. #ifndef QUIZ
    2. #define QUIZ
    3.  
    4. #include <iostream>
    5. #include <fstream>
    6. #include <string>
    7. #include <QDialog>
    8. using namespace std;
    9.  
    10. namespace Ui {
    11. class Quiz;
    12. }
    13.  
    14. class Quiz : public QDialog
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit Quiz(QWidget *parent = 0);
    20. ~Quiz();
    21. ifstream myfile;
    22. string line;
    23. void next_question();
    24.  
    25. private:
    26. Ui::Quiz *ui;
    27.  
    28.  
    29. private slots:
    30. void on_NextButton_clicked();
    31. };
    To copy to clipboard, switch view to plain text mode 


    Ok so here's the problem. The file is opening fine. The function 'next_question();' then runs and "qDebug() << "Question: " << line.c_str();" does output the relevant information to the debugger.

    However in the line above this, 'ui->QuestionLabel->setText(line.c_str());' i have asked for the caption on my form to be set to whatever the current value of line is. For some reason this does not seem to work. I know that the file is being access and I know that the correct line is being found. However I cannot output this information to the form. Could someone please explain what's up??


    Thanks for your time and trouble.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: What am I doing wrong??

    Hi,

    I guess, that because you call question.next_question(); in a loop, the last time line is empty and thus all other previous found strings are deleted since the label would only show the last one. (Or what exactly is the output of your program?)

    Further since it seems you are using a XML file, you could have a look at QDomDocument.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: What am I doing wrong??

    It is not at all obvious where "myfile" used in Quiz::next_question(), that is Quiz::myfile, is opened. QuizDialog::myfile, as used in on_NextButton_clicked(), is not related to Quiz::myfile.

    In on_NextButton_clicked() the local variable "question" and its members (including one called myfile) ceases to exist at the end of the method. Typically destruction of a stream of file object would close the stream/file. We cannot be certain what is happening here because we don't know what type QuizDialog::myfile is.

  4. #4
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: What am I doing wrong??

    Thanks for the help... I think I have managed a work around. I'm now storing each line as a string and then outputting them to the ui once the next_question() function has finished.

    Thanks for your time and trouble

Similar Threads

  1. What do I do wrong
    By Arif Bilgin in forum Newbie
    Replies: 12
    Last Post: 20th October 2010, 20:03
  2. i don't know what i do wrong..
    By Hardstyle in forum Newbie
    Replies: 2
    Last Post: 27th June 2010, 17:33
  3. what is wrong with my qt ?
    By lwb422 in forum Qt Programming
    Replies: 5
    Last Post: 12th April 2010, 14:07
  4. What's wrong??
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2008, 08:07
  5. Help please - what am I doing wrong?
    By Jimmy2775 in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2006, 22:06

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.