qt_傳智播客_1

簡介

  • Qwidget是QMainWindow和QDialog的父類,QMainWindow有狀態欄和工具欄,QDialog有對話框
  • 版本控制工具:svn、vss、git

.pro文件解釋和qt快捷鍵

  • QT += core gui QT包含模塊在這裏插入圖片描述
  • greaterThan(QT_MAJOR_VERSION, 4): QT += widgets大於4版本包含模塊
  • DEFINES += QT_DEPRECATED_WARNINGS
  • Ctrl + r運行;Ctrl + b編譯
  • ctrl + shift + ↑ 或者 ↓ 整行移動
  • 幫助文檔F1
  • 自動對齊 ctrl + i
  • 同名.h和.cpp切換 F4

QT_QPushButton

通過一個例子就能學會基礎用法。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //設置按鈕
    QPushButton * btn = new QPushButton();
    //設置父窗口
    btn->setParent(this);
    //設置文本
    btn->setText("first button");
    //設置按鈕的第二種方法
    QPushButton * btn2 = new QPushButton("second button",this);
    
    //設置窗口大小
    resize(600,400);
    //設置固定窗口大小
//    setFixedSize(600,400);
    //設置標題
    setWindowTitle("My second window");
}

MainWindow::~MainWindow()
{
    delete ui;
}


解釋一下文中的MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow)MainWindow::~MainWindow()
前者是多繼承了QMainWindow(parent)和 ui(new Ui::MainWindow),並且聲明瞭MainWindow成員函數;後者是定義了一個析構函數。
在這裏插入圖片描述
中文亂碼改ascii爲unicode即可。

對象樹

當你創建一個QObiect對象時,會看到QObiect的構造函數接收一個Q0biect指針作爲參數,這個參數就是parent,也就是父對象指針。
這相當於,在創建Object對象時,可以提供一個其父對象,我們創建的這個Obiest對象會自動添加到其父對象的children列表。
當父對象析構的時候,這個列表中的所有對象也會被折構。(注意,這裏的父對象並不是繼承意義上的父類!)這種機制在GUI程序設計中相當有用。例如,一個按鈕有一個9Shortcut(快捷鍵)對象作爲其子對象。當我們刪除按鈕的時候,這個快捷鍵理應被刪除。這是合理的。

只需要記住,析構從下往上。只需要指定父類,那麼就默認指定一個析構方法。
並且,每一個類最好都創建時指定父類。

QT座標系

左上角爲0,0點
x以右爲正方向,y以下爲正方向。

信號Singal和槽Slot

connect(信號的發送者,具體信號,信號的接收者,信號的處理()槽))
信號槽的優點,鬆散耦合。
信號發送端和接收端本身是沒有關聯的,通過connect將兩者耦合在一起。

Signal類:
clicked——點擊事件;
released——釋放

例子:

//需求點擊我的按鈕關閉窗口
//參數1信號的發送者 參數2發送的信號(函數的地址) 參數3信號的接受者 參數4處理的槽函數
connect(btn,&QPushButton::clicked,this,&MainWindow::close);

自定義的信號和槽

信號:返回值爲void,只需要聲明,不需要實現。可以有參數,可以重載。寫在signals下。
槽函數:需要聲明,也需要實現,可以有參數,可以重載。
觸發信號:emit

案例:
teacher.h

#ifndef TEACHER_H
#define TEACHER_H

#include <QObject>

class Teacher : public QObject
{
    Q_OBJECT
public:
    explicit Teacher(QObject *parent = nullptr);

signals:
    //自定義信號寫到signals底下
    //返回值是void,只需要聲明,不需要實現
    //可以有參數,可以重載
    void hungry();
};

#endif // TEACHER_H

student.h

#ifndef STUDENT_H
#define STUDENT_H

#include <QObject>

class Student : public QObject
{
    Q_OBJECT
public:
    explicit Student(QObject *parent = nullptr);

    void treat();
};

#endif // STUDENT_H

student.cpp

#include "student.h"
#include <QDebug>
Student::Student(QObject *parent) : QObject(parent)
{

}

void Student::treat(){
    qDebug()<<"請老師喫飯";
}

mywidget.cpp

#include "mywidget.h"
#include "ui_mywidget.h"
#include <QPushButton>



myWidget::myWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::myWidget)
{
    ui->setupUi(this);
    //創建一個老師的對象和學生的對象

    this->zt = new Teacher(this);
    this->st = new Student(this);

    //創建連接
    connect(zt,&Teacher::hungry,st,&Student::treat);

    //調用下課函數
    classIsOver();
}

myWidget::~myWidget()
{

}

void myWidget::classIsOver(){
    emit zt->hungry();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章