3.Qt-基础布局以及事件

基础控件 (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"
相关推荐
2603_965148111 小时前
如何解析JSON数据?API返回的商品信息处理教程
开发语言·数据库·python·自动化·json·api
ltl1 小时前
RocksDB 经典故障排查:L0、compaction 与 write stall
数据库
xfhuangfu2 小时前
Oracle中建立到CDB和PDB的连接
数据库·oracle·rpc
小小龙学IT2 小时前
DuckDB 深度实战:用 C++ 在进程内跑一个「分析型数据库」
数据库·c++
NineData2 小时前
DTCC 2026 预告|NineData CEO& 创始人叶正盛:面向 AI Agent 的数据库 DevOps 与数据复制实践
数据库·人工智能·数据库开发·devops·ninedata·数据库技术·dtcc
J_bean3 小时前
MySQL 事务是否必须手动开启?
数据库·mysql·数据库事务·自动提交·手动提交
J_bean3 小时前
MySQL InnoDB 如何检测死锁、判定死锁、处理死锁
数据库·mysql·死锁·死锁检测·处理死锁·死锁判定
知无不研4 小时前
lambda表达式的使用(3)
开发语言·c++·lambda
浪子明X4 小时前
从 MongoDB 文档到关系模型:构建可重跑、可对账的数据迁移流水线
数据库·mongodb·oracle
杜子不疼.4 小时前
国产数据库撑起固井软件自主化:金仓 × 中海油服「海恒 Cemsol」落地解析
数据库