Qt点击按钮在附近弹出下拉框

效果

MainWindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include"toollayout.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QAction *actionIpSet;
    QAction *actionAuthorize;
    ToolLayout *myToolLayout;
private slots:
    void slot_action_triggered(QAction *action);
};
#endif // MAINWINDOW_H

MainWindow.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QHBoxLayout"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    myToolLayout=new ToolLayout();
    // 将 myToolLayout 添加到窗口的布局中
    QHBoxLayout *qHbox=new QHBoxLayout();
    qHbox->addWidget(myToolLayout);
    qHbox->addWidget(ui->pushButton_4);
    //初始化下拉菜单
    QMenu* menu = new QMenu(this);
    actionIpSet      = new QAction("按钮1", this);
    actionAuthorize   = new QAction("按钮2", this);
    menu->addAction(actionIpSet);
    menu->addAction(actionAuthorize);
    menu->addSeparator();

    menu->setWindowFlags(menu->windowFlags() | Qt::FramelessWindowHint);
    menu->setAttribute(Qt::WA_TranslucentBackground);
    //    menu->setStyleSheet(" QMenu {border-radius:5px;font-family:'Microsoft Yahei';font-size:14px;color:#fff;}"
    //                            " QMenu::item {height:30px; width:100px;padding-left:20px;border: 1px solid none;}"
    //                            "QMenu::item:selected {background-color:rgb(0,120,215);\
    //                            padding-left:20px;border: 1px solid rgb(65,173,255);}");

    ui->pushButton_4->setMenu(menu);
    // 将水平布局设置为窗口的布局
    centralWidget()->setLayout(qHbox);
    connect(menu, &QMenu::triggered, this, &MainWindow::slot_action_triggered);
}

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

//.cpp
void MainWindow::slot_action_triggered(QAction *action)
{
    if(action == actionIpSet)
    {
        //        qDebug() << "点击下拉菜单按钮1";
    }
    else if(action == actionAuthorize)
    {
        //        qDebug() << "点击下拉菜单按钮2222";
    }
}

ToolLayout.h

cpp 复制代码
#ifndef TOOLLAYOUT_H
#define TOOLLAYOUT_H

#include <QWidget>
#include"QAction"
#include"QMenu"
namespace Ui {
class ToolLayout;
}

class ToolLayout : public QWidget
{
    Q_OBJECT

public:
    explicit ToolLayout(QWidget *parent = nullptr);
    ~ToolLayout();

private:
    Ui::ToolLayout *ui;
    QAction *actionIpSet;
    QAction *actionAuthorize;
private slots:
    void slot_action_triggered(QAction *action);
};

#endif // TOOLLAYOUT_H

ToolLayout.cpp

cpp 复制代码
#include "toollayout.h"
#include "ui_toollayout.h"
#include"QMessageBox"
ToolLayout::ToolLayout(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ToolLayout)
{
    ui->setupUi(this);
    //文字居中
    ui->label_2->setAlignment(Qt::AlignCenter);
    ui->label_3->setAlignment(Qt::AlignCenter);
    ui->label_4->setAlignment(Qt::AlignCenter);
    //初始化下拉菜单
    QMenu* menu = new QMenu(this);
    actionIpSet      = new QAction("actionIpSet", this);
    actionAuthorize   = new QAction("actionAuthorize", this);
    menu->addAction(actionIpSet);
    menu->addAction(actionAuthorize);
    menu->addSeparator();

    menu->setWindowFlags(menu->windowFlags() | Qt::FramelessWindowHint);
//    menu->setAttribute(Qt::WA_TranslucentBackground);
//    menu->setStyleSheet(" QMenu {border-radius:5px;font-family:'Microsoft Yahei';font-size:14px;color:#fff;}"
//                        " QMenu::item {height:30px; width:100px;padding-left:20px;border: 1px solid none;}"
//                        "QMenu::item:selected {background-color:rgb(65,173,255);\
//                        padding-left:20px;border: 1px solid rgb(0,120,215);}");


ui->pushButton->setMenu(menu);
connect(menu, &QMenu::triggered, this, &ToolLayout::slot_action_triggered);
}

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

//.cpp
void ToolLayout::slot_action_triggered(QAction *action)
{
    if(action == actionIpSet)
    {
        QMessageBox::information(this,u8"INFO",u8"actionIpSet");
    }
    else if(action == actionAuthorize)
    {
        QMessageBox::information(this,u8"INFO",u8"actionAuthorize");
    }
}
相关推荐
badhope3 小时前
Mobile-Skills:移动端技能可视化的创新实践
开发语言·人工智能·git·智能手机·github
码云数智-园园4 小时前
微服务架构下的分布式事务:在一致性与可用性之间寻找平衡
开发语言
C++ 老炮儿的技术栈5 小时前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl5 小时前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
Liu628885 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
IT猿手5 小时前
基于控制障碍函数的多无人机编队动态避障控制方法研究,MATLAB代码
开发语言·matlab·无人机·openclaw·多无人机动态避障路径规划·无人机编队
AI科技星5 小时前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘
sunwenjian8866 小时前
Java进阶——IO 流
java·开发语言·python
波特率1152006 小时前
const关键字与函数的重载
开发语言·c++·函数重载
FL16238631296 小时前
[C#][winform]segment-anything分割万物部署onnx模型一键抠图演示
开发语言·c#