Results 1 to 17 of 17

Thread: QTextEdit -> add Text

  1. #1
    Join Date
    Dec 2007
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QTextEdit -> add Text

    Hello

    i am new to Qt itself and playing around with the examples.
    At the moment i am trying to build a small RS232 flash application.

    The idea was to have a scrollable TextField which should be used to log each action
    forced by the user.

    So i have added a QTextedit (scrollable and offers the option to make it readonly...great)
    Now my problem is as follows:

    Each time i am trying to add new text to that QTextEdit the complete application crashes.

    i.e. in the OpenFile Dialog (dummy Style righrt now)
    Qt Code:
    1. void MainWindow::openFileDialog()
    2. {
    3. // Dummy File select dialog
    4. // Method 1: Limit location and limit file-types
    5. QString fileName = QFileDialog::getOpenFileName(this, tr("Select Firmware"),
    6. "/",
    7. tr("All (*.*)"));
    8.  
    9. // Now we want to add an entry into our logging QTextEdit at the Main window)
    10. // this idea results in a crash
    11. loggingBox->setText("open file dialog was selected")
    12. //
    13. // Trying something diffrent ....just clearing the text inside Logging Box
    14. // this idea results in a crash too
    15. loggingBox->clear();
    16. }
    To copy to clipboard, switch view to plain text mode 


    in the main section....how i call the function above
    Qt Code:
    1. selectFileButton = new QPushButton(handleFlashBox); // new QPushButton
    2. selectFileButton->setGeometry(QRect(12,25,100,20));
    3. selectFileButton->setStatusTip(QString::fromUtf8("Press Select to choose the new firmware."));
    4. selectFileButton->setToolTip(QString::fromUtf8("Press SELECT to choose the new firmware."));
    5. connect(selectFileButton, SIGNAL(clicked()), this, SLOT(openFileDialog()));
    To copy to clipboard, switch view to plain text mode 

    Any idea what i am doing wrong ?

    do i have to save the existing text in a string variable first, then add the new text-part to that string and then insert the string-content to my loggingBox maybe ?

    Based on a search in the mailinglist someone posted this idea:
    Qt Code:
    1. textedit->setText( textedit->text() + "some more text" );
    2. // in my case
    3. loggingBox->setText( loggingBox->text() + "new text");
    4. // but seems like QTextEdit has no member named 'text'
    To copy to clipboard, switch view to plain text mode 

    Best regards
    ape
    Last edited by ape; 18th December 2007 at 10:02.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextEdit -> add Text

    Are there any extra threads involved? Could you run the application via debugger and paste backtrace?
    J-P Nurmi

  3. #3
    Join Date
    Dec 2007
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextEdit -> add Text

    Hello JPN,

    thanks for the fast reply
    basicly i have no idea howto debug that.

    the function openFileDialog() is posted as it is in my code.

    Could you provide my howto debug & backtrace on a Windows host.
    I am using Qt4.3.2 on Windows Vista
    My IDE of choice right now is HaiQ



    edit: if i use the clear() function which results in the app crashing Windows Debug information is as follows:
    Qt Code:
    1. Problemsignatur:
    2. Problemeventname: APPCRASH
    3. Applicationname: myexename.exe
    4. Applicationversion: 0.0.0.0
    5. ApplicationTimestamp: 47679bb6
    6. Erroromodulename: QtGui4.dll
    7. Errormodulversion: 4.3.2.0
    8. Errormodultimestamp: 4701090f
    9. ExceptionCode: c0000005
    10. Exceptionoffset: 004a24f9
    11. OS Version: 6.0.6000.2.0.0.256.6
    12. .....
    To copy to clipboard, switch view to plain text mode 
    Last edited by ape; 18th December 2007 at 10:12.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextEdit -> add Text

    Try downloading and installing GDB for MinGW. Compile your application in debug mode (you should also have debug version of Qt installed):
    make clean
    qmake -config debug
    make
    Then, launch the application via gdb, run the app, make it crash, show the backtrace:

    gdb myapp.exe
    (gdb) run
    ...
    Program received signal SIGSEGV, Segmentation fault.
    ...
    (gdb) bt
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    ape (18th December 2007)

  6. #5
    Join Date
    Dec 2007
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextEdit -> add Text

    ok gonna try that.

    so basicly both ideas:


    // Now we want to add an entry into our logging QTextEdit at the Main window)
    // this idea results in a crash
    loggingBox->setText("open file dialog was selected")
    // Trying something diffrent ....just clearing the text inside Logging Box
    // this idea results in a crash too
    loggingBox->clear();



    should work, right ?

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextEdit -> add Text

    Well, let's say it shouldn't crash. Don't expect to see text changing on the fly, though. You will have to let the application to process its events to see changes.

    Qt Code:
    1. {
    2. // changing text will schedule a paint event...
    3. textEdit->setText(...);
    4. // ...but the text edit has no chance to update its content before it gets cleared
    5. textEdit->clear();
    6. }
    To copy to clipboard, switch view to plain text mode 
    One easy solution is to delay clearing a bit, and return back to the event loop from the slot of yours for a while:
    Qt Code:
    1. {
    2. textEdit->setText(...);
    3. // give text edit a chance to update its content before clearing it
    4. QTimer::singleShot(500, textEdit, SLOT(clear()));
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. #7
    Join Date
    Dec 2007
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextEdit -> add Text

    Hi again

    ok basicly i did a small mistake while explaning my code:

    Those 2 lines where just examples:
    ---
    textEdit->setText(...);
    textEdit->clear();
    ---

    that means: i havent tried to use them both at once....
    Only each-cmd as single command.
    And in both cases the application does crash.

    Thats why i was wondering whats wrong about my code in general.

    At the moment i am switching my environment (to Ecplise etc...) and
    gonna try a real debug then. Lets see if it acts similar in the new env.

    best regards and thank you for the input till now

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit -> add Text

    Where do you instantiate that text edit?
    I think it might be uninitialized.

  10. #9
    Join Date
    Dec 2007
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextEdit -> add Text

    Hello Marcel

    inside my mainwindow.cpp i have the following
    Qt Code:
    1. // Logging
    2. QTextEdit* loggingBox = new QTextEdit(this);
    3. loggingBox->setText( "* Application started.\n* Logging enabled.\n" );
    4. loggingBox->setMinimumSize(490, 50);
    5. loggingBox->setMaximumSize(490, 50);
    6. loggingBox->setBaseSize(490,50);
    7. loggingBox->setReadOnly(false);
    8. loggingBox->setToolTip(QString::fromUtf8("This window shows all log-entries."));
    To copy to clipboard, switch view to plain text mode 

    The Box is displays at application launch, displays the predefined text and i guess it is implemented as supposed (ok as a beginner i am never 100% sure)

    Edit: And that was the mistake hehe
    It works like that:
    Qt Code:
    1. // Logging
    2. loggingBox = new QTextEdit(this);
    3. loggingBox->setText( "* Application started.\n* Logging enabled.\n" );
    4. loggingBox->setMinimumSize(490, 50);
    5. loggingBox->setMaximumSize(490, 50);
    6. loggingBox->setBaseSize(490,50);
    7. loggingBox->setReadOnly(false);
    8. loggingBox->setToolTip(QString::fromUtf8("This window shows all log-entries."));
    To copy to clipboard, switch view to plain text mode 



    Ok to get it finaly working as supposed i need to do a last change i case (regarding that specific point)

    At the moment i am just replacing the text of my logging box if the function is called like that:
    Qt Code:
    1. loggingBox->setText("new text");
    To copy to clipboard, switch view to plain text mode 

    Basicly i want to add the new text to the existing one
    Is there something like addText or similar or how can i solve that issue in a better way ?
    Last edited by ape; 18th December 2007 at 15:28.

  11. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextEdit -> add Text

    Good that the problem solved, but I still suggest learning how to get a backtrace out of a debugger. You would have seen yourself from the backtrace that QTextEdit::setText() or QTextEdit::clear() was called via null/uninitialized pointer.
    J-P Nurmi

  12. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit -> add Text

    Yeah, I was just gonna say that. You created the text box somewhere in a function, locally and got destroyed.

    For appending text: QTextEdit::append()

  13. #12
    Join Date
    Dec 2007
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextEdit -> add Text

    hi again

    yes, a working debugger makes sense for sure.
    unfortunaly i messed half of my day with setting up Eclipse, Qt, MinGW etc and finaly switched back to my old setup as eclipse looked kinda special regarding the setup.

    Maybe i should start tomorrow again searching for a good howto setup Eclipse, MinGw, Qt for full debug mode.

    Next problem is: i am working as default windows user, which means i have to re-edit all right etc..... Not the best situation.


    Regarding my loggingBox:
    Any final idea howto add the text to the existing one ? as using setText just replaces the text.


    best regards
    ape



    Finally: big big thanks for you 2 guys. I can imagine it is hard to help starting-users like me if you know the language yourself fora long time.
    Hopefully i will not ask too much in the near future.
    Last edited by ape; 18th December 2007 at 15:40.

  14. #13
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextEdit -> add Text

    Quote Originally Posted by ape View Post
    Regarding my loggingBox:
    Any final idea howto add the text to the existing one ? as using setText just replaces the text.
    Search QTextEdit docs for "append".
    J-P Nurmi

  15. #14
    Join Date
    Dec 2007
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextEdit -> add Text

    regarding append:
    i already noticed the hint above...guess we were all answering & asking at the same time.....ended up in a bit messed up thread. Sorry about that.


    Ok one final question regarding my QTextEdit:

    I remember during my first test with QTextEdit the box displayed a scrollbar if it was needed.

    I "think" i havent added this function
    (to add a scrollbar if amount lof lines > amount of visible lines in the QTextEdit) myself.

    Actually as i am now able to insert Text into that box i am realizing that i dont have that anymore.

    So the question:
    Is it the default behaviour, that a scrollbar appears ? or do i have to define that manually ?

    at the moment i define just the following
    Qt Code:
    1. // Logging
    2. loggingBox = new QTextEdit(this);
    3. loggingBox->setText( "* Application started.\n* Logging enabled." );
    4. loggingBox->setMinimumSize(490, 50);
    5. loggingBox->setMaximumSize(490, 50);
    6. loggingBox->setBaseSize(490,50);
    7. loggingBox->setReadOnly(true);
    8. loggingBox->setToolTip(QString::fromUtf8("This window shows all log-entries."));
    To copy to clipboard, switch view to plain text mode 


    A quick yes/no should be enough, as i have to train my using qt-docs skill

    Best regards
    ape

  16. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextEdit -> add Text

    Yes, a scroll bar should appear by default as needed.

    PS. You can extremely easily verify it with only a few lines of code:
    Qt Code:
    1. // main.cpp
    2. #include <QtGui>
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication a(argc, argv);
    6. t.show();
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Then just type in the magic combo
    qmake -project
    qmake
    make
    ./app
    ...and start typing to see whether a scroll bar appears. In the end it will take less time to test such things than to post a question on the forums.
    J-P Nurmi

  17. #16
    Join Date
    Dec 2007
    Posts
    33
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextEdit -> add Text

    true

    But then again i am asking myself, why my current QTextEdit no longer displays its scrollbars.

    Could that be related with my layout and my defined size of the object itself ?


    EDIT: solved
    i have to damn myself. Actually i am working with a fix window size (which was changed lately) and the QTextEdit had a fix sized too....which i forgot to change too during that resizing action in my source.


    guess best thing is not to work with fixed sizes, if i am not forced too.


    Thanks again to all helping people inhere.
    Last edited by ape; 19th December 2007 at 14:49.

  18. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextEdit -> add Text

    Quote Originally Posted by ape View Post
    guess best thing is not to work with fixed sizes, if i am not forced too.
    Definitely. Not everyone has same resolution. A fixed size of a window/control might be completely inappropriate for somebody else. Fixed sizes will also make localization unnecessarily hard. Imagine that same widget should have different textual content depending on the chosen language. Very easy with flexible layouts, a pain with fixed sizes.

    Thanks again to all helping people inhere.
    You're welcome.
    J-P Nurmi

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. Replies: 3
    Last Post: 20th November 2007, 07:03
  4. How to get text of last line only in QTextEdit?
    By rajesh in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2006, 13:37
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

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.