diff --git a/kybackup/component/backuplistwidget.cpp b/kybackup/component/backuplistwidget.cpp index 44bcc66..05c2e5a 100755 --- a/kybackup/component/backuplistwidget.cpp +++ b/kybackup/component/backuplistwidget.cpp @@ -11,6 +11,7 @@ #include "component/imageutil.h" #define WIDTH_ITEM 36 +#define MERGE_IN 8 MyItemWidget::MyItemWidget(QWidget* parent) : QWidget(parent) @@ -33,13 +34,22 @@ MyItemWidget::MyItemWidget(QWidget* parent) : hlayout->addWidget(m_label); hlayout->addWidget(m_buttonDelete); - hlayout->setAlignment(Qt::AlignLeft); + // hlayout->setAlignment(Qt::AlignLeft); connect(this, &MyItemWidget::setScrollWidth, this, [=](int width) { - this->m_scrollWidth = width; int labelWidth = this->m_label->width(); - this->m_label->setFixedWidth(labelWidth + (this->m_scrollWidth - width)); + this->m_extWidth = 0; + // 隐藏滚动条时会增加右侧边距 + if (this->m_scrollWidth > 0 && width == 0) + this->m_extWidth = -1 * MERGE_IN; + // 显示滚动条时去掉右侧边距 + else if (this->m_scrollWidth == 0 && width > 0) + this->m_extWidth = MERGE_IN; + this->m_label->setFixedWidth(labelWidth + (this->m_scrollWidth - width) + this->m_extWidth); + this->m_label->setElidedText(this->m_text, Qt::ElideLeft); + this->m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + this->m_scrollWidth = width; }); connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::fontChanged, this, [=](int fontSize) { @@ -51,16 +61,24 @@ MyItemWidget::MyItemWidget(QWidget* parent) : }); connect(this, &MyItemWidget::selected, this, [=](bool checked) { + int labelWidth = this->m_label->width(); if (checked) { this->m_buttonDelete->setVisible(true); - this->m_label->setFixedWidth(this->width() - 12 - 20 - this->m_scrollWidth); + this->m_label->setFixedWidth(labelWidth - 5 - m_buttonDelete->width()); this->m_label->setElidedText(this->m_text, Qt::ElideLeft); - this->m_label->setStyleSheet("color:white"); + this->m_label->setFontColor(Qt::white); + this->m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + this->m_label->setStyleSheet("QLabel {color:white}" + "QToolTip {color:palette(windowText)}"); + this->m_checked = true; } else { this->m_buttonDelete->setVisible(false); - this->m_label->setFixedWidth(this->width() - 7 - this->m_scrollWidth); + this->m_label->setFixedWidth(labelWidth + 5 + m_buttonDelete->width()); this->m_label->setElidedText(this->m_text, Qt::ElideLeft); - this->m_label->setStyleSheet("color:palette(windowText)"); + this->m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + this->m_label->setStyleSheet("QLabel {color:palette(windowText)}" + "QToolTip {color:palette(windowText)}"); + this->m_checked = false; } }); @@ -72,7 +90,7 @@ MyItemWidget::MyItemWidget(QWidget* parent) : void MyItemWidget::setText(const QString& text) { m_text = text; - m_label->setFixedWidth(this->width() - 7 - this->m_scrollWidth); + // m_label->setFixedWidth(this->width() - 7 - this->m_scrollWidth); m_label->setElidedText(m_text, Qt::ElideLeft); m_label->setToolTip(m_text); } @@ -81,8 +99,9 @@ MyItemWidget::~MyItemWidget() { } -BackupListWidget::BackupListWidget(QWidget *parent /*= nullptr*/) : - QListWidget(parent) +BackupListWidget::BackupListWidget(QWidget *parent /*= nullptr*/, QHBoxLayout *parentLayout /*= nullptr*/) : + QListWidget(parent), + m_parentLayout(parentLayout) { setSortingEnabled(false); setAcceptDrops(true); @@ -90,7 +109,12 @@ BackupListWidget::BackupListWidget(QWidget *parent /*= nullptr*/) : // setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // 设置为无边框,默认时为StyledPanel - // setFrameShape(QListWidget::NoFrame); + setFrameShape(QListWidget::NoFrame); + if (parentLayout != nullptr) { + parentLayout->setContentsMargins(MERGE_IN, MERGE_IN, MERGE_IN, MERGE_IN); + setGridSize(QSize(this->width(), 40 + MERGE_IN / 2)); + } else + setGridSize(QSize(this->width(), 40)); // 列表为空时,展示一个“+”号图标和拖拽文件提示 m_plusLogo = new QLabel; @@ -163,11 +187,15 @@ bool BackupListWidget::appendItem(const QString &text) connect(this, &BackupListWidget::setScrollWidth, widget, &MyItemWidget::setScrollWidth); // 滚动条是否展示 QScrollBar *vertScroll = verticalScrollBar(); - if (vertScroll->isVisible()) - emit this->setScrollWidth(vertScroll->size().width()); + int scrollWidth = 0; + if (vertScroll->isVisible()) { + if (m_parentLayout != nullptr) + m_parentLayout->setContentsMargins(MERGE_IN, MERGE_IN, 0, MERGE_IN); + scrollWidth = vertScroll->size().width(); + item->setSizeHint(QSize(this->width() - 5 - scrollWidth, WIDTH_ITEM)); + emit this->setScrollWidth(scrollWidth); + } - QRect rect = this->visualItemRect(item); - widget->setBaseSize(rect.width(), rect.height()); widget->setText(text); m_List << text; @@ -184,8 +212,11 @@ bool BackupListWidget::appendItem(const QString &text) emit this->deleteEmpty(); } else { QScrollBar *vertScrollBar = verticalScrollBar(); - if (!vertScrollBar->isVisible()) + if (!vertScrollBar->isVisible()) { + if (m_parentLayout != nullptr) + m_parentLayout->setContentsMargins(MERGE_IN, MERGE_IN, MERGE_IN, MERGE_IN); emit this->setScrollWidth(0); + } } }); diff --git a/kybackup/component/backuplistwidget.h b/kybackup/component/backuplistwidget.h index 616980c..d070128 100755 --- a/kybackup/component/backuplistwidget.h +++ b/kybackup/component/backuplistwidget.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "mylabel.h" class MyItemWidget : public QWidget @@ -32,14 +33,16 @@ private: QString m_text; MyLabel *m_label; QPushButton *m_buttonDelete; - int m_scrollWidth = 0; + int m_scrollWidth = 0; //父窗口QListWidget的滚动条宽度 + int m_extWidth = 0; //去掉父窗口QListWidget的父窗口QFrame的右侧边距时扩展的宽度 + bool m_checked = false; }; class BackupListWidget : public QListWidget { Q_OBJECT public: - explicit BackupListWidget(QWidget *parent = nullptr); + explicit BackupListWidget(QWidget *parent = nullptr, QHBoxLayout *parentLayout = nullptr); virtual ~BackupListWidget(); // 添加列表项,注意使用本类的功能不能用addItem等基类添加项的方法 @@ -78,6 +81,8 @@ private: int m_type = QListWidgetItem::ItemType::UserType + 1; QStringList m_List; QStringList m_pathLimit; + + QHBoxLayout *m_parentLayout; }; #endif // REMOVABLELISTWIDGET_H diff --git a/kybackup/module/databackup.cpp b/kybackup/module/databackup.cpp index 27f55a4..1d6749c 100755 --- a/kybackup/module/databackup.cpp +++ b/kybackup/module/databackup.cpp @@ -370,10 +370,16 @@ void DataBackup::initSecondWidget() getPathsLimit(pathLimits, siderUrls); // 备份列表 - BackupListWidget *listWidget = new BackupListWidget; + QFrame *listFrame = new QFrame; + listFrame->setFrameShape(QFrame::StyledPanel); + listFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + QHBoxLayout * hlayoutFrame = new QHBoxLayout; + listFrame->setLayout(hlayoutFrame); + BackupListWidget *listWidget = new BackupListWidget(listFrame, hlayoutFrame); listWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); listWidget->setPathLimit(pathLimits); - hlayoutLine5->addWidget(listWidget); + hlayoutFrame->addWidget(listWidget); + hlayoutLine5->addWidget(listFrame); hlayoutLine5->addSpacing(40); vlayout->addLayout(hlayoutLine5); @@ -687,10 +693,16 @@ void DataBackup::initSecondWidget_inc() QList siderUrls; getPathsLimit(pathLimits, siderUrls); // 备份列表 - BackupListWidget *listWidget = new BackupListWidget; + QFrame *listFrame = new QFrame; + listFrame->setFrameShape(QFrame::StyledPanel); + listFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + QHBoxLayout * hlayoutFrame = new QHBoxLayout; + listFrame->setLayout(hlayoutFrame); + BackupListWidget *listWidget = new BackupListWidget(listFrame, hlayoutFrame); listWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); listWidget->setPathLimit(pathLimits); - hlayoutLine5->addWidget(listWidget); + hlayoutFrame->addWidget(listWidget); + hlayoutLine5->addWidget(listFrame); hlayoutLine5->addSpacing(40); vlayout->addLayout(hlayoutLine5); @@ -1250,7 +1262,7 @@ void DataBackup::initForthWidget() } // 支持输入中英文数字和部分字符 // QRegExp regx("^[\u4e00-\u9fa5a-zA-Z0-9~!@#$%^&*()-_+={}':;'\\[\\].<>/? ¥()——;《》‘’:“”、?]+$"); //其中匹配中文[\u4e00-\u9fa5] - QRegExp regx("[^<>,; `|#\\^\\$]+");//regx("^[\u4e00-\u9fa5a-zA-Z0-9-@& +():'()——《》‘’:“”]*$"); + QRegExp regx("[^<>,;`|#\\^\\$]+");//regx("^[\u4e00-\u9fa5a-zA-Z0-9-@& +():'()——《》‘’:“”]*$"); InputValidator *validator = new InputValidator(regx); editBackupName->setValidator(validator); labelBackupName->setFixedHeight(editBackupName->height()); @@ -1282,6 +1294,8 @@ void DataBackup::initForthWidget() // vlayout->addLayout(hlayoutCenterLine2); connect(validator, &InputValidator::checked, this, [=](bool valid, QString in, int pos, QChar c) { + if (in.isEmpty()) + return; QString text = editBackupName->text(); if (valid) { if (pos >= MAX_LEN_BACKUPNAME) { diff --git a/kybackup/module/operationlog.cpp b/kybackup/module/operationlog.cpp index e1cf404..7c52c60 100755 --- a/kybackup/module/operationlog.cpp +++ b/kybackup/module/operationlog.cpp @@ -77,6 +77,7 @@ void OperationLog::initFirstWidget() m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); m_tableWidget->setShowGrid(false); m_tableWidget->hideColumn(1); // 隐藏uuid + m_tableWidget->setFrameShape(QTableWidget::NoFrame); hlayoutLine3->addWidget(m_tableWidget); vlayout->addLayout(hlayoutLine3); diff --git a/kybackup/module/systembackup.cpp b/kybackup/module/systembackup.cpp index df554dd..55a9086 100755 --- a/kybackup/module/systembackup.cpp +++ b/kybackup/module/systembackup.cpp @@ -792,7 +792,7 @@ void SystemBackup::initForthWidget() editBackupName->setPlaceholderText(QDateTime::currentDateTime().toString("yy-MM-dd hh:mm:ss")); // 支持输入中英文数字和部分字符 // QRegExp regx("^[\u4e00-\u9fa5a-zA-Z0-9~!@#$%^&*()-_+={}':;'\\[\\].<>/? ¥()——;《》‘’:“”、?]+$"); //其中匹配中文[\u4e00-\u9fa5] - QRegExp regx("[^<>,; `|#\\^\\$]+");//regx("^[\u4e00-\u9fa5a-zA-Z0-9-@& +():'()——《》‘’:“”]*$"); + QRegExp regx("[^<>,;`|#\\^\\$]+");//regx("^[\u4e00-\u9fa5a-zA-Z0-9-@& +():'()——《》‘’:“”]*$"); InputValidator *validator = new InputValidator(regx); editBackupName->setValidator(validator); labelBackupName->setFixedHeight(editBackupName->height()); @@ -814,6 +814,8 @@ void SystemBackup::initForthWidget() labelError->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); connect(validator, &InputValidator::checked, this, [=](bool valid, QString in, int pos, QChar c) { + if (in.isEmpty()) + return; QString text = editBackupName->text(); if (valid) { if (pos >= MAX_LEN_BACKUPNAME) {