【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. ...
随机推荐
- cat echo 打印菜单
cat << END ============================= 1.apple 2.pear 3.banana ...
- 本地连接出现"已启用检测该状态的服务"解决方法、方案
1.运行 输出dcomcnfg 2.组件服务-计算机-我的电脑-DCOM配置-netprofm 3.右键属性-安全-启动和激活权限-自定义 4.编辑-添加-输入对象名称来选择-输入“LOCAL SER ...
- LinkedBlockingQueue,ArrayListBlockingQueue,SynchronousQueue
LinkedBlockingQueue :1.读写锁分开,性能较 ArrayListBlockingQueue 只有一把锁控制读写要高一些.2.无界队列,不会触发Reject异常,ArrayListB ...
- 很实用的HTML5+CSS3注册登录窗体切换效果
1. [代码]3个很实用的HTML5+CSS3注册登录窗体切换效果 <!DOCTYPE html><!--[if lt IE 7 ]> <html lang=" ...
- Python3之hashlib模块
Python3之hashlib 简介: 用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法.在python3中已 ...
- 问题杂烩(scrollTop/背景透明度动画)
今天给同学找我帮忙写js,是公司里的活..我是不是应该跟他要钱哈哈,不过一顿饭肯定是免不了的了. 言归正传,今天写了三个小东西,因为兼容性的问题,用jq写的(很是别扭的说,但是没办法啊,一边看api一 ...
- leetcode 258. Add Digits(数论)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 机器学习:YOLO for Object Detection (一)
最近看了基于CNN的目标检测另外两篇文章,YOLO v1 和 YOLO v2,与之前的 R-CNN, Fast R-CNN 和 Faster R-CNN 不同,YOLO 将目标检测这个问题重新回到了基 ...
- python 标准库 —— http(http.cookiejar)
1. cookie 信息的读取 from urllib import request import http from http import cookiejar cookie = cookiejar ...
- bzoj 3280: 小R的烦恼 费用流
题目: Description 小R最近遇上了大麻烦,他的程序设计挂科了.于是他只好找程设老师求情.善良的程设老师答应不挂他,但是要求小R帮助他一起解决一个难题. 问题是这样的,程设老师最近要进行一项 ...