Qt:自定义枚举

该博客介绍了如何在Qt中通过Q_ENUM宏将枚举类型声明为可外部引用的,以便在不同文件间进行交互。文章以一个具体的例子展示了如何定义一个BorderType枚举,并在多个cpp文件中使用它来处理图像的边框删除操作。通过在头文件中声明并使用Q_ENUM,使得枚举类型可以在各个组件间无缝调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


用 Q_ENUM(enum) 就能供外部引用了:

https://blog.youkuaiyun.com/qq_43248127/article/details/119446455

imagewidget.h

enum BorderType
{
    TOP_BORDER,
    BOTTOM_BORDER,
    LEFT_BORDER,
    RIGHT_BORDER
};
Q_ENUM(BorderType)

imagewidget.cpp

void ImageWidget::deleteBlackBorder(BorderType border_type)
{
    qDebug() << border_type;
    switch (border_type) {
    case BorderType::TOP_BORDER:
        break;
    case BorderType::BOTTOM_BORDER:
        break;
    case BorderType::LEFT_BORDER:
        break;
    case BorderType::RIGHT_BORDER:
        break;
    }
}

mainwindow.cpp

void MainWindow::on_action_deleteBlackBorder_triggered()
{
    QDialog *dialog = new QDialog;
    dialog->setWindowTitle("去黑边");
    dialog->setFixedSize(400, 300);
    QGridLayout *gridLayout = new QGridLayout;
    QPushButton *pushButton_top = new QPushButton("上边");
    gridLayout->addWidget(pushButton_top, 0, 1);
    connect(pushButton_top, &QPushButton::clicked, [&](){
        imageWidget->deleteBlackBorder(imageWidget->BorderType::TOP_BORDER);
    });
    QPushButton *pushButton_left = new QPushButton("左边");
    gridLayout->addWidget(pushButton_left, 1, 0);
    connect(pushButton_left, &QPushButton::clicked, [&](){
        imageWidget->deleteBlackBorder(imageWidget->BorderType::LEFT_BORDER);
    });
    QPushButton *pushButton_right = new QPushButton("右边");
    gridLayout->addWidget(pushButton_right, 1, 2);
    connect(pushButton_right, &QPushButton::clicked, [&](){
        imageWidget->deleteBlackBorder(imageWidget->BorderType::RIGHT_BORDER);
    });
    QPushButton *pushButton_bottom = new QPushButton("下边");
    gridLayout->addWidget(pushButton_bottom, 2, 1);
    connect(pushButton_bottom, &QPushButton::clicked, [&](){
        imageWidget->deleteBlackBorder(imageWidget->BorderType::BOTTOM_BORDER);
    });
    dialog->setLayout(gridLayout);
    dialog->show();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值