18.QT-QPlainEdit 信号与槽

时间:2022-03-23 16:16:26

QPlainEdit编辑功能

Public Slots

void appendHtml ( const QString & html )
void appendPlainText ( const QString & text )
void centerCursor ()
void clear ()
void copy ()
void cut ()
void insertPlainText ( const QString & text )
void paste ()
void redo ()
void selectAll ()
void setPlainText ( const QString & text )
void undo ()

Signals

void blockCountChanged ( int newBlockCount );
//每当按下回车或者删除回车(更新字符块),则newBlockCount计数,并触发该信号, newBlockCount 默认为1 void copyAvailable ( bool yes );
//选择某串文字时,则触发该信号,并设置yes为true,如果取消选择,也会触发该信号,设置 yes为false void cursorPositionChanged ()
////每当光标的位置发生变化时,触发该信号 void redoAvailable ( bool available );
//当文本框为空,则会触发该信号,并设置available为false,因为该文本框没有数据,所以无法重做
//当用户向空文本框输入数据时,同样也会触发该信号,设置available为true,表示可以实现重做 void selectionChanged ();
//当鼠标点击文本框时,触发该信号 void textChanged ();
//每当文档的内容发生变化时,则触发该信号,可以用来判断输入的字符是什么 void undoAvailable ( bool available );
//当用户无法撤销时,便会触发该信号,并设置available为false
//当用户修改/写入文本框内容,便会触发该信号,并设置available为true,表示可以撤销

示例代码

Widget.h:

#ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QDebug> class Widget : public QWidget
{
Q_OBJECT QPlainTextEdit edit;
QPushButton* Undo;
QPushButton* Redo;
QPushButton* Cut;
QPushButton* Copy;
QPushButton* Paste;
QPushButton* all;
QPushButton* clear; private slots:
void oncopyAvailable ( bool yes );
void onredoAvailable ( bool available );
void onundoAvailable ( bool available );
public:
explicit Widget(QWidget *parent = );
}; #endif

Widget.c:

#include "Widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
edit(this)
{
edit.setGeometry(,,,); Undo= new QPushButton("重做",this);
Redo= new QPushButton("撤销",this);
Cut= new QPushButton("剪切",this);
Copy= new QPushButton("复制",this);
Paste= new QPushButton("拷贝",this);
all= new QPushButton("全选",this);
clear= new QPushButton("删除",this);
Undo->setGeometry(,,,);
Redo->setGeometry(,,,);
Cut->setGeometry(,,,);
Copy->setGeometry(,,,);
Paste->setGeometry(,,,);
all->setGeometry(,,,);
clear->setGeometry(,,,);
Undo->setEnabled(false);
Redo->setEnabled(false);
Cut->setEnabled(false);
Copy->setEnabled(false); /*设置按键与文本框槽的关系*/
connect(Undo, SIGNAL(clicked()) , &edit ,SLOT(undo()));
connect(Redo, SIGNAL(clicked()) , &edit ,SLOT(redo()));
connect(Cut, SIGNAL(clicked()) , &edit ,SLOT(cut()));
connect(Copy, SIGNAL(clicked()) , &edit ,SLOT(copy()));
connect(Paste, SIGNAL(clicked()) , &edit ,SLOT(paste()));
connect(all, SIGNAL(clicked()) , &edit ,SLOT(selectAll()));
connect(clear, SIGNAL(clicked()) , &edit ,SLOT(clear())); /*设置文本框信号与槽函数的关系*/
connect(&edit, SIGNAL(copyAvailable(bool)) , this ,SLOT(oncopyAvailable(bool)));
connect(&edit, SIGNAL(redoAvailable(bool)) , this ,SLOT(onredoAvailable(bool)));
connect(&edit, SIGNAL(undoAvailable(bool)) , this ,SLOT(onundoAvailable(bool)));
connect(&edit, SIGNAL(selectionChanged()) , this ,SLOT(onselectionChanged())); } void Widget::oncopyAvailable ( bool yes )
{
Cut->setEnabled(yes);
Copy->setEnabled(yes);
} void Widget::onredoAvailable( bool available )
{
Redo->setEnabled(available);
} void Widget::onundoAvailable ( bool available )
{
Undo->setEnabled(available);
}

效果: 

18.QT-QPlainEdit 信号与槽