用 Q_ENUM(enum) 就能供外部引用了:
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();
}