logplus/logPlus/YxzpDialog.cpp

114 lines
4.0 KiB
C++

#include "YxzpDialog.h"
YxzpDialog::YxzpDialog(QWidget *parent)
: QDialog(parent)
{
// setStyleSheet("QDialog { background-color: #888; }");
setWindowTitle("图片");
setFixedSize(600, 500); // 固定窗口大小,和示例接近
// 1. 边框属性行(示例里是占位,这里也做一个输入框)
QHBoxLayout *layoutBorder = new QHBoxLayout;
QLabel *labelBorder = new QLabel(QObject::tr("边框属性:"));
labelBorder->setMinimumWidth(60); // 统一最小宽度(可根据文本调整)
labelBorder->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
QLineEdit *editBorder = new QLineEdit;
editBorder->setReadOnly(true);
editBorder->setPlaceholderText("");
layoutBorder->setContentsMargins(0, 20, 0, 0);
layoutBorder->addWidget(labelBorder);
layoutBorder->addWidget(editBorder);
// 2. 图片路径行
QHBoxLayout *layoutPath = new QHBoxLayout;
QLabel *labelPath = new QLabel(QObject::tr("图片路径:"));
labelPath->setMinimumWidth(60); // 统一最小宽度(可根据文本调整)
labelPath->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
m_editPath = new QLineEdit;
m_editPath->setReadOnly(true); // 只读,只能通过按钮选择
QPushButton *btnSelect = new QPushButton("...");
btnSelect->setFixedWidth(80);
layoutPath->addWidget(labelPath);
layoutPath->addWidget(m_editPath);
layoutPath->addWidget(btnSelect);
// 3. 预览区域
QVBoxLayout *layoutPreview = new QVBoxLayout;
QLabel *labelPreviewTitle = new QLabel(QObject::tr("预览"));
m_labelPreview = new QLabel;
m_labelPreview->setFrameStyle(QFrame::Box | QFrame::Sunken); // 加边框
m_labelPreview->setAlignment(Qt::AlignCenter);
m_labelPreview->setMinimumSize(550, 320); // 预览区域最小尺寸
layoutPreview->addWidget(labelPreviewTitle);
layoutPreview->addWidget(m_labelPreview);
// 4. 按钮行
QHBoxLayout *layoutButtons = new QHBoxLayout;
QPushButton *btnOk = new QPushButton(QObject::tr("确定"));
QPushButton *btnCancel = new QPushButton(QObject::tr("取消"));
// layoutButtons->setContentsMargins(0, 20, 0, 0);
layoutButtons->addStretch(); // 推到右侧
layoutButtons->addWidget(btnOk);
layoutButtons->addWidget(btnCancel);
// 总布局
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(layoutBorder);
mainLayout->addLayout(layoutPath);
mainLayout->addLayout(layoutPreview);
mainLayout->addStretch();
mainLayout->addLayout(layoutButtons);
mainLayout->setContentsMargins(20, 0, 20, 10);
mainLayout->setSpacing(15);
// 信号槽连接
connect(btnSelect, &QPushButton::clicked, this, &YxzpDialog::onSelectImage);
connect(btnOk, &QPushButton::clicked, this, &YxzpDialog::onAccept);
connect(btnCancel, &QPushButton::clicked, this, &YxzpDialog::onReject);
}
QString YxzpDialog::getImagePath() const
{
return m_imagePath;
}
void YxzpDialog::onSelectImage()
{
// 弹出文件选择框,仅允许 JPG 图片
QString filePath = QFileDialog::getOpenFileName(
this,
"选择图片",
QDir::homePath(),
"JPG 图片 (*.jpg *.jpeg)"
);
if (!filePath.isEmpty())
{
m_editPath->setText(filePath);
// 加载并预览图片
QPixmap pixmap(filePath);
if (!pixmap.isNull())
{
// 按比例缩放预览
QPixmap scaledPixmap = pixmap.scaled(
m_labelPreview->size(),
Qt::KeepAspectRatio,
Qt::SmoothTransformation
);
m_labelPreview->setPixmap(scaledPixmap);
}
}
}
void YxzpDialog::onAccept()
{
m_imagePath = m_editPath->text();
accept(); // 关闭对话框,返回 QDialog::Accepted
}
void YxzpDialog::onReject()
{
m_imagePath.clear();
reject(); // 关闭对话框,返回 QDialog::Rejected
}