QT QLineEdit 添加 label,清除按钮

需求 qt QLineEdit 很多时候需要在前面加上一个标签,后面加上一个清除按钮。 解决 清除按钮 高版本 qt 自身就支持清除按钮,可以在 designer 中,勾选 clearButtonEnabled 属性. 添加 label 这个自有自定义一个 LineEdit 才行了,可以继承 QLineEdit, 然后设置 LineEdit 的布局为横向布局,然后把 QLabel 添加进来即可。 在 designer 中使用的时候,可以先放一个 QLineEdit, 然后提升为自定义的 LineEdit 即可。 参考 三种方法为QLineEdit添加清除内容按钮 C++ Qt自定义控件QLineEdit,简单 Qt之自定义用户名输入框 QLineEdit+QLabel+QComboBox 带输入自动提示补全和历史登录用户记忆 Qt之自定义搜索框——QLineEdit里增加一个Layout,还不影响正常输入文字(好像是一种比较通吃的方法) 【Qt】常用控件(QLabel,QLineEdit以及自定义控件)

2023-07-05 · 1 min · 40 words · RamLife

QT QFrame 占位添加 widget

需求 qt QFrame 有什么作用? 解决 占位 可以由 UI 工程师先使用 QFrame 占位,固定好整体布局,后续的实际的 widget 添加到 QFrame 中即可。 添加 widget QFrame 占位之后,还是需要使用 layout 来管理需要添加进去的 widget, 如果不用 layout, 可能会发生 widget 都在 (0, 0) 处,并且 resize 窗口之后,可能不会自动调整内部 widget. chartView = new ChartBriefView(createScatterChart()); auto layout = new QVBoxLayout(); layout->addWidget(chartView); ui->frame->setLayout(layout); 参考 QtDesigner+QWidget占位设计 QT的布局关键之一QFrame Adding a widget to a QFrame 第二十九章、containers容器类部件QFrame框架部件详解

2023-07-05 · 1 min · 54 words · RamLife

QT QGroupbox 边框设置

需求 qt QGroupBox 自带的边框颜色非常淡,和 QFrame 的 frameShape 中的 Box 颜色相差太大了。希望修改边框,能够整体契合一些。 解决 QGroupBox 没有 frameShape, 只能通过 styleSheet 的配置来达到效果。在 designer 中如下设置 stypeSheet 即可和 QFrame 合拍。 QGroupBox{ border-width:1px; border-style:solid; border-color:gray; margin-top:0.5ex; } 如果还想要圆角矩形之类的,那么需要这样设置: QGroupBox{ border-width:2px; border-style:solid; border-radius: 10px; border-color:gray; margin-top:0.5ex; } 当然也可以在代码中使用如下的格式来进行配置: ui->groupBox->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E0E0E0, stop: 1 #FFFFFF);" " border-radius: 5px; margin-top: 1ex; ");//border: 2px solid gray; 参考 QGroupBox 显示边框 圆角边框 linux环境 QGroupBox设置边框

2023-07-05 · 1 min · 69 words · RamLife

QT QFrame 边框设置

需求 qt QFrame 需要能够边框明显一些,方便各个区域能够对比明显。 解决 在 designer 中,设置 QFrame 的 frameShape 从默认的 StylePanel 改为 Box, 就可以非常明显的看出来边框了,如果还需要进一步设置阴影,那么可以设置 frameShadow 为 Sunken, lineWidth 为 5, midLineWidth 为 10. 参考 《Qt Creator 快速入门 第二版》 P63

2023-07-05 · 1 min · 31 words · RamLife

QT QChartView 点击跳转详细大图

需求 qt 需要在 QChartView 被点击时,跳转详情页面。 解决 其实也简单,只需要 override mousePressEvent 即可。 新建一个类 public 继承 QChartView 在这个类里面重写 mousePressEvent, 每当按下的时候发出 Press 的信号 在前一个页面的代码中绑定 Press 这个信号和详情的页面。 这样就可以在点击图表的时候,发出信号,触发详情页面的打开。 #ifndef CHARTBRIEFVIEW_H #define CHARTBRIEFVIEW_H #include <QWidget> #include <QtCharts/QChartGlobal> #include <QtCharts/QChartView> QT_CHARTS_BEGIN_NAMESPACE class QChartView; class QChart; QT_CHARTS_END_NAMESPACE QT_CHARTS_USE_NAMESPACE class ChartBriefView : public QChartView { Q_OBJECT public: ChartBriefView(QChart *chart, QWidget *parent = nullptr); signals: void Press(int id); private: void mousePressEvent(QMouseEvent *event) override; }; #endif // CHARTBRIEFVIEW_H #include "chart_brief_view.h" #include <QDebug> ChartBriefView::ChartBriefView(QChart *chart, QWidget *parent) : QChartView(chart, parent) { } void ChartBriefView::mousePressEvent(QMouseEvent *event) { qDebug() << "chart view press" << Qt::endl; emit Press(0); } connect(chartView, &ChartBriefView::Press, this, &ControllerForm::ClickSensor); 参考 QtCharts图形移动和缩放...

