创建一个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

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

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

获取文件全部字符:

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

保存全部字符到文件

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

打印支持:

qtHaveModule(printsupport): QT += printsupport
requires(qtConfig(fontdialog))
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printer)
#if QT_CONFIG(printdialog)
#include <QPrintDialog>
#endif // QT_CONFIG(printdialog)
#include <QPrinter>
#endif // QT_CONFIG(printer)
#endif // QT_PRINTSUPPORT_LIB ... void Notepad::print()
{
#if QT_CONFIG(printer)
QPrinter printDev;
#if QT_CONFIG(printdialog)
QPrintDialog dialog(&printDev, this);
if (dialog.exec() == QDialog::Rejected)
return;
#endif // QT_CONFIG(printdialog)
ui->textEdit->print(&printDev);
#endif // QT_CONFIG(printer)
}

复制、剪切、粘贴

void Notepad::copy()
{
#if QT_CONFIG(clipboard)
ui->textEdit->copy();
#endif
} void Notepad::cut()
{
#if QT_CONFIG(clipboard)
ui->textEdit->cut();
#endif
} void Notepad::paste()
{
#if QT_CONFIG(clipboard)
ui->textEdit->paste();
#endif
}

撤销和重做

void Notepad::undo()
{
ui->textEdit->undo();
} void Notepad::redo()
{
ui->textEdit->redo();
}

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

void Notepad::selectFont()
{
bool fontSelected;
QFont font = QFontDialog::getFont(&fontSelected, this);
if (fontSelected)
ui->textEdit->setFont(font);
} void Notepad::setFontUnderline(bool underline)
{
ui->textEdit->setFontUnderline(underline);
} void Notepad::setFontItalic(bool italic)
{
ui->textEdit->setFontItalic(italic);
} void Notepad::setFontBold(bool bold)
{
bold ? ui->textEdit->setFontWeight(QFont::Bold) :
ui->textEdit->setFontWeight(QFont::Normal);
}

关于对话框:

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

【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. awk过滤磁盘使用率

    过滤出大于某个值的磁盘使用率: #!/bin/bash diskspace=`df -Ph`IFS="\n" for line in $diskspace;do        ec ...

  2. vue-cli3 set vue.config.js

    //config目录下index.js配置文件// see http://vuejs-templates.github.io/webpack for documentation.// path是nod ...

  3. Python 模块EasyGui

    1.msgBox msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None) ms ...

  4. 在javascript中使用replace

    javascript中使用replace功能很强大,不仅可以替换为字符串,还可以利用正则表达式,对匹配到的字符串为所欲为的进行操作,首先我们来了解一下replace的基本语法 定义和用法 replac ...

  5. ajax页面中的前进与后台的实现

    具体效果参考:http://www.zhangxinxu.com/study/201306/ajax-page-html5-history-api.html? //以下是自己的部分理解<!DOC ...

  6. 恢复delete删除的数据

    SELECT * FROM tablename AS OF TIMESTAMP TO_TIMESTAMP('2010-12-15 11:10:17', 'YYYY-MM-DD HH:MI:SS')

  7. MVC中URL传多个参数

    1.mvc中url传递多个参数不能直接使用&,会报错(从客户端(&)中检测到有潜在危险的 Request.Path 值) 方法①:使用?---/Home/Index/?id=xxx&a ...

  8. PyCharm 常用快捷键和设置

    pycharm常用快捷键 1.编辑(Editing) Ctrl + Space    基本的代码完成(类.方法.属性)Ctrl + Alt + Space  快速导入任意类Ctrl + Shift + ...

  9. Redis-benchmark使用总结

    Redis-benchmark为Redis性能测试工具. 指令说明: Usage: redis-benchmark [-h <host>] [-p <port>] [-c &l ...

  10. tornado 获取参数

    self.get_argument('name') 没有传递name就报400错误了 tornado.web.MissingArgumentError: HTTP 400: Bad Request ( ...