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"
相关推荐
Dxy123931021610 小时前
MySQL如何做读写分离架构
数据库·mysql·架构
CHANG_THE_WORLD10 小时前
Python 学习三 Python字符串拼接详解
开发语言·python·学习
诸葛老刘11 小时前
next.js 框架中的约定的特殊参数名称
开发语言·javascript·ecmascript
霸王大陆11 小时前
《零基础学 PHP:从入门到实战》模块十:从应用到精通——掌握PHP进阶技术与现代化开发实战-2
android·开发语言·php
釉色清风11 小时前
在openEuler玩转Python
linux·开发语言·python
han_hanker11 小时前
这里使用 extends HashMap<String, Object> 和 类本身定义变量的优缺点
java·开发语言
@小码农11 小时前
2025年北京海淀区中小学生信息学竞赛第二赛段C++真题
开发语言·数据结构·c++·算法
毕设十刻11 小时前
基于Vue的考勤管理系统8n7j8(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
sulikey11 小时前
C++模板初阶详解:从函数模板到类模板的全面解析
开发语言·c++·模板·函数模板·类模板