頭文件的小技巧
#include <QtWidgets>
// 在自動生成的 .h 里面加上此句
適用條件:
QT 的內存管理
當父窗體被關閉時,子部件的內存會自動釋放。
對象樹是一種管理對象生命周期的機制。當一個對象被添加到另一個對象的子對象列表中時,Qt 會自動將其設置為父對象的子對象,并在父對象被銷毀時自動銷毀其所有子對象。這種機制可以避免內存泄漏和懸空指針的問題。
UI 編輯器
注意事項
UI 編輯器會在項目構建目錄中自動生成一個 ui_xxx.h(構建一次后才會生成),來表示 UI 編輯器界面的代碼,屬于自動生成的,一定不要修改。代碼中如果要修改界面中的某個部件的屬性,應該用 ui->xxx 獲取到部件,然后調用相應的方法修改。
// 自動生成的 ui_widget.h(可忽略)/********************************************************************************
** Form generated from reading UI file 'widget.ui'
**
** Created by: Qt User Interface Compiler version 5.2.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/#ifndef UI_WIDGET_H
#define UI_WIDGET_H#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QWidget>
#include <mybutton.h>QT_BEGIN_NAMESPACEclass Ui_Widget
{
public:Mybutton *newBtn;QLineEdit *lineEdit;void setupUi(QWidget *Widget){if (Widget->objectName().isEmpty())Widget->setObjectName(QStringLiteral("Widget"));newBtn = new Mybutton(Widget);newBtn->setObjectName(QStringLiteral("newBtn"));newBtn->setGeometry(QRect(410, 510, 112, 34));lineEdit = new QLineEdit(Widget);lineEdit->setObjectName(QStringLiteral("lineEdit"));lineEdit->setGeometry(QRect(220, 120, 561, 231));retranslateUi(Widget);QMetaObject::connectSlotsByName(Widget);} // setupUivoid retranslateUi(QWidget *Widget){Widget->setWindowTitle(QApplication::translate("Widget", "Widget", 0));newBtn->setText(QApplication::translate("Widget", "newBtn", 0));} // retranslateUi};namespace Ui {class Widget: public Ui_Widget {};
} // namespace UiQT_END_NAMESPACE#endif // UI_WIDGET_H
信號與槽
在設計界面,右鍵相應的組件,選擇“轉到槽”選項,會自動跳轉到槽函數。
這種方法不會有 connect 的體現,遵循的是 qt 內部名字匹配規則。
匹配規則:on_AAA_BBB —— 代表連接 AAA 對象 的 BBB 信號 到此槽。
比如要響應 對象 myBtn 的 clicked 信號,就會生成 on_myBtn_clicked 的槽。
// widget.h#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private slots:void on_loginBtn_clicked(); // 連接 對象loginBtn 的 clicked信號 到 此槽private:Ui::Widget *ui;
};#endif // WIDGET_H
// widget.cpp#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::on_loginBtn_clicked()
{if (ui->usrEdit->text() == "0828" && ui->pwdEdit->text() == "0828"){this->close();}
}
客制化組件
按鈕的樣式及功能
定制一個按鈕,實現自定義樣式及功能。比如每個按鈕擁有自己的 key 值,按鈕字體的默認顏色是紅色,背景色灰色。大小固定為 90*70。
mybutton.h
#ifndef MYBUTTON_H
#define MYBUTTON_H#include <QPushButton>
#include <QTWidgets>class Mybutton : public QPushButton
{Q_OBJECTint key;
public:explicit Mybutton(QWidget *parent = 0);int getKey() { return this->key; }void setKey(int key) { this->key = key; }signals:public slots:};#endif // MYBUTTON_H
mybutton.cpp
#include "mybutton.h"Mybutton::Mybutton(QWidget *parent) :QPushButton(parent)
{key = 0;this->setStyleSheet("color: red; background-color: grey");this->setFixedSize(90, 70);this->setText("默認文字");}
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QtWidgets>
#include "mybutton.h"namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private slots:private:Ui::Widget *ui;Mybutton *btn;
};#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);this->resize(480, 300);btn = new Mybutton(this);qDebug() << "Key(before): " << btn->getKey();btn->setKey(17);qDebug() << "Key(after): " << btn->getKey();
}Widget::~Widget()
{delete ui;
}
提升部件
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);this->resize(480, 300);// btn = new Mybutton(this);
// qDebug() << "Key(before): " << btn->getKey();
// btn->setKey(17);
// qDebug() << "Key(after): " << btn->getKey();qDebug() << "Key: " << ui->newBtn->getKey();
}Widget::~Widget()
{delete ui;
}
內建對話框
對話框幫助文檔要關注 Static Public Members 部分,參考幫助文檔 demo 使用即可。
QMessageBox
QInputDialog
QColorDialog
QFontDialog
QFileDialog
QString getOpenFileName(QWidget * parent = 0, const QString & caption = QString(), \const QString & dir = QString(), const QString & filter = QString(), \QString * selectedFilter = 0, Options options = 0);功能:打開文件對話框
參數:parent:父窗口,一般填thiscaption:彈出窗口的標題dir:默認打開目錄filter:過濾條件剩余參數默認即可
返回值:成功會返回打開的文件路徑,如果失敗那么返回空
彈出文件選擇窗口
在界面加入一個按鈕和一個文本編輯框,點擊按鈕后彈出文件選擇窗口,選擇文件后把文件絕對路徑增加到文本編輯框內。取消選擇彈出對話框提示“打開失敗”。
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QTWidgets>
#include "mybutton.h"namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private slots:void on_newBtn_clicked();private:Ui::Widget *ui;Mybutton *btn;
};#endif // WIDGET_H
打開生成的 ui_widget.h 文件,找到拖拽產生的 QLineEdit,復制對象名字(直接記住也行)。
在 widget.cpp 中,使用復制下來的名字(記住現找也行)。
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::on_newBtn_clicked()
{QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"e:", tr("Files (*.chm)"));if (fileName.isEmpty()){qDebug() << "Failed to open file";return;}ui->lineEdit->setText(fileName); // lineEdit 名字 與 UI 生成的名字 保持一致
}