?非界面編程
QT編譯的時候,依賴于 .pro 配置文件:
SOURCES: 所有需要參與編譯的 .cpp 源文件
HEADERS:所有需要參與編譯的.h 頭文件
QT:所有需要參與編譯的 QT函數庫
.pro文件一旦修改,注意需要鍵盤按 ctrl+s 才能加載最新的配置文件
標準輸出:QDebug 類
#include <QDebug>
qDebug() << 輸出的數據;
qDebug() 是全局函數,?返回 QDebug 對象【 c++中的cout】
QT所有的類,以 Q 開頭
第二個字母通常也大寫
界面編程
使用QT界面,顯示 hello world
查找函數
1:哪個類的
2: CONTENTS? ? ? ?: 目錄
3:類名 class:基礎信息
#include :依賴的頭文件
QT:依賴庫文件,在 .pro 文件中 QT標簽后面追加
Inherits: 基類
Inherits by :有哪些派生類
4:Public Functions:????????類中所有公開函數
QWidget
比較底層的窗口類,是很多很多窗口類的基類
QWidget的函數,很多窗口繼承
QWidget 類:一般作為一個復合型窗口的底座
一個窗口是復雜的,里面復合了很多子窗口
子窗口,都需要依賴底座父窗口去實現
QWidget就是底座功能
設置幾何圖形:
setGeometry(x,y,w,h)?左上角的坐標 x,y ,?圖形的寬度高度獲取 x軸坐標和y軸坐標:
int x() const
int y() const
?QLabel
這個窗口專門承載文字和圖片
承載文字
1 設置文字 setText(“”)
2 獲取文字 QString text()
調整文字格式
QFont 類對象 設置文字格式
QFont font
font.setBold(1) 設置文字是否粗體
font.setItalic(1) 設置文字是否斜體
font.setPointSize(int pointSize) 設置文字大小
將調整好的文字設置給 label
QLabel lb
lb.setFont(font)
?文字調整顏色
QPalette pe;
pe.setColor(QPalette::WindowText,Qt::red);//第一個參數是宏,設置label中哪部分的顏色
lb.setPalette(pe);
文字居中
?//lb.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
lb.setAlignment(Qt::AlignCenter);
承載圖片
?QLabel lb;
?lb.setPixmap()
QLineEdit
文本編輯器,單行文本輸入,支持設置占位文字和隱藏輸入內容
使用代碼輸入/獲取文字
setText(文字)
QString text()
設置占位文字
setPlaceholderText(占位文字)
?輸入的文字隱藏
setEchoMode(宏定義)
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include<QDebug>
int main(int argc, char *argv[])
{QWidget window;QVBoxLayout *layout = new QVBoxLayout(&window);// 創建 QLineEditQLineEdit *lineEdit = new QLineEdit;layout->addWidget(lineEdit);// 創建按鈕QPushButton *button = new QPushButton("Get Text");layout->addWidget(button);// 連接按鈕點擊信號到槽函數QObject::connect(button, &QPushButton::clicked, [lineEdit]() {QString text = lineEdit->text(); // 獲取輸入的文字qDebug() << "Text:" << text;});// 設置文字//lineEdit->setText("Hello, QLineEdit!");//lineEdit->setPlaceholderText("占位文字");// lineEdit->setEchoMode(QLineEdit::Normal);//正常顯示// lineEdit->setEchoMode(QLineEdit::NoEcho);//不顯示// lineEdit->setEchoMode(QLineEdit::Password);//密碼隱藏//lineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);// 輸入的時候不隱藏,輸完再隱藏.編輯完,指的是,操作對象不再是當前窗口,也就是所謂的 "失去焦點"window.show();return app.exec(); // 啟動事件循環
}
QTextEdit
允許有回車的文本編輯器
多行文本輸入,支持富文本和圖片插入。
QString toPlainText()????????獲取文本編輯器中的文字
setText(文字)????????????????設置文字
設置圖片
依賴 html 格式的代碼
html的語句,直接通過 setText 去設置
QT針對HTML語句做過簡化,最終
te.setText("<img src=D:/qrc/shi.jpg width=300 hegith=300>");
toHtml:將文本編輯器中的數據,轉換成html語句
int main(int argc, char *argv[])
{QApplication app(argc, argv);QWidget window;QVBoxLayout *layout = new QVBoxLayout(&window);// 創建 QTextEditQTextEdit *textEdit = new QTextEdit;layout->addWidget(textEdit);// 創建按鈕QPushButton *button = new QPushButton("Insert Image");layout->addWidget(button);// 連接按鈕點擊信號到槽函數QObject::connect(button, &QPushButton::clicked, [textEdit]() {// 插入圖片QTextCursor cursor = textEdit->textCursor();QImage image(":/path/to/image.png"); // 圖片路徑textEdit->document()->addResource(QTextDocument::ImageResource, QUrl("image"), image);cursor.insertImage("image");});window.show();return app.exec();
}
?
QPushButton
普通按鈕
設置按鈕文字
QPushButton btn
btn.setText("文字")