创建一个QApplication对象,用于管理应用程序资源,它对于任何使用了Qt Widgets的程序都必要的。对于没有使用Qt Widgets 的GUI应用,可以使用QGuiApplication代替。

QApplication::exec() 进入事件循环。Qt应用运行时,会产生事件并被发送到应用的widgets。事件举例:鼠标点击和键盘输入。

更多相关阅读:

https://doc.qt.io/qt-5/application-windows.html

https://doc.qt.io/qt-5/eventsandfilters.html

打开文件发生错误时,发出警告信息:

  1. if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
  2. QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
  3. return;
  4. }

获取文件全部字符:

  1. setWindowTitle(fileName);
  2. QTextStream in(&file);
  3. QString text = in.readAll();

保存全部字符到文件

  1. QFile file(fileName);
  2. if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
  3. QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
  4. return;
  5. }
  6. setWindowTitle(fileName);
  7. QTextStream out(&file);
  8. QString text = ui->textEdit->toPlainText();
  9. out << text;
  10. file.close();

打印支持:

  1. qtHaveModule(printsupport): QT += printsupport
  2. requires(qtConfig(fontdialog))
  1. #if defined(QT_PRINTSUPPORT_LIB)
  2. #include <QtPrintSupport/qtprintsupportglobal.h>
  3. #if QT_CONFIG(printer)
  4. #if QT_CONFIG(printdialog)
  5. #include <QPrintDialog>
  6. #endif // QT_CONFIG(printdialog)
  7. #include <QPrinter>
  8. #endif // QT_CONFIG(printer)
  9. #endif // QT_PRINTSUPPORT_LIB
  10.  
  11. ...
  12.  
  13. void Notepad::print()
  14. {
  15. #if QT_CONFIG(printer)
  16. QPrinter printDev;
  17. #if QT_CONFIG(printdialog)
  18. QPrintDialog dialog(&printDev, this);
  19. if (dialog.exec() == QDialog::Rejected)
  20. return;
  21. #endif // QT_CONFIG(printdialog)
  22. ui->textEdit->print(&printDev);
  23. #endif // QT_CONFIG(printer)
  24. }

复制、剪切、粘贴

  1. void Notepad::copy()
  2. {
  3. #if QT_CONFIG(clipboard)
  4. ui->textEdit->copy();
  5. #endif
  6. }
  7.  
  8. void Notepad::cut()
  9. {
  10. #if QT_CONFIG(clipboard)
  11. ui->textEdit->cut();
  12. #endif
  13. }
  14.  
  15. void Notepad::paste()
  16. {
  17. #if QT_CONFIG(clipboard)
  18. ui->textEdit->paste();
  19. #endif
  20. }

撤销和重做

  1. void Notepad::undo()
  2. {
  3. ui->textEdit->undo();
  4. }
  5.  
  6. void Notepad::redo()
  7. {
  8. ui->textEdit->redo();
  9. }

设置字体和斜体/加粗等,设置斜体/加粗时,可以只对选中文字生效:

  1. void Notepad::selectFont()
  2. {
  3. bool fontSelected;
  4. QFont font = QFontDialog::getFont(&fontSelected, this);
  5. if (fontSelected)
  6. ui->textEdit->setFont(font);
  7. }
  8.  
  9. void Notepad::setFontUnderline(bool underline)
  10. {
  11. ui->textEdit->setFontUnderline(underline);
  12. }
  13.  
  14. void Notepad::setFontItalic(bool italic)
  15. {
  16. ui->textEdit->setFontItalic(italic);
  17. }
  18.  
  19. void Notepad::setFontBold(bool bold)
  20. {
  21. bold ? ui->textEdit->setFontWeight(QFont::Bold) :
  22. ui->textEdit->setFontWeight(QFont::Normal);
  23. }

关于对话框:

  1. void Notepad::about()
  2. {
  3. QMessageBox::about(this, tr("About MDI"),
  4. tr("The <b>Notepad</b> example demonstrates how to code a basic "
  5. "text editor using QtWidgets"));
  6.  
  7. }

