第一个Qt程序
新建一个空的项目

.pro文件内容

示例代码:

1/*应用程序抽象类*/2#include<QApplication>3#include<QWidget>4#include<QPushButton>5#include<QDebug>67intmain(intargc,char*argv[])8{9QApplicationapp(argc,argv);1011QWidgetw;12w.show();13w.setWindowTitle("你好,世界!");//Qt5不存在乱码的问题了1415QPushButtonbtn("button");16btn.setParent(&w);//不设置父对象,那么按钮也就是一个独立的窗口17btn.show();1819QObject::connect(&btn,QPushButton::clicked,[](){20qDebug()<<"buttonisclicked..."<<endl;21});2223returnapp.exec();24}

QLineEdit常用功能
设置输入密码
设置提示文字
设置自动匹配
示例代码:

1#include<QApplication>2#include<QLineEdit>3#include<QDebug>4#include<QCompleter>5#include<QWidget>67intmain(intargc,char*argv[])8{9QApplicationapp(argc,argv);1011QLineEditlineEdit;12/*显示模式enumEchoMode{Normal,NoEcho,Password,PasswordEchoOnEdit};*/13//lineEdit.setEchoMode(QLineEdit::PasswordEchoOnEdit);//失去焦点时候变为密文1415/*淘汰传统的输入之后还要点击按钮*/16QObject::connect(&lineEdit,QLineEdit::returnPressed,[](){17qDebug()<<"Youhaveenteredthereturnkey...";18});1920/*自动补全只有echomode设置为Normal时completer才有效*/21QCompletercompleter(QStringList()<<"aa"<<"abc"<<"123");22completer.setFilterMode(Qt::MatchContains);//包含匹配比如:输入b也会匹配abc23lineEdit.setCompleter(&completer);2425/*lineEdit设置提示文字*/26lineEdit.setPlaceholderText("Pleaseinputtext:");2728lineEdit.show();2930returnapp.exec();31}

坐标系统
设置坐标

1#include<QApplication>2#include<QWidget>3#include<QPushButton>45intmain(intargc,char*argv[])6{7QApplicationapp(argc,argv);89/*构造一个窗口*/10QWidgetw;1112/*显示窗口*/13w.show();1415/*按钮也是个窗口*/16QPushButtonbutton;17button.setText("Button");18/*窗口对象的父子关系,影响显示位置*/19/*没有父窗口的窗口,我们称之为主窗口*/20button.setParent(&w);21button.show();2223button.setGeometry(30,30,100,30);2425returnapp.exec();26}

布局Layout
QHBoxLayout简单实现控件居中:

main.cpp

效果图:

QHBoxLayout实现居中并且设置控件比重:

main.cpp

效果图:

QGridLayout实现控件居中:

main.cpp

效果图:

QGridLayout实现简单的登录界面:

main.cpp

效果图:

常用控件的基本使用
QLabel

QLabel.cpp

Button组(QPushButton,QRadioButton, QCombobox,QCheckBox)

Button.cpp

效果图:

QTextEdit基本使用:

QTextEdit.cpp

效果图:

QGroupBox基本使用:

QGroupBox.cpp

效果图:

QSlider、QSpinBox基本使用:

QSpinBoxAndQSlider.cpp

效果图:

QLCDNumber基本使用:

QLCDNumber.cpp

效果图:

QEvent

MyEvent.h

MyEvent.cpp

EventFilter

MyApplication.h

MyApplication.cpp

MyWidget.h

MyWidget.cpp

QPainter
覆盖基类的painterEvent虚函数,函数原型为:

简单的图形绘制:

1voidMyWidget::paintEvent(QPaintEvent*)2{3//简单的绘制4QPainterp(this);5//p.drawArc(100,100,20,20,10,10);6p.drawLine(QPointF(100,100),QPointF(200,200));7p.setPen(QPen(QColor("red")));8p.setBrush(Qt::yellow);//画刷是代表封闭图形的内部填充9p.drawRect(QRect(200,200,220,220));10p.setRenderHint(QPainter::Antialiasing);//变得圆润一些11p.drawEllipse(QPointF(150,150),40,40);12p.drawText(QPointF(250,250),"HelloQt");13}

通过变换集来存储所有的变换:

1{2//通过变换集来存放所有的变换3QPainterp(this);4QTransformtrans;5//trans.rotate(88.0,Qt::YAxis);6//trans.translate(100,300);7trans.scale(3,3);8p.setTransform(trans);9p.drawLine(100,100,200,100);10}

先画一张图,然后将该图绘制到窗口上:

1{2//先画一张图,然后将图绘制到窗口3QPixmappixmap(size());//pixmap默认的是黑色背景4QPainterp(&pixmap);56//想着设置pixmap背景为白色,并不能实现,需要通过以下方法7//p.setBackground(QBrush(Qt::white));8p.setBrush(Qt::white);9p.drawRect(0,0,size().width(),size().height());10p.drawLine(QPointF(100,100),QPointF(200,200));11p.setPen(QPen(QColor("red")));12p.setBrush(Qt::yellow);//画刷是代表封闭图形的内部填充13p.drawRect(QRect(200,200,220,220));1415p.end();16p.begin(this);//开始绘制窗口17p.drawPixmap(0,0,pixmap);18}