当前位置: 首页 > news >正文

建设管理网站网站如何做百度实名认证

建设管理网站,网站如何做百度实名认证,h5页面制作效果图,wordpress幻灯片回收站在哪里【Qt】QTextEdit/QPlainTextEdit 实现 Tab 键多行缩进与反缩进 文章目录 I - 主要原理II - 代码实现2.1 - 自定义类2.2 - 实现 Tab 缩进2.3 - 实现反缩进 III - 参考链接 I - 主要原理 由于 QTextEdit 和 QPlainTextEdit #xff0c;都无法实现多行选中缩进与反缩进#xff… 【Qt】QTextEdit/QPlainTextEdit 实现 Tab 键多行缩进与反缩进 文章目录 I - 主要原理II - 代码实现2.1 - 自定义类2.2 - 实现 Tab 缩进2.3 - 实现反缩进 III - 参考链接 I - 主要原理 由于 QTextEdit 和 QPlainTextEdit 都无法实现多行选中缩进与反缩进选中多行后按下缩进或反缩进选中文本都会清空并替换为(反)缩进或空格。因此会造成使用很不方便和低效的问题。 需要实现一个类继承自 QTextEdit 或 QPlainTextEdit 类并重写其 keyPressEvent 接口处理 Tab 按键 II - 代码实现 2.1 - 自定义类 自定义类 TextEdit视使用目的继承 QTextEdit 或 QPlainTextEdit #include QTextEdit // 或 #include QPlainTextEditclass TextEdit : public QTextEdit // 或 class TextEdit : public QPlainTextEdit {Q_OBJECT public:explicit TextEdit(QWidget* parent nullptr);protected:void keyPressEvent(QKeyEvent *e) override; // 键盘事件// 添加 override 编译时检查是否重写父类函数防止敲错 private:// 根据是否保留制表符设置为 \t 或空格QString m_indentation \t; // 或 }2.2 - 实现 Tab 缩进 需要包含头文件 #include QTextCursor // 获取光标实例 #include QTextBlock // 文本块void TextEdit::keyPressEvent(QKeyEvent* e) {// 判断 Tab 按键 以及排除 Shift, Ctrl, Alt 等控制按键的情况因为通常通过 Shift Tab 实现反缩进if (Qt::Key_Tab Qt::NoModifier e-modifiers()){// 获取光标实例QTextCursor cursor textCursor();// 在无选中的情况下仅插入一个缩进并返回if (!cursor.hasSelection()){insertPlainText(m_indentation);return;} // 记录光标选中内容的开始和结束位置 spos(start position), epos(end position)int spos cursor.anchor();int epos cursor.position();// 在由下向上选中时交换开始和结束位置if(spos epos){int hold spos;spos epos;epos hold;}// 获取开始文本块序号cursor.setPosition(spos, QTextCursor::MoveAnchor);int sblock curs.block().blockNumber();// 获取结束文本块序号cursor.setPosition(epos, QTextCursor::MoveAnchor);int eblock curs.block().blockNumber();// 开始处理文本缩进cursor.setPosition(spos, QTextCursor::MoveAnchor);// QTextCursor 在编辑文本块时需要调用此方法cursor.beginEditBlock();// 依次遍历所选中的文本块并在起始处插入缩进for(int i 0; i (eblock - sblock); i){cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);cursor.insertText(m_indentation);cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor);}// 编辑文本块结束cursor.endEditBlock();// 将光标的选择设置为跨越所有涉及的行也就是说保留之前内容的选中状态并加入缩进为选中内容。// 将光标移动至开始位置cursor.setPosition(spos, QTextCursor::MoveAnchor);cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);while(cursor.block().blockNumber() eblock){// 使用 QTextCursor::KeepAnchor 为选中文本cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor);}// 移动至最后一个文本块结束位置cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);// 编辑器设置此光标结束setTextCursor(curs);} // end if }2.3 - 实现反缩进 反缩进的实现较缩进难实现需要知道待反缩进的每一个文本行开头有多少个缩进位置以及不足时如何处理。 int TextEditor::GetIndentationSpaces(const QString blockText) {int indentationSpaceCount 0;// 遍历此文本块的每一个字符检查是否包含空格或制表符for (int i 0; i blockText.size() QString(\t ).contains(blockText[i]); i){// 若为空格if ( blockText[i]){indentationSpaceCount;}else // 若为制表符 \t{indentationSpaceCount tabStopDistance() / fontMetrics().averageCharWidth();}}return indentationSpaceCount; }继续在 keyPressEvent 接口中实现 Shift Tab 反缩进或者也可以使用 Qt::Key_BackTab 枚举这里使用与缩进处理文本不同的方式 void TextEdit::keyPressEvent(QKeyEvent* e) {// ...else if (Qt::Key_BackTab e-key()){// 获取光标实例QTextCursor cursor textCursor();// 没有选中内容时去除当前行的一个缩进并返回if (!cursor.hasSelection()){int spaceCount std::min(GetIndentationSpaces(cursor.block().text(), m_indentation.size());cursor.movePosition(QTextCursor::StartOfLine); // movePosition 第二个参数缺省值为 QTextCursor::MoveMode::MoveAnchor// 从行起始处删除当前的一个缩进长度cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, spaceCount);cursor.removeSelectedText();return;}// 记录光标选中内容的开始和结束位置 int startPos cursor.anchor();int endPos cursor.position();// 在由下向上选中时交换开始和结束位置if(startPos endPos){std::swap(startPos, endPos);}// 若选中内容未选中首行的全部内容更新开始位置cursor.setPosition(startPos, QTextCursor::MoveAnchor);cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);startPos cursor.position()// 重新选中curosr.setPosition(endPos, QTextCursor::MoveAnchor); cursor.setPosition(startPos);cursor.setPosition(endPos, QTextCursor::KeepAnchor);QString content cursor.selection().toPlainText();QStringList list content.split(\n);// 减少开销int sz list.size();for (int 0; i sz; i){int spaceCount GetIndentationSpaces(list[i]);spaceCount std::min(spaceCount, m_indentation.size());// 若该行无缩进则跳过if (0 spaceCount){continue;}// 截断list[i] list[i].mid(spaceCount); }// 文本还原content list.join(\n);// 移除选中文本替换为去除缩进的文本cursor.removeSelectedText();cursor.insertText(content);// 重新使用光标选中文本保证可连续反缩进cursor.setPosition(startPos);cursor.setPosition(startPos() content.size(), QTextCursor::KeepAnchor);setTextCursor(cursor);return;} }最后不要忘了 调用父类的keyPressEvent 以保证不影响其他的键盘事件 QTextEdit::keyPressEvent(e); // 或 QPlainTextEdit::keyPressEvent(e);III - 参考链接 https://www.qtcentre.org/threads/33582-indent-selection-in-QPlainTextEdit https://codereview.stackexchange.com/questions/33899/qplaintextedit-subclass-function-to-indent-lines-in-selection
http://www.hkea.cn/news/14342176/

