4
Qt에서 프로젝트 작업을하고 있습니다. 나는 QTextEdit을 가지고 있고 신호에 textChanged() 슬롯을 연결했다. 신호가 방출 될 때 어떻게 변화를 발견 할 수 있습니까? 예를 들어, 커서 위치와 내가 쓸 때 작성된 문자를 저장하려고합니다.textChanged() 신호가 발생하면 QTextEdit가 변경됩니다.
Qt에서 프로젝트 작업을하고 있습니다. 나는 QTextEdit을 가지고 있고 신호에 textChanged() 슬롯을 연결했다. 신호가 방출 될 때 어떻게 변화를 발견 할 수 있습니까? 예를 들어, 커서 위치와 내가 쓸 때 작성된 문자를 저장하려고합니다.textChanged() 신호가 발생하면 QTextEdit가 변경됩니다.
신호가 방출 될 때 호출되는 슬롯에서 QString str = textEdit->toplainText();
으로 텍스트를 가져올 수 있습니다. 또한 이전 버전의 문자열을 저장하고 비교하여 추가 된 문자와 위치를 비교할 수 있습니다. 커서 위치에 관한
할 수 있습니다 우리 예제와 같이 QTextCurosr 클래스 :
widget.h 파일 :
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextEdit>
#include <QTextCursor>
#include <QVBoxLayout>
#include <QLabel>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private slots:
void onTextChanged();
void onCursorPositionChanged();
private:
QTextCursor m_cursor;
QVBoxLayout m_layout;
QTextEdit m_textEdit;
QLabel m_label;
};
#endif // WIDGET_H
widget.cpp 파일 :
#include "widget.h"
#include <QString>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
connect(&m_textEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
connect(&m_textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(onCursorPositionChanged()));
m_layout.addWidget(&m_textEdit);
m_layout.addWidget(&m_label);
setLayout(&m_layout);
}
Widget::~Widget()
{
}
void Widget::onTextChanged()
{
// Code that executes on text change here
}
void Widget::onCursorPositionChanged()
{
// Code that executes on cursor change here
m_cursor = m_textEdit.textCursor();
m_label.setText(QString("Position: %1").arg(m_cursor.positionInBlock()));
}
MAIN.CPP 파일 :
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
"cu rsor "는 textEdit 객체에서 읽을 수있는 텍스트 커서를 나타냅니다. –
정확히 원하는 것을 이해하지 못했습니다. 게시물을 업데이트했습니다. –