• 18.QT-QPlainEdit 信号与槽


    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 = 0);
    };
    
    #endif

    Widget.c:

    #include "Widget.h"
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        edit(this)
    {
        edit.setGeometry(0,0,280,300);
    
        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(290,0,100,30);
        Redo->setGeometry(290,40,100,30);
        Cut->setGeometry(290,80,100,30);
        Copy->setGeometry(290,120,100,30);
        Paste->setGeometry(290,160,100,30);
        all->setGeometry(290,200,100,30);
        clear->setGeometry(290,240,100,30);
        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);
    }

    效果: 

     

  • 相关阅读:
    Transact_SQL小手册
    使用泛型的 TArray 从动态数组中查找指定元素
    使用泛型的 TArray 为动态数组排序
    使用 InputBox、InputQuery 的启发
    近况汇报
    泛型排序器 TComparer
    详测 Generics Collections TList (2): First、Last、IndexOf、LastIndexOf
    详测 Generics Collections TList (1): Add、Clear、Count、Capacity
    WindowsAPI: MulDiv
    SendTextMessage 等方便的消息发送函数
  • 原文地址:https://www.cnblogs.com/lifexy/p/9003918.html
Copyright © 2020-2023  润新知