【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. ...
随机推荐
- FOJ 2213 简单几何
题意:给你两个圆的圆心坐标和半径,判断两个圆公切线数目. 思路:考虑两个圆间公切线的情况,两个圆的位置关系分为相离,相交,外切,内切,内含,重合,公切线数分别为4,2,3,1,0,-1. #inclu ...
- 剑指offer之 二进制中1的个数
问题描述: 请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如把9表示成二进制是1001,有2位是1 因此如果输入9,该函数输出2; package Problem10; public ...
- Hadoop 运行jar包时 java.lang.ClassNotFoundException: Class com.zhen.mr.RunJob$HotMapper not found
错误如下 Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.zhen.mr.RunJob$H ...
- Python- 贪婪与非贪婪
python运行匹配时,如果没有人为限定,默认是贪婪模式. import re a = 'python 22222java34bigdata' r = re.findall('[a-z]{3}',a) ...
- Codeforces 372B Counting Rectangles is Fun:dp套dp
题目链接:http://codeforces.com/problemset/problem/372/B 题意: 给你一个n*m的01矩阵(1 <= n,m <= 40). 然后有t组询问( ...
- C# 往excel出力数据
/// <summary> /// 出力Excel /// </summary> /// <param name="storeModelForExcel&quo ...
- 时间复杂度O(n)与空间复杂度O(1)
把输入规模看成x轴,所花时间/空间看成y轴.O(n)就是 y = x, y随x的增长而线性增长.一条斜线O(1)就是 y = 1,不管x如何变,y不变.一条与x平行的线 举个简单的例子,要从0加到n, ...
- CI中控制器名不能和本个 控制器中的方法名相同
控制器名称:application/controllers/tang.php 控制器中方法名称:application/controllers/role.php 中有方法 public funct ...
- 如何使 vlc 支持 fdk-aac 编码(windows平台
可能是由于fdk-aac开源协议的原因,VLC默认是不支持fdk-aac编码的,fdk-aac 是非常优秀的AAC编码库,并且支持AAC-LD AAC-ELD, 对于要求低延迟的场景下很有用. 可以通 ...
- VC6++常用快捷键
VC6快捷键大全(转载) VC6快捷键大全,记在这里,方便查阅.F1: 帮助Ctrl+O :OpenCtrl+P :PrintCtrl+N :NewCtrl+Shift+F2 :清除所有书签F2 :上 ...