#include "shelldemo.h"
#include <QLineEdit>
#include <QTextBlock>
#include <QDebug>
#include <QPlainTextEdit>
//#include <private/qplaintextedit_p.h>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <private/v8.h>
#endif
ShellDemo::ShellDemo(QWidget *parent) :
QPlainTextEdit(parent)
{
setReadOnly(true);
QFont font = this->font();
font.setPointSize(font.pointSize()+2);
this->setFont(font);
appendPlainText(">>> ");
edit = new QLineEdit(this->viewport());
edit->setStyleSheet("border-style:none; background-color:transparent;");
connect(edit, SIGNAL(returnPressed()), SLOT(onEditFinished()));
//connect(verticalScrollBar(), SIGNAL(valueChanged(int)), SLOT(onScrollBarValueChanged()));
}
void ShellDemo::resizeEvent(QResizeEvent *e)
{
updateEditPosition();
}
void ShellDemo::onScrollBarValueChanged()
{
updateEditPosition();
}
QString ShellDemo::runCommand(const QString &cmd)
{
return QString("Result of %1").arg(cmd);
}
void ShellDemo::onEditFinished()
{
QString cmd = edit->text();
if (cmd.isEmpty()) {
return;
}
moveCursor(QTextCursor::End);
insertPlainText(cmd);
//edit->hide();
edit->clear();
appendPlainText(runCommand(cmd));
appendPlainText(">>> ");
updateEditPosition();
edit->show();
edit->setFocus();
}
void ShellDemo::updateEditPosition()
{
QPlainTextEditPrivate *d = reinterpret_cast<QPlainTextEditPrivate*>(qGetPtrHelper(d_ptr));
//QRectF rect = d->control->blockBoundingRect(d->control->document()->lastBlock()); //无法通过????
//edit->move(rect.topRight().toPoint());
//edit->resize(viewport()->size().width(), edit->size().height());
}
按照 http://blog.csdn.net/dbzhang800/article/details/6751775 的方法在qt4.85环境下无法编译通过
报错信息是
错误:invalid use of undefined type ‘struct QPlainTextEditPrivate’
/usr/local/Trolltech/Qt-4.8.5/include/QtGui/qplaintextedit.h:65: 错误:forward declaration of ‘struct QPlainTextEditPrivate’
就是出在 QRectF rect = d->control->blockBoundingRect(d->control->document()->lastBlock()) 这一句 求高人解释 急求!
------解决方案--------------------
QPlainTextEditPrivate?是类QPlainTextEdit的私有成员部分的封装。
是为了做出好的封装库,让库的使用者在头文件中看不到私有成员的一种方法。
这样的好处,库更新后只要公有成员没动,头文件是不用动的。
也就是说你在写代码的时候最好不要用QPlainTextEditPrivate
------解决方案--------------------
对,不要用任何Qt库中的private