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"
相关推荐
YOU OU7 小时前
Redis基础常识与命令
数据库·redis·缓存
Scott9999HH8 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
码智社9 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海9 小时前
python 列表、元组、集合和字典
开发语言·python
萧瑟余晖10 小时前
JDK 26 新特性详解
java·开发语言
Database_Cool_10 小时前
OLTP 和 OLAP 区别详解:分析型数据库和事务型数据库怎么选(附阿里云 AnalyticDB MySQL 选型指南)
数据库·mysql·阿里云
爱写代码的阿森10 小时前
鸿蒙三方库 | harmony-utils之KvUtil键值型数据库操作详解
数据库·华为·harmonyos·鸿蒙·huawei
马优晨10 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
Database_Cool_10 小时前
单机 MySQL 迁移到分布式数据库方便吗?阿里云 PolarDB-X 100% MySQL 协议兼容零改造平滑迁移
数据库·分布式·mysql