(在b站學(xué)習(xí)QT,順便做做筆記 2019年最新QT從入門到實戰(zhàn)完整版|傳智播客)
1.顯示窗口;
myWidget w;? ? ? ? ? ? //myWidget 窗口類
w.show;? ? ? ? ? ? ? ? ? ? //使窗口類對象顯示
2.介紹.pro文件部分代碼
QT? ? ? += core gui? ? ? ? ? ? ? ? //QT包含的模塊
//大于四版本以上的 包含 widget模塊
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
TARGET = untitled1? ? ? ? //目標(biāo) 生成exe程序的名稱
TEMPLATE = app? ? ? ? ? ? //模板 應(yīng)用程序模板 Application
SOURCES += \? ? ? ? ? ? ? ? //源文件
? ? ? ? main.cpp \
? ? ? ? mainwindow.cpp
HEADERS += \? ? ? ? ? ? ? ? //頭文件
? ? ? ? mainwindow.h? ?
3.部分常用快捷鍵
注釋? ? ctrl? ? +? ? /
運行? ? ctrl? ? +? ? r
編譯? ? ctrl? ? +? ? b
字體縮放? ? ctrl? ? +? ? 鼠標(biāo)滾輪
查找? ? ctrl? ? +? ? f
整行移動? ? ctrl? ? +? ? shift? ? +? ? ↑? ? 或者? ? ↓
幫助文檔? ? F1
同名? ? .h? ? 與? ? .cpp? ? 文件快速切換? ? F4
4.基礎(chǔ)UI代碼
//QPushButton()共五種重載,可以查看幫助文檔對其進行初始化
QPushButton *btn1=new QPushButton;? ? ? ? //創(chuàng)建QPushButton 指針
//btn1.show();? ? ? ? //以頂層方式彈出窗口控件
//設(shè)定窗口父對象,this指向當(dāng)前窗口,可以替換為其他窗口地址 &+對象窗口名
btn1->setParent(this);? ? ? ?
btn1->setText("第一個按鈕");? ? ? ? //設(shè)定按鈕顯示文字
btn1->move(100,100);? ? ? ? //移動按鈕(x,y),以左上角零點,橫為x,豎為y
setWindowTitle("第一個窗口");? ? ? ? //設(shè)置窗口標(biāo)題
resize(600,400);? ? ? ? //重置窗口大小,固定窗口大小,不能進行縮放
5.對象樹
QT所有繼承于QObject的類的對象在窗口結(jié)束時都會自己釋放。
每次對應(yīng)類創(chuàng)建對象都會提供一個Parent指針,也就是父對象指針。當(dāng)創(chuàng)建QObject對象時,會傳一個Parent指針,這相當(dāng)于每次創(chuàng)建一個QObject對象,可以提供一個父對象。我們創(chuàng)建的對象會自動添加到父對象的children()列表;當(dāng)父對象析構(gòu),其children()列表的所有對象都會析構(gòu)。
MainWindow::MainWindow(QWidget *parent) :? ? ? ? //parent即為父對象指針
? ? QMainWindow(parent),
? ? ui(new Ui::MainWindow)
{
? ? ui->setupUi(this);
? ? QPushButton *btn1=new QPushButton();
? ? //btn1.show();
? ? btn1->setParent(this);
? ? btn1->setText("第一個按鈕");
? ? btn1->move(100,100);
? ? setWindowTitle("第一個窗口");
? ? resize(9600,5000);
}
將所有對象的析構(gòu)函數(shù)寫上qDebug()<<"對象釋放";父對象的qDebug先打印,但是其實子對象先釋放,等子對象釋放完,父對象再釋放。
6.信號和槽
//點擊按鈕關(guān)閉窗口
//connect連接信號和槽
// connect(對象1,&對象1的類::對象1里的信號,對象2,&對象2的類::對象2的槽);
connect(btn1,&QPushButton::clicked,this,&MainWindow::close);?
//自定義信號和槽
//類1? teacher類
#ifndef TEACHER_H
#define TEACHER_H
#include <QObject>
class teacher : public QObject
{
? ? Q_OBJECT
public:
? ? explicit teacher(QObject *parent = nullptr);
signals:? ? ? ? //信號都應(yīng)寫再signals后面
? ? void hungry();? ? ? ? //定義信號,老師餓了,信號不必定義但要聲明
public slots:? ? ? ? //老版本的槽都該寫在slots后面,新版本不做要求
};
#endif // TEACHER_H
//類2? student類
#ifndef STUDENT_H
#define STUDENT_H
#include <QObject>
class student : public QObject
{
? ? Q_OBJECT
public:
? ? explicit student(QObject *parent = nullptr);
signals:
public slots:
? ? void treat();? ? ? ? //槽,學(xué)生請客函數(shù),槽既要定義也要聲明
};
#endif // STUDENT_H
//student 的cpp文件
#include "student.h"
#include<QDebug>
student::student(QObject *parent) : QObject(parent)
{
}
void student::treat()
{
? ? qDebug()<<"老師餓了,學(xué)生請客";
}
//connect寫在了窗口類上,這里顯示其cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QPushButton"
#include "QWidget"
#include"teacher.h"
#include"student.h"
MainWindow::MainWindow(QWidget *parent) :
? ? QMainWindow(parent),
? ? ui(new Ui::MainWindow)
{
? ? ui->setupUi(this);
? ? teacher *tc=new teacher();? ? ? ? //定義teacher的對象指針
? ? student *st=new student();? ? ? ? //定義student的對象指針
? ? connect(tc,&teacher::hungry,st,&student::treat);? ? ? ? //連接信號和槽
? ? emit tc->hungry();? ? ? ? //觸發(fā)信號
}
MainWindow::~MainWindow()
{
? ? delete ui;
}
7.信號和槽重載
//類 teacher
class teacher : public QObject
{
? ? Q_OBJECT
public:
? ? explicit teacher(QObject *parent = nullptr);
signals:
? ? void hungry();
? ? void hungry(QString foodname);? ? ? ? //信號hungry重載
public slots:
};
//類 student
class student : public QObject
{
? ? Q_OBJECT
public:
? ? explicit student(QObject *parent = nullptr);
signals:
public slots:
? ? void treat();
? ? void treat(QString foodname);? ? ? ? //槽treat重載
};
//student.cpp
#include "student.h"
#include<QDebug>
student::student(QObject *parent) : QObject(parent)
{
}
void student::treat()
{
? ? qDebug()<<"老師餓了,學(xué)生請客";
}
void student::treat(QString foodname)
{
? ? qDebug()<<"老師餓了,學(xué)生請客"<<foodname;
}
//窗口類
#include"student.h"
MainWindow::MainWindow(QWidget *parent) :
? ? QMainWindow(parent),
? ? ui(new Ui::MainWindow)
{
? ? ui->setupUi(this);
? ? teacher *tc=new teacher();
? ? student *st=new student();
//? ? connect(tc,&teacher::hungry,st,&student::treat);
//? ? emit tc->hungry();
? ? void(teacher::*teachersignal)(QString)=&teacher::hungry;? ? ? ? //函數(shù)指針teachersignal
? ? void(student::*studentslot)(QString)=&student::treat;? ? ? ? ? ? ? ? //函數(shù)指針studentslot
? ? connect(tc,teachersignal,st,studentslot);
? ? emit tc->hungry("宮爆雞丁");
}
8.QString轉(zhuǎn)char*
QString->char*先轉(zhuǎn)成QByteArray(foodname.toUtf8())再轉(zhuǎn)char*
例:foodname.toUtf8().data();
9.信號連接信號
//通過定義按鈕,點擊按鈕觸發(fā)信號,再引發(fā)槽函數(shù)
teacher *tc=new teacher();
? ? student *st=new student();
? ? void(teacher::*teachersignal)()=&teacher::hungry;
? ? void(student::*studentslot)()=&student::treat;
? ? connect(tc,teachersignal,st,studentslot);
? ? QPushButton *btn=new QPushButton();
? ? btn->setParent(this);
? ? btn->setText("觸發(fā)老師餓");
? ? btn->resize(60,30);
? ? btn->move(200,100);
? ? connect(btn,&QPushButton::clicked,tc,teachersignal);
//拓展
//信號可以連接信號
//一個信號可以連接多個槽函數(shù)
//多個信號可以連接一個槽函數(shù)
//信號的參數(shù)必須大于等于槽函數(shù)且類型一一對應(yīng)
10.Lambda表達式
[](){? ? ? ? //中括號[ ]里=是值傳遞,&是引用傳遞;()里是參數(shù)
Qdebug<<"這是Lambda表達式"
}();? ? ? ? //不加小括號算聲明,加括號算是引用
Lambda表達式多用于信號和槽,引用不同類界面上面的UI,常用=,用等于的時候表達式就可以對界面上的UI進行代碼控制。