仿qq界面(简略版)

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPoint>
#include <QMenu>
#include <QAction>
#include <QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
public:
    // 声明菜单
    QMenu * m_menu;
    // 声明菜单项
    QAction * m_Action_1;
    QAction * m_Action_2;
    QAction * m_Action_3;
    QAction * m_Action_4;
    
private slots:
    void on_Button_set_Icon_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    
    // ***********************  窗口位置  ***********************
    
    // 读取 窗口信息  用的 矩形类  QRect
    qDebug() << this->frameGeometry();
    
    // 设置窗口的 坐标 尺寸
    this->setGeometry(0 , 0 , 500 , 500);
    
    
    
    // 移动窗口坐标
    this->move(50 , 50);
    
    
    // ***********************  窗口尺寸  ***********************
    
    // 获取窗口尺寸
    qDebug() << this->size();
    
    
    // 设定窗口尺寸
    this->resize(800 , 800);
    
    // 设置固定尺寸  鼠标不能拖动
    // this->setFixedSize(500 , 500);
    
    
    // 设定最大 尺寸
    //this->setMaximumSize(800 , 800);
    
    // 设定最小 尺寸
    this->setMinimumSize(500 , 500);
    
    // *********************** 标题和图标  ***********************

    // *********************** 信号 ***********************
    // 设置右键策略
    
    this->setContextMenuPolicy(Qt::CustomContextMenu);
    
    // 抓右键点击信号
 connect(this, &QWidget::customContextMenuRequested, this, [=](const QPoint &pos) {
        qDebug() << "右键点击了窗口" << pos;

        // 构造菜单
        m_menu = new QMenu("文件操作", this);

        // 构造菜单项目
        m_Action_1 = new QAction(QIcon(R"(D:\Project\hqyj_2411\07_QAction\头像图标.png)"), "打开文件", this);
        m_Action_2 = new QAction("关闭文件", this);
        m_Action_3 = new QAction("保存文件", this);
        m_Action_4 = new QAction("另存为", this);

        // 快捷键
        m_Action_1->setShortcut(QKeySequence("Ctrl+O")); // 可选择使用 Ctrl+O 作为打开文件的快捷键
        m_Action_1->setToolTip(QString("我是一个打开文件的按钮,请你帮我打开文件"));

        // 添加菜单项
        m_menu->addAction(m_Action_1);
        m_menu->addSeparator();
        m_menu->addAction(m_Action_2);
        m_menu->addAction(m_Action_3);
        m_menu->addAction(m_Action_4);

        // 连接菜单项
        connect(m_Action_1, &QAction::triggered, this, [=]() {
            qDebug() << "打开文件";
            // 在这里处理打开文件的逻辑
        });

        connect(m_Action_2, &QAction::triggered, this, [=]() {
            qDebug() << "关闭文件";
            // 在这里处理关闭文件的逻辑
        });

        connect(m_Action_3, &QAction::triggered, this, [=]() {
            qDebug() << "保存文件";
            // 在这里处理保存文件的逻辑
        });

        connect(m_Action_4, &QAction::triggered, this, [=]() {
            qDebug() << "另存为";
            // 在这里处理另存为的逻辑
        });

        // 显示右键菜单
        m_menu->exec(mapToGlobal(pos));
    });

    
    // 关闭
    connect(ui->Button_close , &QPushButton::clicked , this , &QWidget::close);
    
    // 最小化
    connect(ui->Button_showMin , &QPushButton::clicked , this , &QWidget::showMinimized);
    
    // 最大化
    connect(ui->Button_showMax , &QPushButton::clicked , this , &QWidget::showMaximized);
    
    
}

Widget::~Widget()
{
    delete ui;
}

main.cpp

#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

最后呈现的效果

进一步完善

将上面三个按钮换成图片

让按钮随着窗口大小的变化而变化

美化界面

改变菜单

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值