基础控件 (Basic Widgets)
QLabel: 显示文本或图片。它通常是静态的,用户不能编辑。
QLineEdit: 单行输入框。用于输入用户名、密码、命令等。
QPushButton: 按钮。它是触发信号(Signal)的最常用控件。
c
QLabel *label = new QLabel("目标名称:");
QLineEdit *input = new QLineEdit();
QPushButton *btn = new QPushButton("开火");
// 设置占位符文本(灰色提示字)
input->setPlaceholderText("请输入打击目标...");
布局管理 (Layouts)
把控件"扔"进布局管理器里,让布局管理器帮我们设置位置。
QVBoxLayout (Vertical): 垂直布局。控件从上到下排列。
QHBoxLayout (Horizontal): 水平布局。控件从左到右排列。
QGridLayout: 网格布局。像 Excel 表格一样,指定(行, 列)。
html
使用流程:
创建一个布局(如 QVBoxLayout)。
把控件 addWidget 进去。
把这个布局设置给窗口 setLayout。
如果是this方式(布局时传入了父窗口指针(this))则这一行是默认调用的。
简单的安装方式
c
// 在构造函数里传入 'this'
QVBoxLayout *mainLayout = new QVBoxLayout(this);
// 此时 setLayout 已经被自动执行了,不用再写了
mainLayout->addWidget(button);
显式安装
c
// 1. 创建布局(不传参,暂时不指定父亲)
QVBoxLayout *mainLayout = new QVBoxLayout();
// 2. 往布局里塞东西
mainLayout->addWidget(button);
// 3. 显式地把布局安装给窗口
this->setLayout(mainLayout);
事件处理 (Event System)
事件 (Event) 是底层的物理输入
要重写(Override)父类的虚函数。进行拦截。
c
void mousePressEvent(QMouseEvent *event) override {
if (event->button() == Qt::LeftButton) {
qDebug() << "鼠标左键被按下了!";
}
}
常用对话框 (Common Dialogs)
Qt 内置了很多现成的标准窗口:
QMessageBox: 弹窗提示(报错、警告、询问)。
QFileDialog: 打开/保存文件。
QColorDialog: 选颜色。
简单应用
c
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDebug>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>
#include <QMouseEvent>
class CommanderWindow:public QWidget{
Q_OBJECT
public:
CommanderWindow(QWidget * parent=nullptr):QWidget(parent){
setupUI();//调用布局函数
//连接按钮点击信号到槽函数
connect(sendButton,// 1. 发送者:按钮
&QPushButton::clicked,// 2. 信号:被点击了
this,// 3. 接收者:本窗口
&CommanderWindow::onSendButtonClicked);// 4. 槽函数:处理点击事件
}
protected:
//构造函数
void mousePressEvent (QMouseEvent *event) override{
if (event->button() == Qt::RightButton){
qDebug() << "检测到鼠标右键,执行清空操作";
targetInput->clear(); // 清空输入框
}
// 调用父类处理,保持默认行为
QWidget::mousePressEvent(event);
}
//私有属性以及布局函数
private:
QLineEdit * targetInput;//定义输入框
QPushButton * sendButton;//定义按钮 这里就代替了信号signal
QLabel * statusLabel;//定义标签
void setupUI(){//界面布局函数
setWindowTitle("Commander");//设置窗口标题
QVBoxLayout * mainLayout=new QVBoxLayout(this);//主垂直布局 this表示是主布局(直接接管窗口)
QHBoxLayout * inputLayout=new QHBoxLayout();//输入水平布局 作为一个整体的子布局
targetInput=new QLineEdit();
targetInput->setPlaceholderText("Enter target here...");
sendButton=new QPushButton("Send");
statusLabel=new QLabel("Status: Ready"); //成员变量初始化
QLabel * titleLabel=new QLabel("Input");// 局部变量初始化 后续不需要修改静态标签内容
inputLayout->addWidget(titleLabel);
inputLayout->addWidget(targetInput);
mainLayout->addLayout(inputLayout);
mainLayout->addWidget(sendButton);
mainLayout->addWidget(statusLabel);
}
//槽函数 发射成功以后调用里面的函数
private slots:
void onSendButtonClicked(){//按钮点击槽函数
QString target=targetInput->text();
if(target.isEmpty()){
QMessageBox::warning(this,"Input Error","Please enter a target before sending the command.");
return;
}else{
statusLabel->setText("Status: Command sent to " + target);
qDebug()<<"Command sent to "<<target;
QMessageBox::information(this,"Command Sent","Command successfully sent to " + target);
}
}
};
int main(int argc,char *argv[]){
QApplication a(argc,argv);
CommanderWindow w;
w.resize(400,150);
w.show();
return a.exec();
}
#include "main.moc"