Results 1 to 6 of 6

Thread: setPlainText/setText simply won't run

  1. #1
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default setPlainText/setText simply won't run

    I have made a simple text editor similar to notepad, with some simple functions. I have spent the entire day making a 'load file' dialog, and when everything finally worked, "ui->textEdit->setPlainText();" started failing on me. I think this might actually be a bug, as I can't for the life of me figure out what the problem is.

    Anyway, here's the relevant code:

    SaveLoad.cpp
    Qt Code:
    1. QFile myFile(rPath);
    2. #include "mainwindow.h"
    3.  
    4. void SaveLoad::load()
    5. {
    6. qDebug() << endl << "rPath in SaveLoad.h: " << rPath;
    7.  
    8. QFile myFile(rPath);
    9. if (myFile.open(QIODevice::ReadOnly))
    10. {
    11. QTextStream textStream(&myFile);
    12. text = textStream.readAll();
    13.  
    14. myFile.close();
    15.  
    16. MainWindow obj;
    17. obj.setTextFile(text);
    18. } else{
    19. qDebug() << "The file didn't open properly";
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    Qt Code:
    1. void MainWindow::setTextFile(QString x)
    2. {
    3. qDebug() << endl << "This code runs in MainWindow.cpp" << endl << "fullText's content is:" << x << endl;
    4. ui->textEdit->setText(x);
    5. qDebug() << "This code(1) runs" << endl;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Tell me if that's not enough, then I'll post the entire code or the project file. Anyway, all the qDebugs run as they should. in the qDebug, QString x in void setTextFile(QString x) displays "Hello World!" as it should. The only problems is the line

    Qt Code:
    1. ui->textEdit->setText(x);
    To copy to clipboard, switch view to plain text mode 

    I have tried replacing x with "Hello World!" but that didn't work either so the variable is not the problem. The entire program runs without errors or issues.
    The only 'oddity' I noticed was that every variable I stored in SaveLoad.h or any variable in MainWindow.h that I tried to change from SaveLoad.cpp returned to null immediately after running the code.

    (If I had, say, QString x in MainWindow.h, and I changed it in SaveLoad.cpp using:
    Qt Code:
    1. MainWindow object;
    2. object.x = "Hello World!"
    To copy to clipboard, switch view to plain text mode 

    then qDebug() << object.x; would return "Hello World!" but if I ran qDebug() << x; in MainWindow then it would return "".
    Last edited by RandomNobodyEU; 9th June 2013 at 16:59.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,480
    Thanked 275 Times in 270 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setPlainText/setText simply won't run

    How is obj (line 17 in first code) initialised ?

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setPlainText/setText simply won't run

    Well I include MainWindow.h in SaveLoad.cpp:
    #include "mainwindow.h"

    then I create an object:
    MainWindow obj;

    and then I run the function setTextFile with parameter (text)
    obj.setTextFile(text);

    I don't think I initialized it anywhere else than in SaveLoad.cpp

    Also to add to the OP, nothing in this function runs except for qDebug:
    Qt Code:
    1. void MainWindow::setTextFile(QString x)
    2. {
    3. qDebug() << endl << "This code runs in MainWindow.cpp" << endl << "fullText's content is:" << x << endl;
    4. ui->textEdit->setText(x);
    5. qDebug() << "This code(1) runs" << endl;
    6. }
    To copy to clipboard, switch view to plain text mode 

    I tried setting a global variable equal to x but that didn't work either.

    edit: tried
    Qt Code:
    1. MainWindow *obj = new MainWindow;
    2. obj->setTextFile(text);
    To copy to clipboard, switch view to plain text mode 

    but this had the same issue
    Last edited by RandomNobodyEU; 9th June 2013 at 18:00.

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

    Default Re: setPlainText/setText simply won't run

    What is the type of ui->textEdit?
    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.


  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,480
    Thanked 275 Times in 270 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setPlainText/setText simply won't run

    OK, You create MainWindow object obj in line 16 (first source), in next line You put text on obj and then obj is destroyed because it is on stack.

  6. The following user says thank you to Lesiok for this useful post:

    RandomNobodyEU (9th June 2013)

  7. #6
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: setPlainText/setText simply won't run

    Thank you very much everyone! After 24 hours I somehow fixed it!

    I still don't know what was wrong with my initial code but I did a workaround.

Similar Threads

  1. QSound simply doesn't work
    By Momergil in forum Newbie
    Replies: 4
    Last Post: 2nd September 2011, 21:12
  2. Replies: 2
    Last Post: 23rd February 2011, 14:37
  3. QGraphicsTextItem setPlainText Memory issue
    By decin in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2010, 03:03
  4. Simply Client E-mail (POP3)
    By Altertwin in forum Qt Programming
    Replies: 5
    Last Post: 25th February 2010, 17:27
  5. Replies: 5
    Last Post: 20th June 2006, 19:40

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.