相关文章:

  • 服务器2003怎么做网站网易企业邮箱彻底删除的邮件还能恢复吗
  • 做外贸网站推广什么比较好站长工具手机综合查询
  • 大学生兼职网网站建设计划书西安市市政建设网站
  • 瀑布流网站有哪些wordpress怎么加站点图标
  • 织梦怎么做网站地图成品网站和模板建站
  • 网站开发付款方式和比例自助建站网
  • 如何创建网站内容在线排名优化工具
  • 崇明网站建设如何做网站里的子网站
  • 用国外服务器做网站网站搜资源的搜索引擎
  • 适合用struts2做的网站网络营销策略是什么
  • 做电影网站的资源从哪里换邢台做网站哪家便宜
  • php做网站速成软件开发一天收费多少
  • 广州好的做网站公司做贷款的网站有哪些
  • 网站收录提交入口网站建设要什么软件
  • 做网站商城需要什么条件低调赚大钱的灰色行业
  • 响应式网站有什么好处中文 wordpress插件下载
  • 品牌网站建设黑白H狼做靓号网站
  • 做本地分类信息网站赚钱吗国外产品展示网站源码
  • 优秀的图片设计网站推荐wordpress网站布局
  • 长春做个人网站做不了class wp wordpress
  • 法治建设网站模块阳澄湖大闸蟹网站建设
  • 免费网站建设一级深圳做网站排名哪家好
  • 怎么做网站端口代理帝国cms 网站名称
  • 网站建设内页徐州建设工程招标公示
  • 腾讯网qq网站做网站多少流量可以做广告
  • 零陵做网站国内主流网站开发技术
  • 做广告在哪个网站做效果人流最多适合手机的网站
  • 知道一个网站怎么知道是谁做的百度优化中软属于国企还是央企
  • 广东品牌网站建设平台wordpress qqworld
  • 专升本需要考些什么科目百度seo公司整站优化软件