新增自定义控件,修复查询添加问题

This commit is contained in:
taocong
2026-06-01 19:32:23 +08:00
parent fad539e127
commit cc7fc51399
9 changed files with 232 additions and 55 deletions
+116
View File
@@ -0,0 +1,116 @@
#include "elasearchedit.h"
#include <QHBoxLayout>
#include "ElaIcon.h"
ElaSearchEdit::ElaSearchEdit(QWidget* parent)
: QWidget(parent), m_exactMatch(false)
{
initUI();
}
ElaSearchEdit::~ElaSearchEdit()
{
}
void ElaSearchEdit::initUI()
{
QHBoxLayout* mainLayout = new QHBoxLayout(this);
mainLayout->setSpacing(0);
mainLayout->setContentsMargins(0, 0, 0, 0);
m_lineEdit = new ElaLineEdit(this);
m_lineEdit->setFixedHeight(35);
m_lineEdit->setPlaceholderText("输入查询内容");
m_lineEdit->setIsClearButtonEnable(false);
QHBoxLayout* inputLayout = new QHBoxLayout(m_lineEdit);
inputLayout->setContentsMargins(0, 0, 4, 0);
inputLayout->addStretch();
m_exactMatchBtn = addToolButton(m_lineEdit, ElaIconType::FontCase, "区分大小写", QSize(26, 26), true, false);
inputLayout->addWidget(m_exactMatchBtn);
resetInputMargin();
mainLayout->addWidget(m_lineEdit);
m_searchBtn = addToolButton(this, ElaIconType::MagnifyingGlass, "搜索", QSize(35, 35), false, false);
mainLayout->addWidget(m_searchBtn);
connectSignals();
}
void ElaSearchEdit::connectSignals()
{
connect(m_lineEdit, &ElaLineEdit::textChanged, this, &ElaSearchEdit::textChanged);
connect(m_lineEdit, &ElaLineEdit::returnPressed, this, &ElaSearchEdit::returnPressed);
connect(m_searchBtn, &ElaToolButton::clicked, this, &ElaSearchEdit::searchClicked);
connect(m_exactMatchBtn, &ElaToolButton::clicked, this, [this](bool checked) {
m_exactMatch = checked;
emit exactMatchChanged(checked);
});
}
void ElaSearchEdit::resetInputMargin()
{
m_lineEdit->setTextMargins(0, 0, 30, 0);
}
ElaToolButton* ElaSearchEdit::addToolButton(QWidget* parent, ElaIconType::IconName icon, const QString& tooltip, const QSize& size, bool checkable, bool checked)
{
ElaToolButton* btn = new ElaToolButton(parent);
btn->setElaIcon(icon);
btn->setFixedSize(size);
btn->setToolTip(tooltip);
btn->setCheckable(checkable);
btn->setChecked(checked);
btn->setCursor(Qt::PointingHandCursor);
return btn;
}
QString ElaSearchEdit::text() const
{
return m_lineEdit->text();
}
void ElaSearchEdit::setText(const QString& text)
{
m_lineEdit->setText(text);
}
void ElaSearchEdit::setPlaceholderText(const QString& placeholderText)
{
m_lineEdit->setPlaceholderText(placeholderText);
}
void ElaSearchEdit::setFixedSize(const QSize& size)
{
m_lineEdit->setFixedSize(size.width() - 35, size.height());
m_searchBtn->setFixedSize(35, size.height());
QWidget::setFixedSize(size);
}
void ElaSearchEdit::setFixedSize(int w, int h)
{
m_lineEdit->setFixedSize(w - 35, h);
m_searchBtn->setFixedSize(35, h);
QWidget::setFixedSize(w, h);
}
void ElaSearchEdit::setFixedHeight(int h)
{
m_lineEdit->setFixedHeight(h);
m_searchBtn->setFixedSize(35, h);
QWidget::setFixedHeight(h);
}
bool ElaSearchEdit::isExactMatch() const
{
return m_exactMatch;
}
void ElaSearchEdit::setExactMatch(bool exact)
{
m_exactMatch = exact;
m_exactMatchBtn->setChecked(exact);
}
+46
View File
@@ -0,0 +1,46 @@
#ifndef ELASEARCHEDIT_H
#define ELASEARCHEDIT_H
#include <QWidget>
#include <QHBoxLayout>
#include <QList>
#include "ElaLineEdit.h"
#include "ElaToolButton.h"
class ElaSearchEdit : public QWidget
{
Q_OBJECT
public:
explicit ElaSearchEdit(QWidget* parent = nullptr);
~ElaSearchEdit() override;
QString text() const;
void setText(const QString& text);
void setPlaceholderText(const QString& placeholderText);
void setFixedSize(const QSize& size);
void setFixedSize(int w, int h);
void setFixedHeight(int h);
bool isExactMatch() const;
void setExactMatch(bool exact);
signals:
void textChanged(const QString& text);
void returnPressed();
void exactMatchChanged(bool exact);
void searchClicked();
private:
void initUI();
void connectSignals();
void resetInputMargin();
ElaToolButton* addToolButton(QWidget* parent, ElaIconType::IconName icon, const QString& tooltip, const QSize& size, bool checkable = false, bool checked = false);
ElaLineEdit* m_lineEdit;
ElaToolButton* m_searchBtn;
ElaToolButton* m_exactMatchBtn;
bool m_exactMatch;
};
#endif // ELASEARCHEDIT_H