介绍
在一些工业开发的软件中,需要对一些硬件或者算法进行参数的设置,但是QT官方给出的QtTreeProperty控件,在一些程度上,不足以满足我们的需求,比如说,单独的按钮,以及一些选择文件夹和文件路径等。在次基础上,利用QT5.14.2的QTableView控件来制作一款参数设置控件。界面效果如下:
说明:
参数1 代表 int型整数
参数2 代表 double型参数
参数3 代表 QString型参数
参数4 代表 true or false参数
参数5 代表 下拉框型参数
参数6 代表 枚举型参数
参数7 代表 QPoint型参数
参数8 代表 矩形参数(row1, col1, row2, col2)
参数9 代表 旋转矩形参数(row,col,phi,length1,length2)
参数10 代表 QSize型参数
参数11 代表 QPushButton型
参数12 代表 QColor型参数
参数13 代表 QFont型参数
参数14 代表 选择文件路径
参数15 代表 选择文件及路径
使用方法
1.在你自己的工程中定义一个参数的结构体
struct STBaseParam //算法输入的基本参数
{
STBaseParam()
{
nParamClassify = 1;
sParamName = "";
sParamLabel = "";
sParamTip = "";
nParamType = 0;
sParamDefault = "";
sParamValueRange = "";
sParamValue = "";
sParamRemark = "";
}
~STBaseParam()
{
nParamClassify = 1;
sParamName = "";
sParamLabel = "";
sParamTip = "";
nParamType = 0;
sParamDefault = "";
sParamValueRange = "";
sParamValue = "";
sParamRemark = "";
}
STBaseParam& operator=(STBaseParam& pInfo)
{
this->nParamClassify = pInfo.nParamClassify;
this->sParamName = pInfo.sParamName;
this->sParamLabel = pInfo.sParamLabel;
this->sParamTip = pInfo.sParamTip;
this->nParamType = pInfo.nParamType;
this->sParamDefault = pInfo.sParamDefault;
this->sParamValueRange = pInfo.sParamValueRange;
this->sParamValue = pInfo.sParamValue;
this->sParamRemark = pInfo.sParamRemark;
return (*this);
}
friend QDataStream& operator << (QDataStream& output, const STBaseParam& Obj)
{
output << Obj.nParamClassify;
if (Obj.sParamName == "") output << QString("NULL");
else output << Obj.sParamName;
if (Obj.sParamLabel == "") output << QString("NULL");
else output << Obj.sParamLabel;
if (Obj.sParamTip == "") output << QString("NULL");
else output << Obj.sParamTip;
output << Obj.nParamType;
if (Obj.sParamDefault == "") output << QString("NULL");
else output << Obj.sParamDefault;
if (Obj.sParamValueRange == "") output << QString("NULL");
else output << Obj.sParamValueRange;
if (Obj.sParamValue == "") output << QString("NULL");
else output << Obj.sParamValue;
if (Obj.sParamRemark == "") output << QString("NULL");
else output << Obj.sParamRemark;
return output;
}
friend QDataStream& operator >> (QDataStream& input, STBaseParam& Obj)
{
input >> Obj.nParamClassify;
input >> Obj.sParamName;
if (Obj.sParamName == "NULL") Obj.sParamName = "";
input >> Obj.sParamLabel;
if (Obj.sParamLabel == "NULL") Obj.sParamLabel = "";
input >> Obj.sParamTip;
if (Obj.sParamTip == "NULL") Obj.sParamTip = "";
input >> Obj.nParamType;
input >> Obj.sParamDefault;
if (Obj.sParamDefault == "NULL") Obj.sParamDefault = "";
input >> Obj.sParamValueRange;
if (Obj.sParamValueRange == "NULL") Obj.sParamValueRange = "";
input >> Obj.sParamValue;
if (Obj.sParamValue == "NULL") Obj.sParamValue = "";
input >> Obj.sParamRemark;
if (Obj.sParamRemark == "NULL") Obj.sParamRemark = "";
return input;
}
int nParamClassify; //参数类别(1=过程参数;2=结果参数;3=调试参数)
QString sParamName; //参数名字
QString sParamLabel; //参数在界面显示的中文名字
QString sParamTip; //参数在界面的提示的说明文本
//参数类型(0=int;1=double;2=QString;3=bool;4=combobox;
//5 = flag;6 = point(x, y);7 = 矩形(row1, col1, row2, col2);
//8 = 旋转矩形(row, col, phi, lenth1, length2);9 = size(width, height);
//10 = 按钮;11 = 颜色选择对话框color(r, g, b, a); 12 = 字体选择对话框font 13 = 选择文件对话框 14 = 选择文件夹对话框)
int nParamType;
QString sParamDefault; //参数默认值
QString sParamValueRange; //参数的取值范围(0%%255%%1:最小值为0;最大值为255;每次增加1)
QString sParamValue; //参数值
QString sParamRemark; //参数的备注
};
2.在你自己工程的界面中拖入一个QTableView控件,将其提升为DBTableParamView.h
3.Test_TableView.h文章来源:https://www.toymoban.com/news/detail-443747.html
#pragma once
#include <QtWidgets/QWidget>
#include "ui_Test_TableView.h"
#include "DBHeader.h"
#pragma execution_character_set("utf-8")
class Test_TableView : public QWidget
{
Q_OBJECT
public:
Test_TableView(QWidget *parent = Q_NULLPTR);
~Test_TableView();
protected:
void closeEvent(QCloseEvent* event);
private:
void InitWidget();
void InitConnection();
private:
void ParamValueChanged(QString sParamName, QString sParamValue);
private:
QVector<STBaseParam*> Vect_BaseParam;
private:
Ui::Test_TableViewClass ui;
};
Test_TableView.cpp文章来源地址https://www.toymoban.com/news/detail-443747.html
#include "Test_TableView.h"
#include <QDebug>
#include <QMessageBox>
Test_TableView::Test_TableView(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
InitWidget();
InitConnection();
}
Test_TableView::~Test_TableView()
{
}
void Test_TableView::closeEvent(QCloseEvent* event)
{
//exit(0);
}
void Test_TableView::InitWidget()
{
//int型SpinBox控件
STBaseParam* pBase1 = new STBaseParam;
pBase1->nParamClassify = 1;
pBase1->nParamType = 0;
pBase1->sParamName = "Param1";
pBase1->sParamLabel = "参数1";
pBase1->sParamTip = "这是参数1";
pBase1->sParamDefault = "12";
pBase1->sParamValueRange = "1%%100%1";
pBase1->sParamValue = "15";
pBase1->sParamRemark = "最小值是1,最大值是100,默认值是12";
Vect_BaseParam.push_back(pBase1);
//double型SpinBox控件
STBaseParam* pBase2 = new STBaseParam;
pBase2->nParamClassify = 1;
pBase2->nParamType = 1;
pBase2->sParamName = "Param2";
pBase2->sParamLabel = "参数2";
pBase2->sParamTip = "这是参数2";
pBase2->sParamDefault = "2.5";
pBase2->sParamValueRange = "1%%100%%0.5";
pBase2->sParamValue = "15.6";
pBase2->sParamRemark = "最小值是1,最大值是100,默认值是2.5";
Vect_BaseParam.push_back(pBase2);
//TextEdit型控件
STBaseParam* pBase3 = new STBaseParam;
pBase3->nParamClassify = 1;
pBase3->nParamType = 2;
pBase3->sParamName = "Param3";
pBase3->sParamLabel = "参数3";
pBase3->sParamTip = "这是参数3";
pBase3->sParamDefault = "dongbin";
pBase3->sParamValueRange = "";
pBase3->sParamValue = "dongbin123";
pBase3->sParamRemark = "请输入名字,默认值是dongbin";
Vect_BaseParam.push_back(pBase3);
//CheckBox型控件
STBaseParam* pBase4 = new STBaseParam;
pBase4->nParamClassify = 1;
pBase4->nParamType = 3;
pBase4->sParamName = "Param4";
pBase4->sParamLabel = "参数4";
pBase4->sParamTip = "这是参数4";
pBase4->sParamDefault = "1";
pBase4->sParamValueRange = "是否选择信息";
pBase4->sParamValue = "0";
pBase4->sParamRemark = "=true 运行 =false 不运行 默认值:true";
Vect_BaseParam.push_back(pBase4);
//ComboBox型控件
STBaseParam* pBase5 = new STBaseParam;
pBase5->nParamClassify = 1;
pBase5->nParamType = 4;
pBase5->sParamName = "Param5";
pBase5->sParamLabel = "参数5";
pBase5->sParamTip = "这是参数5";
pBase5->sParamDefault = "Name";
pBase5->sParamValueRange = "Name,Age,City,Other";
pBase5->sParamValue = "City";
pBase5->sParamRemark = "选择相关信息,默认值是Name";
Vect_BaseParam.push_back(pBase5);
//多个CheckBox型控件
STBaseParam* pBase6 = new STBaseParam;
pBase6->nParamClassify = 1;
pBase6->nParamType = 5;
pBase6->sParamName = "Param6";
pBase6->sParamLabel = "参数6";
pBase6->sParamTip = "这是参数6";
pBase6->sParamDefault = "0,0,0,0";
pBase6->sParamValueRange = "Flag1,Flag2,Flag3,Flag4";
pBase6->sParamValue = "0,1,0,1";
pBase6->sParamRemark = "设置每个Falg的状态,默认值是Flag1=false,Flag2=false,Flag3=false,Flag4=false";
Vect_BaseParam.push_back(pBase6);
//点数据控件
STBaseParam* pBase7 = new STBaseParam;
pBase7->nParamClassify = 1;
pBase7->nParamType = 6;
pBase7->sParamName = "Param7";
pBase7->sParamLabel = "参数7";
pBase7->sParamTip = "这是参数7";
pBase7->sParamDefault = "10,20";
pBase7->sParamValueRange = "";
pBase7->sParamValue = "30,40";
pBase7->sParamRemark = "设置点的坐标(X,Y),默认值是(10,20)";
Vect_BaseParam.push_back(pBase7);
//矩形1数据控件
STBaseParam* pBase8 = new STBaseParam;
pBase8->nParamClassify = 1;
pBase8->nParamType = 7;
pBase8->sParamName = "Param8";
pBase8->sParamLabel = "参数8";
pBase8->sParamTip = "这是参数8";
pBase8->sParamDefault = "10,20,30,40";
pBase8->sParamValueRange = "";
pBase8->sParamValue = "50,60,70,80";
pBase8->sParamRemark = "设置矩形1的坐标(Row1,Col1,Row2,Col2),默认值是(10,20,30,40)";
Vect_BaseParam.push_back(pBase8);
//旋转矩形2数据控件
STBaseParam* pBase9 = new STBaseParam;
pBase9->nParamClassify = 1;
pBase9->nParamType = 8;
pBase9->sParamName = "Param9";
pBase9->sParamLabel = "参数9";
pBase9->sParamTip = "这是参数9";
pBase9->sParamDefault = "10,20,0,30,40";
pBase9->sParamValueRange = "";
pBase9->sParamValue = "50,60,1.32007,70,80";
pBase9->sParamRemark = "设置矩形1的坐标(Row,Col,Phi,Length1,Length2),默认值是(10,20,0,30,40)";
Vect_BaseParam.push_back(pBase9);
//尺寸数据控件
STBaseParam* pBase10 = new STBaseParam;
pBase10->nParamClassify = 1;
pBase10->nParamType = 9;
pBase10->sParamName = "Param10";
pBase10->sParamLabel = "参数10";
pBase10->sParamTip = "这是参数10";
pBase10->sParamDefault = "10,20";
pBase10->sParamValueRange = "";
pBase10->sParamValue = "30,40";
pBase10->sParamRemark = "设置尺寸的大小(Width,Height),默认值是(10,20)";
Vect_BaseParam.push_back(pBase10);
//按钮控件
STBaseParam* pBase11 = new STBaseParam;
pBase11->nParamClassify = 1;
pBase11->nParamType = 10;
pBase11->sParamName = "Param11";
pBase11->sParamLabel = "参数11";
pBase11->sParamTip = "这是参数11";
pBase11->sParamDefault = "";
pBase11->sParamValueRange = "";
pBase11->sParamValue = "调试程序";
pBase11->sParamRemark = "按下按钮,开始调试程序";
Vect_BaseParam.push_back(pBase11);
//选择颜色对话框控件
STBaseParam* pBase12 = new STBaseParam;
pBase12->nParamClassify = 1;
pBase12->nParamType = 11;
pBase12->sParamName = "Param12";
pBase12->sParamLabel = "参数12";
pBase12->sParamTip = "这是参数12";
pBase12->sParamDefault = "200,200,200,100"; //r,g,b,a
pBase12->sParamValueRange = "";
pBase12->sParamValue = "255,255,255,200";
pBase12->sParamRemark = "选择颜色对话框(R,G,B,A),默认值是rgba(200,200,200,100)";
Vect_BaseParam.push_back(pBase12);
//选择字体对话框控件
STBaseParam* pBase13 = new STBaseParam;
pBase13->nParamClassify = 1;
pBase13->nParamType = 12;
pBase13->sParamName = "Param13";
pBase13->sParamLabel = "参数13";
pBase13->sParamTip = "这是参数13";
pBase13->sParamDefault = "宋体,-1,-1,0";
pBase13->sParamValueRange = "";
pBase13->sParamValue = "宋体,12,12,0";
pBase13->sParamRemark = "选择字体对话框(family,pointSize,weight,italic),默认值是rgba(宋体,-1,-1,0)";
Vect_BaseParam.push_back(pBase13);
//选择文件路径对话框控件
STBaseParam* pBase14 = new STBaseParam;
pBase14->nParamClassify = 1;
pBase14->nParamType = 13;
pBase14->sParamName = "Param14";
pBase14->sParamLabel = "参数14";
pBase14->sParamTip = "这是参数14";
pBase14->sParamDefault = "D:\\test.txt";
pBase14->sParamValueRange = "";
pBase14->sParamValue = "";
pBase14->sParamRemark = "选择文件路径对话框(文件路径),默认值是D:\\test.txt";
Vect_BaseParam.push_back(pBase14);
//选择文件夹路径对话框控件
STBaseParam* pBase15 = new STBaseParam;
pBase15->nParamClassify = 1;
pBase15->nParamType = 14;
pBase15->sParamName = "Param15";
pBase15->sParamLabel = "参数15";
pBase15->sParamTip = "这是参数15";
pBase15->sParamDefault = "D:\\test";
pBase15->sParamValueRange = "";
pBase15->sParamValue = "";
pBase15->sParamRemark = "选择文件夹路径对话框(文件夹路径),默认值是D:\\test";
Vect_BaseParam.push_back(pBase15);
ui.tableView->InitTableParamView(Vect_BaseParam);
}
void Test_TableView::InitConnection()
{
ui.tableView->RegisterCallbackParamValueChanged(std::bind(&Test_TableView::ParamValueChanged, this, std::placeholders::_1, std::placeholders::_2));
}
void Test_TableView::ParamValueChanged(QString sParamName, QString sParamValue)
{
qDebug() << "ParamName=" << sParamName << " ParamValue=" << sParamValue;
if (sParamName == QString("Param11"))
{
QMessageBox::information(this, QString("提示"), QString("按钮按下!!!!!"));
}
}
到了这里,关于基于QT的参数设置界面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!