Files
Renderive/Widget/component/simple_widget.cpp
T
2026-07-29 12:30:40 +08:00

128 lines
6.2 KiB
C++

#pragma once
#include "global.h"
// https://blog.csdn.net/MrHHHHHH/article/details/134182253
class Input : public QLineEdit {
public:
explicit Input(QWidget* parent = nullptr) : QLineEdit(parent) {
setStyle(new Style());
setFrame(true);
// 当光标的位置在文本编辑器中改变时,这个信号会被触发。old和new参数分别表示光标改变前后的位置。
connect(this, &QLineEdit::cursorPositionChanged, this, [=](int n_old, int n_new) {
// qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "n_old :" << n_old << "n_new :" << n_new;
});
// 当用户完成文本编辑(例如按下Enter键或点击其他区域使编辑结束)时,这个信号会被触发。
connect(this, &QLineEdit::editingFinished, this, [=]() {
// qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "text :" << this->text();
});
// 当用户在文本编辑器中按下Enter键时,这个信号会被触发。
connect(this, &QLineEdit::returnPressed, this, [=]() {
// qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "text :" << this->text();
});
// 当文本编辑器中的选区发生变化时,这个信号会被触发。例如,当用户选择或取消选择文本时。
connect(this, &QLineEdit::selectionChanged, this, [=]() {
// qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "text :" << this->text();
});
// 当文本编辑器中的文本发生更改时,这个信号会被触发。参数text是改变后的文本内容。
connect(this, &QLineEdit::textChanged, this, [=](const QString& text) {
// qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "text :" << text;
});
// 当文本编辑器开始编辑新的文本时,这个信号会被触发。参数text是当前编辑的文本内容。
connect(this, &QLineEdit::textEdited, this, [=](const QString& text) {
// qDebug().noquote() << "[" << __FILE__ << __LINE__ << "]" << "text :" << text;
});
// update();
}
QString unit = "MHZ";
QFont font;
QPen pen;
QColor focus_color = Qt::red;
QColor default_color = Qt::black;
protected:
void paintEvent(QPaintEvent* event) override {
QLineEdit::paintEvent(event); // 调用原始绘制
auto fm = QFontMetrics(font);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
int w = width();
int h = height();
int pen_w = 1;
if (QApplication::focusWidget() == this) {
painter.setPen(QPen(focus_color, pen_w));
}
else {
painter.setPen(QPen(default_color, pen_w));
}
painter.drawRoundedRect(rect(), h / 4, h / 4);
painter.setPen(pen);
painter.setFont(font);
int unit_width = fm.horizontalAdvance(unit);
int th = fm.height();
int x = rect().right() - unit_width - 5; // 计算单位的绘制位置
painter.drawText(x, height() / 2 + th / 2, unit);
setTextMargins(0, 0, unit_width + 4, 0); // 右边留出单位的空间
}
class Style : public QProxyStyle {
public:
void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption* option, QPainter* painter,
const QWidget* widget) const override {
// 先调用默认的绘制
auto t = qobject_cast<const QLineEdit*>(widget);
if (!t)
return;
static bool it = true;
// QProxyStyle::drawPrimitive(element, option, painter, widget);
// qDebug() << "t->hasFrame() == " << t->hasFrame();
if (it) {
// QProxyStyle::drawPrimitive(element, option, painter, widget);
it = false;
}
// 行编辑器边框
if (element == PE_PanelLineEdit) {
//
}
// 行编辑器边框
if (element == PE_FrameLineEdit) {
// qDebug() << "line_edit drawPrimitive " << element;
// qDebug() << "width " << option->rect.width();
}
}
void drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter,
const QWidget* widget) const override {
// auto t = qobject_cast<const QLineEdit*>(widget);
// if (!t) return;
// qDebug() << "line_edit drawComplexControl " << control;
// QProxyStyle::drawComplexControl(control, option, painter, widget);
}
void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter,
const QWidget* widget) const override {
// auto t = qobject_cast<const QLineEdit*>(widget);
// qDebug() << "line_edit drawControl " << element;
// QProxyStyle::drawControl(element, option, painter, widget);
// if (!t) return;
}
// void drawItemText(QPainter* painter, const QRect& rect, int flags, const QPalette& pal, bool enabled,
// const QString& text, QPalette::ColorRole text_role) const override {
//
//
// }
// void drawItemPixmap(QPainter* painter, const QRect& rect, int alignment, const QPixmap& pixmap) const
// override {
//
// }
QRect subElementRect(SubElement element, const QStyleOption* option, const QWidget* widget) const override {
auto t = qobject_cast<const QLineEdit*>(widget);
if (!t) {
return QProxyStyle::subElementRect(element, option, widget);
}
// qDebug() << "line_edit subElementRect " << element;
// 这里可以自定义返回不同的 QRect 以控制子元素的大小或位置
if (element == SE_LineEditContents) {
QRect rect = QProxyStyle::subElementRect(element, option, widget);
// rect.adjust(5, 5, -5, -5); // 例如,让文本内容区域缩小一点
return rect;
}
return QProxyStyle::subElementRect(element, option, widget);
}
};
};