18.QT-QPlainEdit 信号与槽
生活随笔
收集整理的這篇文章主要介紹了
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 默認為1void copyAvailable ( bool yes ); //選擇某串文字時,則觸發該信號,并設置yes為true,如果取消選擇,也會觸發該信號,設置 yes為falsevoid 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_OBJECTQPlainTextEdit 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); }效果:?
?
?
?
?
轉載于:https://www.cnblogs.com/lifexy/p/9003918.html
總結
以上是生活随笔為你收集整理的18.QT-QPlainEdit 信号与槽的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adb重启或关机手机命令
- 下一篇: 编写Arduino支持的C++类库