【Qt官方例程学习笔记】Getting Started Programming with Qt Widgets的更多相关文章

  1. 【Qt官方例程学习笔记】Application Example(构成界面/QAction/退出时询问保存/用户偏好载入和保存/文本文件的载入和保存/QCommandLineParser解析运行参数)

    The Application example shows how to implement a standard GUI application with menus, toolbars, and ...

  2. 【Qt官方例程学习笔记】Raster Window Example(画笔的平移/旋转/缩放应用)

    这个例子显示了如何使用QPainter渲染一个简单的QWindow. 值得学习的内容 <QtGui>头文件 #include <QtGui>就可以使用Qt GUI模块中的所有类 ...

  3. 【Qt官方例程学习笔记】Analog Clock Window Example (画笔的平移/旋转/缩放应用)

    这个例子演示了如何使用QPainter的转换和缩放特性来简化绘图. 值得学习的: 定时器事件ID检查: 在定时器事件中检查定时器id是比较好的实践. QPainter抗锯齿: We call QPai ...

  4. 【Qt官方例程学习笔记】Address Book Example(代理模型)

    地址簿示例展示了如何使用代理模型在单个模型的数据上显示不同的视图. 本例提供了一个地址簿,允许按字母顺序将联系人分组为9组:ABC.DEF.GHI.…,VW,…XYZ.这是通过在同一个模型上使用多个视 ...

  5. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  6. (转)Qt Model/View 学习笔记 (五)——View 类

    Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...

  7. qt学习笔记(1):qt点击运行没有反应。

    因为公司的项目需要,今天开始重新学习已经忘干净了的QT, 说起qt之前在学校刚接触的时候就打心底里喜欢这个编辑器, 因为一直使用vs做项目,面对着黑洞洞的窗口总让人不舒服, 自从接触了qt感觉迎来了曙 ...

  8. Qt快速入门学习笔记(基础篇)

    本文基于Qter开源社区论坛版主yafeilinux编写的<Qt快速入门系列教程目录>,网址:http://bbs.qter.org/forum.php?mod=viewthread&am ...

  9. 【QT 学习笔记】 一、 VS2015+ QT环境安装

    1.   安装    qt-opensource-windows-x86-msvc2015_64-5.6.0.exe   (根据自己的VS版本来安装) 下载地址 http://download.qt. ...

随机推荐

  1. cat echo 打印菜单

    cat << END        =============================        1.apple        2.pear        3.banana   ...

  2. 本地连接出现"已启用检测该状态的服务"解决方法、方案

    1.运行 输出dcomcnfg 2.组件服务-计算机-我的电脑-DCOM配置-netprofm 3.右键属性-安全-启动和激活权限-自定义 4.编辑-添加-输入对象名称来选择-输入“LOCAL SER ...

  3. LinkedBlockingQueue,ArrayListBlockingQueue,SynchronousQueue

    LinkedBlockingQueue :1.读写锁分开,性能较 ArrayListBlockingQueue 只有一把锁控制读写要高一些.2.无界队列,不会触发Reject异常,ArrayListB ...

  4. 很实用的HTML5+CSS3注册登录窗体切换效果

    1. [代码]3个很实用的HTML5+CSS3注册登录窗体切换效果 <!DOCTYPE html><!--[if lt IE 7 ]> <html lang=" ...

  5. Python3之hashlib模块

    Python3之hashlib   简介: 用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法.在python3中已 ...

  6. 问题杂烩(scrollTop/背景透明度动画)

    今天给同学找我帮忙写js,是公司里的活..我是不是应该跟他要钱哈哈,不过一顿饭肯定是免不了的了. 言归正传,今天写了三个小东西,因为兼容性的问题,用jq写的(很是别扭的说,但是没办法啊,一边看api一 ...

  7. leetcode 258. Add Digits(数论)

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  8. 机器学习:YOLO for Object Detection (一)

    最近看了基于CNN的目标检测另外两篇文章,YOLO v1 和 YOLO v2,与之前的 R-CNN, Fast R-CNN 和 Faster R-CNN 不同,YOLO 将目标检测这个问题重新回到了基 ...

  9. python 标准库 —— http(http.cookiejar)

    1. cookie 信息的读取 from urllib import request import http from http import cookiejar cookie = cookiejar ...

  10. bzoj 3280: 小R的烦恼 费用流

    题目: Description 小R最近遇上了大麻烦,他的程序设计挂科了.于是他只好找程设老师求情.善良的程设老师答应不挂他,但是要求小R帮助他一起解决一个难题. 问题是这样的,程设老师最近要进行一项 ...