PDA

View Full Version : QTextEdit -> add Text



ape
18th December 2007, 09:47
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)


void MainWindow::openFileDialog()
{
// Dummy File select dialog
// Method 1: Limit location and limit file-types
QString fileName = QFileDialog::getOpenFileName(this, tr("Select Firmware"),
"/",
tr("All (*.*)"));

// 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();
}



in the main section....how i call the function above


selectFileButton = new QPushButton(handleFlashBox); // new QPushButton
selectFileButton->setGeometry(QRect(12,25,100,20));
selectFileButton->setStatusTip(QString::fromUtf8("Press Select to choose the new firmware."));
selectFileButton->setToolTip(QString::fromUtf8("Press SELECT to choose the new firmware."));
connect(selectFileButton, SIGNAL(clicked()), this, SLOT(openFileDialog()));


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:


textedit->setText( textedit->text() + "some more text" );
// in my case
loggingBox->setText( loggingBox->text() + "new text");
// but seems like QTextEdit has no member named 'text'


Best regards
ape

jpn
18th December 2007, 10:02
Are there any extra threads involved? Could you run the application via debugger and paste backtrace?

ape
18th December 2007, 10:05
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:


Problemsignatur:
Problemeventname: APPCRASH
Applicationname: myexename.exe
Applicationversion: 0.0.0.0
ApplicationTimestamp: 47679bb6
Erroromodulename: QtGui4.dll
Errormodulversion: 4.3.2.0
Errormodultimestamp: 4701090f
ExceptionCode: c0000005
Exceptionoffset: 004a24f9
OS Version: 6.0.6000.2.0.0.256.6
.....

jpn
18th December 2007, 10:16
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

ape
18th December 2007, 10:27
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 ?

jpn
18th December 2007, 10:45
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.



{
// changing text will schedule a paint event...
textEdit->setText(...);
// ...but the text edit has no chance to update its content before it gets cleared
textEdit->clear();
}

One easy solution is to delay clearing a bit, and return back to the event loop from the slot of yours for a while:


{
textEdit->setText(...);
// give text edit a chance to update its content before clearing it
QTimer::singleShot(500, textEdit, SLOT(clear()));
}

ape
18th December 2007, 11:58
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

marcel
18th December 2007, 12:02
Where do you instantiate that text edit?
I think it might be uninitialized.

ape
18th December 2007, 15:22
Hello Marcel

inside my mainwindow.cpp i have the following


// Logging
QTextEdit* loggingBox = new QTextEdit(this);
loggingBox->setText( "* Application started.\n* Logging enabled.\n" );
loggingBox->setMinimumSize(490, 50);
loggingBox->setMaximumSize(490, 50);
loggingBox->setBaseSize(490,50);
loggingBox->setReadOnly(false);
loggingBox->setToolTip(QString::fromUtf8("This window shows all log-entries."));


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:


// Logging
loggingBox = new QTextEdit(this);
loggingBox->setText( "* Application started.\n* Logging enabled.\n" );
loggingBox->setMinimumSize(490, 50);
loggingBox->setMaximumSize(490, 50);
loggingBox->setBaseSize(490,50);
loggingBox->setReadOnly(false);
loggingBox->setToolTip(QString::fromUtf8("This window shows all log-entries."));




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:


loggingBox->setText("new text");


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 ?

jpn
18th December 2007, 15:27
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. ;)

marcel
18th December 2007, 15:30
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()

ape
18th December 2007, 15:32
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.

jpn
18th December 2007, 16:08
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". :)

ape
19th December 2007, 11:01
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


// Logging
loggingBox = new QTextEdit(this);
loggingBox->setText( "* Application started.\n* Logging enabled." );
loggingBox->setMinimumSize(490, 50);
loggingBox->setMaximumSize(490, 50);
loggingBox->setBaseSize(490,50);
loggingBox->setReadOnly(true);
loggingBox->setToolTip(QString::fromUtf8("This window shows all log-entries."));



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

Best regards
ape

jpn
19th December 2007, 11:52
Yes, a scroll bar should appear by default as needed.

PS. You can extremely easily verify it with only a few lines of code:


// main.cpp
#include <QtGui>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QTextEdit t;
t.show();
return a.exec();
}

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. ;)

ape
19th December 2007, 14:45
true :D

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.

jpn
19th December 2007, 14:59
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. :)