Overview
I've been writing a file system recently.
The infrastructure has been written for a while, and it's basically the same. Also need to use QT to do a shell interactive program interface. Refer to the shells of mac, linux and windows, all of which are operated in a text box. And
in
this text, only the bottom line can be entered. The problem is:
How to set part of the text read-only in qtextedit?
Ideas:
Follow us: Cursor! If the position of the cursor changes (not the bottom line), then set the text to read-only (setReadOnly(true)); otherwise (setReadOnly(false));
the following key is how to track it?
Use the signal-slot mechanism.
connect( myTextEdit, SIGNAL(cursorPositionChanged()), SLOT(on_textEdit_cursorPositionChanged()) );
The cursorPositionChanged() system here already exists. And
on_textEdit_cursorPositionChanged()
needs to be written by itself:
write the function under widget:
void Widget::on_textEdit_cursorPositionChanged()
{
//当前光标
qDebug()<<"coming"<<endl; //可以看到行号随着光标的改变而改变
QTextCursor tc = myTextEdit->textCursor();
QTextLayout *pLayout = tc.block().layout();
//当前光标在本BLOCK内的相对位置
int nCurpos = tc.position() - tc.block().position();
int nTextline = pLayout->lineForTextPosition(nCurpos).lineNumber() + tc.block().firstLineNumber();
qDebug()<<nTextline<<endl; //可以看到行号随着光标的改变而改变
}
and documented in the header file.
It should also be noted that a lot of people have reported slot loss errors:
Object::connect: No such slot (QT槽丢失问题)
This is because your slot function is not placed under it
private slot:
The complete code is given below:
code
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(300,300);
w.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextEdit>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
private:
QTextEdit *myTextEdit;
int max_line;
private slots:
void on_textEdit_cursorPositionChanged();
};
#endif
widget.cpp
#include "widget.h"
#include <QTextLayout>
#include <QTextBlock>
#include <QDebug>
void Widget::on_textEdit_cursorPositionChanged()
{
//当前光标
qDebug()<<"coming"<<endl; //可以看到行号随着光标的改变而改变
QTextCursor tc = myTextEdit->textCursor();
QTextLayout *pLayout = tc.block().layout();
//当前光标在本BLOCK内的相对位置
int nCurpos = tc.position() - tc.block().position();
int nTextline = pLayout->lineForTextPosition(nCurpos).lineNumber() + tc.block().firstLineNumber();
qDebug()<<nTextline<<endl; //可以看到行号随着光标的改变而改变
}
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
setWindowTitle(tr("Viverrine-guest"));
myTextEdit = new QTextEdit(this);
myTextEdit->setFocus(); //得到鼠標
myTextEdit->setText(tr("這是一段text內容:\n內容通常很多......")); //設定內容文字
myTextEdit->setGeometry(0,0,550,330); //設定位置大小
qDebug()<<"nTextline"<<endl; //可以看到行号随着光标的改变而改变
connect( myTextEdit, SIGNAL(cursorPositionChanged()), SLOT(on_textEdit_cursorPositionChanged()) );
}
You can see the number of lines printed. Here read-only and writable can be written in additionally.