QTextEdit , send messgae to Application
-
I am trying to send some text from QTextEdit to standard output when the enter key is pressed,.
But the text is not getting displayed in both text box and output and the enter is also does not seem to be detected.whats wrong in the code?
following the code
MainWidget.cpp@
#include "MainWidget.h"
#include<QString>
QString str;
MainWidget::MainWidget()
{
QLayout *layout = new QVBoxLayout();
cmdWin = new CommandWindow;
this->setLayout(layout);
layout->addWidget(cmdWin);
}CommandWindow::CommandWindow()
{
str = this->toPlainText();
}void CommandWindow:: read()
{
qDebug()<< "str" <<str;
}void CommandWindow::keyPressEvent(QKeyEvent *ke)
{
qDebug()<<"in KeyPress";if(ke->key()== Qt::Key_Enter) { qDebug()<<"read is pressed"; read(); }
}
@main.cpp
@
#include "MainWidget.h"int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qDebug()<< "Main Window:";
MainWidget mainWidget;
mainWidget.show();return app.exec();
}
@
MainWidget.h
@
#ifndef MAINWIDGET_H
#define MAINWIDGET_H#include<QApplication>
#include <QtDebug>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QKeyEvent>class CommandWindow;
class MainWidget : public QWidget
{
Q_OBJECT
public:
MainWidget();
private:
CommandWindow *cmdWin;};
class CommandWindow : public QTextEdit
{
public:
CommandWindow();
void read();
private:
QTextEdit *textbox;
protected:
void keyPressEvent(QKeyEvent *ke);};
#endif // MAINWIDGET_H@
-
There is a ReturnPressed event. Have you tried it?
-
Have you run this in debug and stepped through the code to see if your getting a key event when enter is pressed at least?
Ive always used:
@
if (event->key()== Qt::Key_Enter || event->key() == Qt::Key_Return){
// do something...
}
@or you could use an event filter and check if the event is a key event using installEventFilter(true) and using the eventFilter function...then check the keys from there (will most likely be a lot slower though since your going to be picking up event event in the form).
-
Thanks for suggestion, it was detecting only one enter key before ,now after adding "Qt::Key_Return" detecting both.
But still there is a problem.
when i press any key that is not displayed in the textwindow and str does not contain the keys that i entered.
below is the debug sample from gdb. -
@
(gdb) p str
$1 = {static null = {<No data fields>}, static shared_null = {ref = {
_q_value = 0}, alloc = 0, size = 0, data = 0x0, clean = 0,
simpletext = 0, righttoleft = 0, asciiCache = 0, capacity = 0,
reserved = 0, array = {0}}, static shared_empty = {ref = {_q_value = 1},
alloc = 0, size = 0, data = 0x12, clean = 0, simpletext = 0,
righttoleft = 0, asciiCache = 0, capacity = 0, reserved = 0, array = {0}},
d = 0x0, static codecForCStrings = 0x0}
(gdb) r
Starting program: /home/skylab/exercise/Text/Text
[Thread debugging using libthread_db enabled]
[New Thread 0xb578ab70 (LWP 2040)]
[New Thread 0xb4dffb70 (LWP 2041)]
Main Window:Breakpoint 1, MainWidget::MainWidget (this=0xbffff284) at MainWidget.cpp:7
7 cmdWin = new CommandWindow;
(gdb) s
CommandWindow::CommandWindow (this=0xbffff284) at MainWidget.cpp:12
12 CommandWindow::CommandWindow()
(gdb) s
14 str = this->toPlainText();
(gdb) p str
$2 = {static null = {<No data fields>}, static shared_null = {ref = {
_q_value = 138}, alloc = 0, size = 0, data = 0x804e232, clean = 0,
simpletext = 0, righttoleft = 0, asciiCache = 0, capacity = 0,
reserved = 0, array = {0}}, static shared_empty = {ref = {_q_value = 2},
alloc = 0, size = 0, data = 0xb74e709e, clean = 0, simpletext = 0,
righttoleft = 0, asciiCache = 0, capacity = 0, reserved = 0, array = {0}},
d = 0x804e220, static codecForCStrings = 0x0}
(gdb) s
toPlainText (this=<optimized out>)
at /usr/local/qt4/include/QtGui/qtextedit.h:190
190 { return document()->toPlainText(); }
(gdb) s
[Thread 0xb578ab70 (LWP 2040) exited]
in KeyPress 74
Enter 16777221
in KeyPress 75
Enter 16777221
in KeyPress 74
Enter 16777221
in KeyPress 16777220
Enter 16777221
read is pressed
str ""@
-
I have added " setText("Hello"); " in below function , now the str is cntaining "Hello", i observed that the cursor is not moving when i type some characters, any idea whats the problem.?
@CommandWindow::CommandWindow()
{
setText("Hello");
str = this->toPlainText();
}@ -
Hi Dcqt. In your keyPressEvent, if you want the widget to process the captured key, you should call the superclass' implementation of the handler. Otherwise, your keys are eaten discarded.
-
Hi Skh1002,
I really dont understand what is superclass, can you give some sample or reference please...
-
I'm sorry to have confused you, I used Objective-C terminology. Well, the basic key press processing is implemented in your QTextEdit parent class and although you do your own custom processing by reimplementing the keyPressEvent, you still want that basic processing to happen. However, since you have reimplemented (overloaded) the handler method, the original one is no longer called. The only way to to that is to manually call the QTextEdit::keyPressEvent() in your implementation. Something like this:
@
void CommandWindow::keyPressEvent(QKeyEvent *ke)
{
qDebug()<<"in KeyPress";QTextEdit::keyPressEvent(ke); if(ke->key()== Qt::Key_Enter) { qDebug()<<"read is pressed"; read(); }
}
@Edit: I also think that you need to call your read() method after the default processing of the key press. I edited the code to reflect that. Also, in your read() method you send the string to output, but you never seem to update it. I guess, it should be updated every time your widget registers a key press, that is inside your keyPressEvent().
-
Hi,
I managed to do it with keyPressEvent.
my next task is to make the QTextEdit as a terminal where we enter some commands.(custom).i need to show a promt there like
@
pmt>
@where i enter commands.
i tried with QTextEdit::setText("pmt>") and QTextEdit::appened("pmt>").
when i press backspace it is removing the promt, how will i control my cursor not to remove the promt.
also how will i retain the previously entered commands
is there any example or reference would be help full -
I think you've got the basic set-up for it. You are already filtering key presses. A logical extension would be to check for the Backspace key as well, but to keep the previously entered commands and the prompt in the widget, you may need to filter out cursor movement keys and count characters entered by the user on the current line and eat the Backspace key (by not calling the parent's keyPressEvent() implementation) if this count is at zero.
I can't think of a good example right now, but it does not seem to be very hard to implement. By the way, if you don't need rich text capabilities, you may want to use QPlainTextEdit instead.
-
Hi Skh1002,
I try to use it the following way but i am not able stop the control from eating the character .
how to stop the control if the length is 4 and key is backspace.The promt length is 4
@ Pmt> @@
bool eventFilter(QObject (obj, QEvent *eve)
{
if(eve->type()==QEvent::KeyPress){
QKeyEvent keve = static_cast<QKeyEvent>(eve);
if(cmd->toPlainText().length()==4 && keve->key()==Qt::Key_Backspace ){
qDebug()<<"backspcae pressed";
return 0;
}
.....
}@
-
Hi Dcqt,
Unfortunately, I could not respond earlier and I don't know whether or not you still need any help. But for the record, first of all, you do not have to install an event filter if you only need to register key presses. The keyPressEvent() should be enough. If you need to process other events as well, though, you may need to reimplement eventFilter().If you do it with the eventFilter(), you need to indicate to the calling framework that you want to stop processing the captured event by returning true. In that case, it will be discarded. If you return false (or zero, as you do in your snippet), then the event will be propagated further and processed by your widget normally.
-
Thanks Skh1002 for the reply.
I have solved all my problems and i used KeyPressEvent and handled all the keys required.
for the prompt i used cursor.position and compared with the length required.Thanks for returning again.
-
bool eventFilter( QObject *dist, QEvent *event )
{
if( event->type() == QEvent::KeyPress )
{
.......
//Here "dist" is required object, i.e, TextEdit etc.
}