This example should give you some idea. This example is based on Qt5, but should be trival to run using Qt4
#include <QtGui>
#include <QtCore>
#include <QtWidgets>
{
Q_OBJECT
public:
, mPrompt(prompt)
, mCount(0)
{
append(mPrompt);
}
protected:
{
if((event->key() == Qt::Key_Up) or
(event->key() == Qt::Key_Down) or
(event->key() == Qt::Key_Left) or
(event->key() == Qt::Key_Right))
{
return;
}
else if(event->key() == Qt::Key_Return)
{
append(mPrompt);
mCount = 0;
return;
}
else if(event->key() == Qt::Key_Backspace)
{
if(mCount > 0)
mCount--;
else
return;
}
else if((event->key() & Qt::Key_Escape) == Qt::Key_Escape)
{
return;
}
else
{
mCount++;
}
}
private:
unsigned int mCount;
};
int main(int argc, char ** argv)
{
TextEdit widget("comman-prompt>");
widget.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QtCore>
#include <QtWidgets>
class TextEdit : public QTextEdit
{
Q_OBJECT
public:
explicit TextEdit(const QString & prompt, QWidget * parent = 0)
: QTextEdit(parent)
, mPrompt(prompt)
, mCount(0)
{
append(mPrompt);
}
protected:
void keyPressEvent(QKeyEvent * event)
{
if((event->key() == Qt::Key_Up) or
(event->key() == Qt::Key_Down) or
(event->key() == Qt::Key_Left) or
(event->key() == Qt::Key_Right))
{
return;
}
else if(event->key() == Qt::Key_Return)
{
append(mPrompt);
mCount = 0;
return;
}
else if(event->key() == Qt::Key_Backspace)
{
if(mCount > 0)
mCount--;
else
return;
}
else if((event->key() & Qt::Key_Escape) == Qt::Key_Escape)
{
return;
}
else
{
mCount++;
}
return QTextEdit::keyPressEvent(event);
}
private:
QString mPrompt;
unsigned int mCount;
};
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
TextEdit widget("comman-prompt>");
widget.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks