收藏 分销(赏)

多年来开发QT累计的笔记.doc

上传人:s4****5z 文档编号:8656924 上传时间:2025-02-24 格式:DOC 页数:24 大小:163KB 下载积分:10 金币
下载 相关 举报
多年来开发QT累计的笔记.doc_第1页
第1页 / 共24页
多年来开发QT累计的笔记.doc_第2页
第2页 / 共24页


点击查看更多>>
资源描述
QT学习笔记-1.QT主要的对象 说来惭愧学习c++很长时间了一直没有使用c++开发过软件界面 所以现在想认认真真的学习一个c++图形界面框架库 本来想学习Xwidget但是这个资料不大好找 有啥问题不好解决 那就学习QT吧 不说QT的优缺点,不说如何编译QT 从QT的主要库类开始吧 知道了基本的对象之后如果需要学习看看文档就知道了 如果需要编译QT的话再下个代码试着编译吧 QApplication 应用程序类                管理图形用户界面应用程序的控制流和主要设置 QLabel 标签类                                提供文本或者图像的显示 QPushButton 按钮类                      提供了命令按钮 按钮的一种 QButtonGroup 按钮组合类              按钮组 相关按钮的组合 QGroupBox 群组类                         一个有标题的组合框 QDateTimeEdit 日期时间编辑框类   QLineEdit 行编辑框类                     单行文本编辑器 QTextEdit 文本编辑框类                  单页面多信息编辑器对象 QComboBox 组合框类 QProgressBar 进度条类 QLCDNumber 数字显示框类 QScrollBar 滚动条类 QSpinBox 微调框类 QSlider 滑动条类 QIconView 图标视图类 QListView 列表视图类 QListBox 列表框类 QTable 表格类 QValidator 有效性检查类 QImage 图像类 QMainWindow 主窗口类 QPopupMenu 弹出性菜单类 QMenuBar 菜单栏类 QToolButton 工具按钮类 QToolTip 提示类 QWhatsThis 这是什么类 QAction 动作类 QHBoxLayout 水平布局类 QVBoxLayout 垂直布局类 QGridLayout 表格布局类 QT对话框类 QMessageBox 消息对话框类 QProgressDialog 进度条对话框类 QWizard 向导对话框类 QFileDialog 文件对话框类 QColorDialog 颜色对话框类 QFontDialog 字体对话框类 QPrintDialog 打印对话框类 基本就这些对象了 要系统学习QT 还需要看看QT的slot系统,QT库类接口等 具体的学习就是看例子咯 QT学习笔记-2.QT窗体布局和皮肤加载 学习QT的一个原因是貌似QT做出来的界面比较绚丽 我倒想看看能做出来啥样子的 从QT窗体布局说起 凡是窗体布局无非就是如何摆放的问题 1.想当然如果摆放有2个方式一个是所见即所得,一个是使用布局管理器 先说后者吧 2.QT有好几种布局管理器无非就是啥子流式布局,格子布局等等 从这个层级上说软件界面都是布局嵌套的 3.布局和控件的关系    一般是一个布局对应于一个控件容器(或者顶层控件)    使用当前布局管理器加挂子控件(容器)即可 然后给当前控件挂上布局管理器即可 下面是一个简单的QT Layout的例子(从QT例子改的) 1. class Dialog : public QDialog 2. { 3.     Q_OBJECT 4. public: 5.     Dialog(); 6. private: 7.     void createHorizontalGroupBox(); 8. 9.     enum {button_number = 4}; 10.     QGroupBox *groupbox; 11.     QPushButton *buttons[button_number]; 12.     QDialogButtonBox *buttonBox; 13. }; 复制代码 实现如下: 1. #include <QtGui> 2. 3. #include "dialog.h" 4. 5. //! [0] 6. Dialog::Dialog() 7. { 8.     createHorizontalGroupBox(); 9. 10.     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok 11.                                      | QDialogButtonBox::Cancel); 12. 13.     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); 14.     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); 15. 16.     QVBoxLayout *mainLayout = new QVBoxLayout; 17.     mainLayout->addWidget(groupbox); 18.     mainLayout->addWidget(buttonBox); 19.     setLayout(mainLayout); 20. 21.     setWindowTitle(tr("LayoutTest")); 22. } 23. 24. void Dialog::createHorizontalGroupBox() 25. { 26.     groupbox = new QGroupBox(tr("Layout Test")); 27.     QHBoxLayout *layout = new QHBoxLayout; 28. 29.     buttons[0] = new QPushButton(tr("Button1")); 30.     buttons[1] = new QPushButton(tr("Button2")); 31.     buttons[2] = new QPushButton(tr("Button3")); 32.     buttons[3] = new QPushButton(tr("Button4")); 33. 34.     for(int i = 0;i<button_number;i++) 35.         layout->addWidget(buttons[i]); 36.     groupbox->setLayout(layout); 37. } 复制代码 几个知识点: 1.groupbox = new QGroupBox(tr("Layout Test")); Layout Test 是个文本这个无须解释 那tr呢?查查资料知道是为了支持多语言 先知道即可以后使用的话在具体查查吧 2.QDialogButtonBox是个什么东西    看看最终的程序界面吧    原来是对话框的确认和取消按钮     再看信号槽函数无非就是绑定按钮到操作函数     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));     那accepted和accept函数有啥区别?     看看文档     accept函数的解释是:Hides the modal dialog and sets the result code to Accepted   accpeted函数的解释是:This signal is emitted when the dialog has been accepted either 在说说QT皮肤 学习QT的主要目的就是想做做脸蛋好看好的软件界面 那就试试看吧 查到的QT有一个名叫QSS(CSS?)的文件可以原来换肤 那就改改看吧 #include <QApplication> #include <QFile> #include <QStyleFactory> #include <QTextStream> #include "dialog.h" void setSkin(QApplication* const app, QString const &skinFile); int main(int argc, char *argv[]) {     QApplication app(argc, argv);     setSkin(&app ,"skin.qss");     Dialog dialog;     dialog.show();     return app.exec(); } void setSkin(QApplication* const app, QString const &skinFile) {     QFile qss(skinFile);     qss.open(QFile::ReadOnly);     app->setStyleSheet(qss.readAll());     qss.close(); } 相应的QSS文件如下: QPushButton {     color:red;     background:url(setting.png) } 这里把PushButton的文本颜色设置为红色 同时把它的背景设置为图片stting.png QT学习笔记-3.Codecs例子学习 QT自带的例子Codecs是一篇关于保存和载入不同编码文本的例子 其界面比较简单一个简单的单文档而已 有2个主要的对象 一个是MainWindow用于窗体主界面 另外一个是PreviewForm用于显示编码格式列表 1.其编码格式的获取 部分代码如下:     QMap<QString, QTextCodec *> codecMap;     QRegExp iso8859RegExp("ISO[- ]8859-([0-9]+).*");     foreach (int mib, QTextCodec::availableMibs()) {         QTextCodec *codec = QTextCodec::codecForMib(mib);         QString sortKey = codec->name().toUpper();         int rank;         if (sortKey.startsWith("UTF-8")) {             rank = 1;         } else if (sortKey.startsWith("UTF-16")) {             rank = 2;         } else if (iso8859RegExp.exactMatch(sortKey)) {             if (iso8859RegExp.cap(1).size() == 1)                 rank = 3;             else                 rank = 4;         } else {             rank = 5;         }         sortKey.prepend(QChar('0' + rank));         codecMap.insert(sortKey, codec);     }     codecs = codecMap.values(); 通过使用foreach循环来获取支持的编码格式并保存 不过看上去foreach (int mib, QTextCodec::availableMibs())有点奇怪 查查资料 解释是foreach(variables ,container)关键字是Qt对c++的一个扩展,主要用于按顺序历经容器(container)中的对象 2关于文件菜单的生成和设置 菜单的构造 一个例子 saveAsMenu = new QMenu(tr("&Save As"), this);这里有2个参数一个是菜单显示文另外一个是当前窗体指针 然后就可以加载子菜单,设置分隔符,设置事件响应等操作了 例子为:     fileMenu = new QMenu(tr("&File"), this);     fileMenu->addAction(openAct);     fileMenu->addMenu(saveAsMenu);     fileMenu->addSeparator();     fileMenu->addAction(exitAct); 那如何绑定菜单到当前窗体呢 如下: menuBar()->addMenu(fileMenu); menuBar()->addSeparator(); menuBar()->addMenu(helpMenu); menuBar()是QmainWindow的成员函数用于获取窗体菜单项指针 3.QT对象QAction QT文档对QAction的解释是可以抽象用户接口对象-可以插入控件 例子如下:     openAct = new QAction(tr("&Open"), this);     openAct->setShortcuts(QKeySequence::Open);     connect(openAct, SIGNAL(triggered()), this, SLOT(open())); 无非就是对象声明,设置快捷键,链接函数和响应而已 4.窗体设置     setWindowTitle(tr("Codecs"));     resize(500, 400); 5.消息对话框     QMessageBox::about(this, tr("About Codecs"),             tr("The <b>Codecs</b> example demonstrates how to read and write "                "files using various encodings.")); QT学习笔记-4.信号与插槽 本文主要是对C++ GUI Programming with Qt4一书 Signals and Slots in Depth 部分的翻译 信号与插槽机制是Qt编程的基础.它可以绑定对象而不需要对象之间彼此了解。 槽类似于c++中的成员函数他可以是虚拟的,可重载的,私有的,公开的,受保护的。 不同点式槽可以链接到信号。通过这种方式可以在每次信号发射的的时候做到调用槽函数 connect()语句是这样的 1. connect(sender, SIGNAL(signal), receiver, SLOT(slot)); 复制代码 在这里sender和receiver是指向信号对象和槽对象的指针。宏SIGNAL()和SLOTS()负责转换他们的参数到字符串。 当然一个信号可以连接到多个槽(似乎都是这样的) 1. connect(slider, SIGNAL(valueChanged(int)), 2.         spinBox, SLOT(setValue(int))); 3. connect(slider, SIGNAL(valueChanged(int)), 4.         this, SLOT(updateStatusBarIndicator(int))); 复制代码 同样多个信号可以连接到单个槽 例如: 1. connect(lcd, SIGNAL(overflow()), 2.         this, SLOT(handleMathError())); 3. connect(calculator, SIGNAL(divisionByZero()), 4.         this, SLOT(handleMathError())); 复制代码 除此之外信号可以连接到其他信号(见过的其他插槽系统似乎不大可以?) connect(lineEdit, SIGNAL(textChanged(const QString &)),         this, SIGNAL(updateRecord(const QString &))); 需要指出的是信号信号链接和信号插槽连接时不同的 既然信号和插槽可以连接那么他们应该可以断开,如下: 1. disconnect(lcd, SIGNAL(overflow()), 2.            this, SLOT(handleMathError())); 复制代码 一个简单的例子: 1. class Employee : public QObject 2. { 3.     Q_OBJECT 4. public: 5.     Employee() { mySalary = 0; } 6.     int salary() const { return mySalary; } 7. public slots: 8.     void setSalary(int newSalary); 9. signals: 10.     void salaryChanged(int newSalary); 11. private: 12.     int mySalary; 13. }; 14. void Employee::setSalary(int newSalary) 15. { 16.     if (newSalary != mySalary) { 17.         mySalary = newSalary; 18.         emit salaryChanged(mySalary); 19.     } 20. } 21. 复制代码 说明 关键字 public slots:和signals 他们用于修饰插槽函数和信号函数 至于信号的反射通过关键字 emit来实现 通过本文基本掌握了QT的信号插槽机制 QT学习笔记-5.QT容器和算法 本文涉及QT中的容器对象系列 一容器 QVector类似stl中的vector插入尾部数据速度很快 基本例子如下: QVector<double> vect(3); vect[0] = 1.0; vect[1] = 0.540302; vect[2] = -0.416147; 这样也可以 QVector<double> vect; vect.append(1.0); vect.append(0.540302); vect.append(-0.416147); 另外也可以通过流 vect << 1.0 << 0.540302 << -0.416147; 2.QLinkedList没有提供[]操纵 随机访问速度很慢 3.QList<T>序列容器是一种数组链表它具有前2者的大部分操作支持随机访问 在容器头部尾部插入数据很迅速 4.QStringList是QList<QString>的子类提供以下操作 排序,过滤,正则表达式处理等 5.QStack<T> 提供push,pop,top等操作 6.Queue<T> QT的队列模板 7.QMap<T,V> 基本操作如下: QMap<QString, int> map; map.insert("eins", 1); map.insert("sieben", 7); map.insert("dreiundzwanzig", 23); 当然也可以这样插入数据 map["eins"] = 1; map["sieben"] = 7; map["dreiundzwanzig"] = 23; 但是这样做有一个问题那就是会生成空值 为了不出现这样的结果可以使用成员函数value()来代替[]来获取数据,如果对于键不存在则返回默认的对象值 8.QHash<T,V> 使用散列存储键值对 二通用算法 需要说明的是QT中的容器是stl风格的,这意味着可以使用Stl中的算法。 当然在QT库中QtAlgorithm文件包含了基本的算法模板 主要的函数有 qFind-查找给定元素在容器中的位置类似std::find qBinaryFind - 类似qFind 需要查找元素有序排列 qFill-类似std::fill 使用给定元素填充容器 qCopy,qSort.. qDeleteAll-函数对容器中的每个元素做删除处理 基本的容器和算法就这么多了 其实熟悉stl的话学习这个上手很快的 QT学习笔记-6.QApplication和基本控件的使用 接上文,这篇学学QT中基本控件的使用和QApplication对象 1.什么是QApplication? 文档说明:The QApplication class manages the GUI application's control flow and main settings. Application类管理GUI程序控制流和主要参数设置 QApplication继承于QCoreApplication。后者提供了控制台程序的事件流 2.基本控件的使用例子: #include <QApplication> #include <QLabel> #include <QPalette> #define QT_HTML QLabel* label = NULL; void initlabel() { #ifndef QT_HTML     label = new QLabel("Hello Qt!"); #else     label = new QLabel("<h2><i>Hello</i><font color=red>Qt!</font></h2>"); #endif     //! set size     label->setBaseSize(64,48);     //! set alignment     label->setAlignment(Qt::AlignHCenter);         //! sht background color     QColor bk(100,100,125);     QPalette palette(bk);     label->setPalette(palette); } int main(int argc, char *argv[]) {     QApplication app(argc, argv);     app.setApplicationName("QT Test");     initlabel();     label->show();     return app.exec(); } QLabel是QT中的标签控件它具有控件的一般属性比如设置大小setBaseSite,设置对齐格式,当然也可以设置背景色或者图片-这都是通过QPalette调色板来实现的 需要说明的是QT中的控件文本可以使用Html语法的文本来操作具体如上。 那觉这个功能比较给力! 3.那么什么是QPalette? QPalette负责控制控件状态的颜色组-注意是控件状态。 那么对一个控件每个状态的颜色都可以是不一样的咯 至于QPalette的详细功能和使用方法以后需要的时候再看吧 4.基本的信号链接使用例子 #include <QApplication> #include <QPushButton> int main(int argc, char *argv[]) {     QApplication app(argc, argv);     QPushButton *button = new QPushButton("Quit");     //! when click button, app exit.     QObject::connect(button, SIGNAL(clicked()),&app, SLOT(quit()));     button->show();     return app.exec(); } 5.一个复杂点的例子 #include <QApplication> #include <QHBoxLayout> #include <QSlider> #include <QSpinBox> #include <QIcon> int main(int argc, char *argv[]) {     QApplication app(argc, argv);     QWidget* widget = new QWidget;     QIcon icon("config.png");     widget->setWindowIcon(icon);     widget->setWindowTitle("Using QT");                  QSlider* slider = new QSlider(widget);     slider->setRange(0,99);     QSpinBox* spinbox = new QSpinBox(widget);     spinbox->setRange(0,99);         widget->show();     return app.exec(); } 编译运行可以看出QWidget中默认的布局管理器是竖直向下排列的 在QT中可以通过setWindowIcon来设置窗体图标 通过setWindowTitle设置窗体标题 6.加上布局管理器和信号连接的话代码大致应该是这个样子 1. #include <QApplication> 2. #include <QHBoxLayout> 3. #include <QSlider> 4. #include <QSpinBox> 5. #include <QIcon> 6. 7. int main(int argc, char *argv[]) 8. { 9.     QApplication app(argc, argv); 10.     QWidget* widget = new QWidget; 11.     QIcon icon("config.png"); 12.     widget->setWindowIcon(icon); 13.     widget->setWindowTitle("Using QT"); 14. 15.     QSlider* slider = new QSlider(widget); 16.     slider->setRange(0,99); 17. 18.     QSpinBox* spinbox = new QSpinBox(widget); 19.     spinbox->setRange(0,99); 20. 21.     QHBoxLayout* layout = new QHBoxLayout; 22.     layout->addWidget(spinbox); 23.     //! adjust slider's direction 24.     slider->setOrientation(Qt::Horizontal); 25.     layout->addWidget(slider); 26. 27.     spinbox->setValue(28); 28. 29.     //! connect signals and slots 30.     QObject::connect(spinbox, SIGNAL(valueChanged(int)),slider,SLOT(setValue(int))); 31.     QObject::connect(slider,SIGNAL(valueChanged(int)),spinbox,SLOT(setValue(int))); 32. 33.     widget->setLayout(layout); 34.     widget->show(); 35.     return app.exec(); 36. } 复制代码 需要说明的是在这里QSlider,QPinBox控件是互动 编译程序并运行界面如下: 2011-6-30 09:29 上传 下载附件 (14.46 KB) 这是关于QT的第六篇笔记 总结下吧 QT功能还是很强大贴心的 比较容易上手 不过有2点我感觉不大舒服的地方是对这个变量命名格式有点不大喜欢 比如setValue我喜欢写成SetValue. 仅此而已 QT学习笔记-7.QString和QByteArray 1.QString 在c++标准库中有2种类型的字符串char*和std::string QT则提供了对象QString QString除了具备字符串对象一般功能函数之外还提供了自己特有的功能比如: str.sprintf("%s %.1f%%", "perfect competition", 100.0); ---格式化输出 当然也可以使用匿名形式 str = QString("%1 %2 (%3s-%4s)").arg("permissive").arg("society").arg(1950).arg(1970); %1,%2,%3是占位符 另外QString还提供了一系列类型类型装换函数比如: str = QString::number(59.6); str.setNum(59.6); 当然还有其他功能 下面是具体的测试例子: #include <QtCore/QCoreApplication> #include <QString> #include <QtDebug> int main(int argc, char *argv[]) {     QCoreApplication a(argc,  argv);     QString str;     str.append("Hello QT");     qDebug()<<str.left(3);     qDebug()<<str.right(3);     qDebug()<<str.mid(2,2);     qDebug()<<str.mid(2);     qDebug()<<str.indexOf("QT");     qDebug()<<str.startsWith("Hello");     qDebug()<<str.toLower();     str.replace(" ","O(∩_∩)O~");     qDebug()<<str;     str.insert(0,"....");     qDebug()<<str;     str.remove(0,3);     qDebug()<<str;     str = QString("%1%2%3").arg("1").arg(2).arg("3");     qDebug()<<str;     return a.exec(); } 当然还有其他它功能 2.QByteArray QByteArray是比特数组 为了掌握它的基本功能还是测试下功能大致就知道了 #include <QtCore/QCoreApplication> #include <QByteArray> #include <QtDebug> int main(int argc, char *argv[]) {     QCoreApplication a(argc,  argv);     QByteArray arr;     arr.append("123");     qDebug()<<arr.at(2);     arr = "lots\t of\nwhitespace\r\n ";     qDebug()<<arr;     qDebug()<<arr.simplified();     qDebug()<<arr;     arr.chop(3);     qDebug()<<arr;     qDebug()<<arr.count();     qDebug()<<arr.isEmpty();     qDebug()<<arr.isNull();     arr.fill(244,10);     qDebug()<<arr;     return a.exec(); } QT学习笔记-8.查找文件对话框 只所以选择这个QT自带的例子有2个原因 1是界面简单容易理解 2是当点击more按键会动态改变窗体出现高级选项(我想知道这个是如何做到的) 基本代码如下: 1. #ifndef FINDDIALOG_H 2. #define FINDDIALOG_H 3. 4. #include <QDialog> 5. 6. QT_BEGIN_NAMESPACE 7. class QCheckBox; 8. class QDialogButtonBox; 9. class QGroupBox; 10. class QLabel; 11. class QLineEdit; 12. class QPushButton; 13. QT_END_NAMESPACE 14. 15. class FindDialog : public QDialog 16. { 17.     Q_OBJECT 18. 19. public: 20.     FindDialog(QWidget *parent = 0); 21. 22. private: 23.     QLabel *label; 24.     QLineEdit *lineEdit; 25.     QCheckBox *caseCheckBox; 26.     QCheckBox *fromStartCheckBox; 27.     QCheckBox *wholeWordsCheckB
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服