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"
相关推荐
Forget_8550几秒前
RHEL——NoSQL集群技术
数据库·nosql
2301_764441336 分钟前
使用python构建的STAR实验ΛΛ̄自旋关联完整仿真
开发语言·python·算法
共享家95277 分钟前
Java入门( 异常 )
java·开发语言·php
御形封灵8 分钟前
基于canvas的路网编辑交互
开发语言·javascript·交互
xifangge202511 分钟前
Python 爬虫实战:爬取豆瓣电影 Top250 数据并进行可视化分析
开发语言·爬虫·python
SunnyDays101112 分钟前
C# 实战:快速查找并高亮 Word 文档中的文字(普通查找 + 正则表达式)
开发语言·c#
kaoshi100app15 分钟前
本周,河南二建报名公布!
开发语言·人工智能·职场和发展·学习方法
421!15 分钟前
ESP32学习笔记之GPIO
开发语言·笔记·单片机·嵌入式硬件·学习·算法·fpga开发
wertyuytrewm20 分钟前
自动化与脚本
jvm·数据库·python
Hello.Reader22 分钟前
PySpark DataFrame 快速入门创建、查询、分组、读写、SQL 实战一篇讲透
数据库·sql·spark