2023-07-05 · 1 min · 104 words · RamLife

QT connect signal 和 slot 类型匹配

需求 qt 在使用 connect 连接时,有些时候因为类型匹配的问题,会报错: QPushButton * controller_button_[APP_NUM_OF_CONTROLLER]; QSignalMapper * signalMapper_; signalMapper_ = new QSignalMapper(this); for (int i = 0; i < APP_NUM_OF_CONTROLLER; i++) { controller_button_[i] = new QPushButton(this); connect(controller_button_[i], &QPushButton::clicked, signalMapper_, &QSignalMapper::map); signalMapper_->setMapping(controller_button_[i], i); } connect(signalMapper_, &QSignalMapper::mapped, this, &MainWindow::ClickDetail); mainwindow.cpp:173:9: error: no matching member function for call to 'connect' qobject.h:222:36: note: candidate function not viable: no known conversion from 'void (QAbstractButton::*)(bool) __attribute__((thiscall))' to 'const char *' for 2nd argument qobject....

2023-07-05 · 2 min · 278 words · RamLife

QT 进度条 介绍

需求 最近想了解下 进度条。 解决 QT–进度条 参考

2023-07-05 · 1 min · 6 words · RamLife

c++ json 库 nlohmann json 转换异常 [json.exception.type_error.305]

需求 nlohmman 进行 json 转换时,报异常: [json.exception.type_error.305] cannot use operator[] with a string argument with array. 解决 转换为 json 异常 原因可能是,嵌套时,括号不匹配 void to_json(json& j, const VoltageConfigJson& config) { j = json{{"config_name", config.name}}; for (auto & cfg : config.data) { json channel; to_json(channel, cfg); j["config_data"].push_back(channel); } } 上面这个代码中,如果第一句写成下面这样,那么就会是 305 的异常。 j = json{"config_name", config.name}; 参考 {json.exception.type_error.305} cannot use operator{} with a string argument with array Cannot use operator{} with a string argument with string...

2023-06-26 · 1 min · 92 words · RamLife

c++ json 库 nlohmann 中类和 json 转换

需求 尝试使用 nlohmman 把复杂的类转为 json。 解决 复杂的类需要对其中的某些部分单独抽出来进行转换。 用于转换为 json 的类 struct VoltageSetConfigData { float w1; float p1; float w2; float p2; float ref; float pf; }; using VoltageGetConfigData = VoltageSetConfigData; class VoltageConfigJson { public: std::string name; std::vector<VoltageGetConfigData> data; }; 数组单个元素转换 void to_json(json& j, const VoltageGetConfigData& data) { j = json{{"w1", data.w1}, {"p1", data.p1}, {"w2", data.w2}, {"p2", data.p2}, {"ref", data.ref}, {"pf", data.pf}}; } void from_json(const json& j, VoltageGetConfigData& data) { j.at("w1").get_to(data.w1); j....

2023-06-26 · 1 min · 135 words · RamLife

c++ json 库 nlohmann 文件读写

需求 nlohmman 读写 json 配置文件. 解决 写文件 QString fileName = QFileDialog::getSaveFileName(this, tr("save config file"), "./voltage_", tr("Json (*.json)")); qDebug() << fileName << Qt::endl; if (fileName.isEmpty()) return; ofstream file(fileName.toStdString(), ios::out); json j; to_json(j, config_json); file << j << std::endl; 读文件 QString file_name = QFileDialog::getOpenFileName(this, tr("load config file"), "./voltage_", tr("Json (*.json)")); qDebug() << file_name << Qt::endl; if (file_name.isEmpty()) return; ifstream file(file_name.toStdString(), ios::in); json j = json::parse(file); 参考 nlohmann/json 的主要用法 【C++ JSON 开源库】nlohmann入门使用总结 nlohmann/json...

2023-06-26 · 1 min · 72 words · RamLife