【Qt官方例程学习笔记】Getting Started Programming with Qt Widgets
创建一个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的更多相关文章
- 【Qt官方例程学习笔记】Application Example(构成界面/QAction/退出时询问保存/用户偏好载入和保存/文本文件的载入和保存/QCommandLineParser解析运行参数)
The Application example shows how to implement a standard GUI application with menus, toolbars, and ...
- 【Qt官方例程学习笔记】Raster Window Example(画笔的平移/旋转/缩放应用)
这个例子显示了如何使用QPainter渲染一个简单的QWindow. 值得学习的内容 <QtGui>头文件 #include <QtGui>就可以使用Qt GUI模块中的所有类 ...
- 【Qt官方例程学习笔记】Analog Clock Window Example (画笔的平移/旋转/缩放应用)
这个例子演示了如何使用QPainter的转换和缩放特性来简化绘图. 值得学习的: 定时器事件ID检查: 在定时器事件中检查定时器id是比较好的实践. QPainter抗锯齿: We call QPai ...
- 【Qt官方例程学习笔记】Address Book Example(代理模型)
地址簿示例展示了如何使用代理模型在单个模型的数据上显示不同的视图. 本例提供了一个地址簿,允许按字母顺序将联系人分组为9组:ABC.DEF.GHI.…,VW,…XYZ.这是通过在同一个模型上使用多个视 ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- (转)Qt Model/View 学习笔记 (五)——View 类
Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...
- qt学习笔记(1):qt点击运行没有反应。
因为公司的项目需要,今天开始重新学习已经忘干净了的QT, 说起qt之前在学校刚接触的时候就打心底里喜欢这个编辑器, 因为一直使用vs做项目,面对着黑洞洞的窗口总让人不舒服, 自从接触了qt感觉迎来了曙 ...
- Qt快速入门学习笔记(基础篇)
本文基于Qter开源社区论坛版主yafeilinux编写的<Qt快速入门系列教程目录>,网址:http://bbs.qter.org/forum.php?mod=viewthread&am ...
- 【QT 学习笔记】 一、 VS2015+ QT环境安装
1. 安装 qt-opensource-windows-x86-msvc2015_64-5.6.0.exe (根据自己的VS版本来安装) 下载地址 http://download.qt. ...
随机推荐
- 剑指offer——圆圈中最后剩下的数字
1.如果通过环形列表去模拟圆圈的话,最后时间复杂度为O(mn),而且还需要一个辅助链表来模拟圆圈,空间复杂度为O(n). 2.通过找出递推公式的方法,求得递推公式为 时间复杂度为O(n),空间复杂度为 ...
- 仿联想商城laravel实战---7、lavarel中如何给用户发送邮件
仿联想商城laravel实战---7.lavarel中如何给用户发送邮件 一.总结 一句话总结: 设置邮件服务器,比如163邮箱 lavarel中配置邮件服务,在.env中 控制器中使用Mail对象发 ...
- node.js+express+jade系列二:rotue路由的配置
页面的访问最常见的是get和post两种,无论是get请求还是post请求express自动判断执行app.get或app.post 1:app.get(名称,路径)或app["get&qu ...
- Java_异常_01_org.apache.commons.lang.exception.NestableRuntimeException
异常信息: The type org.apache.commons.lang.exception.NestableRuntimeException cannot be resolved. It is ...
- mybatis学习第(一)天
课程安排: Mybatis和springMVC通过订单商品案例驱动 第一天:基础知识(重点,内容量多) 对原生态jdbc程序(单独使用jdbc开发)问题总结 Mybatis框架原理 Mybatis的入 ...
- spring MVC basic
1.MVC&&Spring MVC .mvc的就核心思想是业务数据抽取同业务数据呈现相分离 .View,视图层,为用户提供UI,重点关注数据的呈现 .model,业务数据的信息表示,关 ...
- java正则表达式匹配文本中想要的字符串
需求:获取一个本地文件中所有符合 $[MAKE_PACKAGE] 格式的字符串,并输出到另一个文件中. public static void main(String[] args) throws Ex ...
- Statement
题目大意 给定一棵基环外向树,和若干组询问,对于每次独立的询问都指定一些起点和一些终点,你删去一些边,使得从任意起点出发都无法到达终点,并让删去的边的编号的最小值最大,求这个最大的最小值. 题解 不难 ...
- msdtc中rpc调试
http://www.cnblogs.com/nzperfect/archive/2011/11/03/2234595.html 1 工具: dtcping 2 配置 3 netboise
- jcrop的bug
1 360(7.1.1.620,内核:31.0.1650.63)的极速模式下,出现裁剪框后,鼠标点击,页面就会滑动到底部. 查看了下源码,发现是下面的代码: function watchKeys() ...