QtExa001自动包装流水线的框架设计
工程代码:
https://download.csdn.net/download/txwtech/89636815https://download.csdn.net/download/txwtech/89636815
主界面:
设置:进行参数配置,保存ini文件
调试:tcp/ip,串口,modbus tcp/RTU,http进行调试
查询:通过连接mysql数据库实现增删改查操作
登录界面:tt,密码1登录后,设置与调试显示可用
多线程的操作:点击开始后,启动任务,对主界面相关控件进行信息更新
底部实时显示当前时间,配置的IP与端口信息
实现整体流水线任务时,根据项目业务逻辑,通过调试通信的代码一起放到线程中运行
鼠标移动到按钮工具提示,信息提示
程序启动,初始化部分
void auto_pack_line_demo::InitInterface()
{
AddToMainLog("程序启动");
AddToMainLog("developed by cdtxw@foxmail");
ui.pushButton_setting->setEnabled(false);
ui.pushButton_debug->setEnabled(false);
std::shared_ptr<CFile_Operation> ptr1 = std::make_shared<CFile_Operation>();//make_shared函数创建
std::shared_ptr<CFile_Operation> ptr2 = std::shared_ptr<CFile_Operation>(); //构造函数创建
ptr1->AddToLoginfo("smart ptr test");
ptr2->AddToLoginfo("smart ptr test2");
serial_modbus_ptr = std::make_shared<SerialModbus>();
cfile_operation = new CFile_Operation();
main_thread = new MainTaskThread();
c_op->ReadAllMainConfig();
tcp_Client = new tcp_client();
http_req = new http_request();
tcp_client_modbus = new TcpClientModbus();
tcp_Client->Connect(c_op->vision_ip, c_op->vision_port);
tcp_client_modbus->Connect(c_op->plc1_ip,c_op->plc1_port);
serial_modbus_ptr->SerialConnect(c_op->serial_port_read_weight,c_op->baudrate_read_weight.toInt());
//SerialModbus* bb = serial_modbus_ptr.get();
//bb->SerialConnect("aa",11);
QString tcp_msg=tcp_client_modbus->GetConnectInfo();
QTimer* timer2 = new QTimer(this);
connect(timer2, &QTimer::timeout, this, &auto_pack_line_demo::TimerUpdate);
timer2->start(1000);
ui.lineEdit_PLC_IP->setText(c_op->plc1_ip);
ui.lineEdit_PLC_port->setText(c_op->plc1_port);
ui.lineEdit_vision_IP->setText(c_op->vision_ip);
ui.lineEdit_vision_port->setText(c_op->vision_port);
QToolTip::setFont(font3); //按钮提示信息通用设置
// QPalette toolTipPalette= QToolTip::palette();
// toolTipPalette.setColor(QPalette::Text, Qt::white); // 设置文本颜色为红色
// toolTipPalette.setColor(QPalette::WindowText, Qt::blue); // 设置窗口文本颜色为蓝色
// toolTipPalette.setColor(QPalette::Background, Qt::yellow); // 设置背景颜色为黄色
QPalette pal = QToolTip::palette();
// 修改背景色,工具提示,按钮信息提示
pal.setColor(
QPalette::Inactive,
QPalette::ToolTipBase,
// QColor("darkblue")
QColor("blue")
);
// 修改文本颜色
pal.setColor(
QPalette::Inactive,
QPalette::ToolTipText,
//QColor("lightyellow")
QColor("black")
);
QToolTip::setPalette(pal);
setWindowIcon(QIcon("box_1a.png")); // 使用文件系统路径
table_view_msg = ui.tableView_lot_information;
//table_view_box_label = ui->tableView_box_label_info;
//QPixmap pixmap(":/auto_pack_line_demo/pics/box_1a.png");//代码设置图片路径
// ui.label_main_box->setPixmap(pixmap); //设置图片
ui.label_main_box->setScaledContents(true); //适应控件大小
QString msg = "";
// ui->plainTextEdit_log_msg->appendPlainText(msg);
btn_start = ui.pushButton_start;
btn_pause = ui.pushButton_pause;
btn_inspection = ui.pushButton_inspection;
btn_exit = ui.pushButton_system_exit;
btn_prod_precheck = ui.pushButton_inspection;
btn_logoff = ui.pushButton_user_logoff;
// lineedit_yinzi = ui->lineEdit_yinzi_check;
btn_setting = ui.pushButton_setting;
btn_debug = ui.pushButton_debug;
// btn_camera_debug = ui->pushButton_camera_debug;
btn_database = ui.pushButton_query;
btn_log_in = ui.pushButton_user;
//btn_log_info = ui->pushButton_log_info;
connect(this, &auto_pack_line_demo::Thread3Finished, this, [=](int value) {
ui.lineEdit_vision_info->setText(QString::number(value));
});
connect(ui.pushButton_system_exit, (SIGNAL(clicked())), this, SLOT(CloseWindow()));
connect(ui.pushButton_start, (SIGNAL(clicked())), this, SLOT(StartTask()));
connect(ui.lineEdit_equ_comm, &QLineEdit::textChanged, this, [=] {
if (ui.lineEdit_equ_comm->text() == "OK")
{
QString green = QString("background-color: rgb(85, 255, 0);");
ui.lineEdit_equ_comm->setStyleSheet(green);
}
else
{
QString red = QString("background-color: rgb(255, 0, 0);");
QString yellow = QString("background-color: rgb(255, 255, 0);");
ui.lineEdit_equ_comm->setStyleSheet(yellow);
}
});
connect(btn_setting, (SIGNAL(clicked())), this, SLOT(OpenSettingWindow()));
connect(btn_debug, (SIGNAL(clicked())), this, SLOT(OpenDebugWindow()));//宏定义方式,QT4.0
connect(ui.pushButton_query, &QPushButton::clicked, this, [=] {
Dialog_DataBase2* dialog_database = new Dialog_DataBase2(this);
dialog_database->setModal(true);
dialog_database->show();
});
connect(table_view_msg, &QTableView::clicked, this, &auto_pack_line_demo::GetTableViewLotSelectedText); //QT5
connect(lineedit_yinzi, &QLineEdit::textChanged, this, [=](QString input_msg) {
if (input_msg.length() > 3)
{
AddToMainLog("已输入印字信息:" + lineedit_yinzi->text());
}
});
connect(ui.pushButton_user, &QPushButton::clicked, this, [=] {
dialog_login = new Dialog_LogIn(this);
dialog_login->setModal(true);
dialog_login->show();
connect(dialog_login, &Dialog_LogIn::LoginRet, this, [=](bool value) {
if (value == true)
{
ui.pushButton_setting->setEnabled(true);
ui.pushButton_debug->setEnabled(true);
btn_setting->setStyleSheet(styleSheet_setting);
btn_debug->setStyleSheet(styleSheet_debug);
AddToMainLog("登录成功");
}
else
{
btn_setting->setStyleSheet(sytleSheet_gray_font);
btn_debug->setStyleSheet(sytleSheet_gray_font);
ui.pushButton_setting->setEnabled(false);
ui.pushButton_debug->setEnabled(false);
AddToMainLog("登录失败");
}
});
});
connect(ui.pushButton_pause, &QPushButton::clicked, this, [=] {
AddToMainLog("点击了暂停"); //cdtxw
});
connect(ui.pushButton_inspection, &QPushButton::clicked, this, [=] {
AddToMainLog("点击了检测"); //cdtxw
});
connect(ui.pushButton_user_logoff, &QPushButton::clicked, this, [=] {
AddToMainLog("点击了注销"); //cdtxw
});
// connect(btn_camera_debug, &QPushButton::clicked, this, [=] {
// Dialog_camera_debug* camera_debug = new Dialog_camera_debug();
// camera_debug->setModal(true);
// camera_debug->showNormal();
// });
// connect(ui->pushButton_exit,(SIGNAL(clicked())),this,SLOT(close()));
btn_start->setToolTip(("启动程序")); //按钮提示信息
btn_pause->setToolTip(("系统暂停")); //按钮提示信息
btn_exit->setToolTip(("退出程序")); //按钮提示信息
btn_logoff->setToolTip(("注销用户")); //按钮提示信息
btn_prod_precheck->setToolTip(("产品检测")); //按钮提示信息
btn_setting->setToolTip(("参数配置")); //按钮提示信息
btn_debug->setToolTip(("输入输出调试")); //按钮提示信息
//btn_camera_debug->setToolTip(("相机调试")); //按钮提示信息
btn_database->setToolTip(("数据库查询")); //按钮提示信息
btn_log_in->setToolTip(("权限登录")); //按钮提示信息
//btn_log_info->setToolTip(("日志查询")); //按钮提示信息
// start_value = ("开始");
start_value = "开始";
stop_value = "停止";
QString pic_path_start = tr(":/auto_pack_line_demo/pics/button_start_icon.png");
QString pic_path_pause = tr(":/auto_pack_line_demo/pics/button_pause_icon.png");
QString pic_path_user_change = tr(":/auto_pack_line_demo/pics/button_bg.png");
QString pic_path_setting_icon = tr(":/auto_pack_line_demo/pics/setting_icon2.png");
QString pic_path_debug_icon = tr(":/auto_pack_line_demo/pics/button_debug_icon_副本.png");
QString pic_path_camera_icon = tr(":/auto_pack_line_demo/pics/摄像头图像采集_副本2.png");
QString pic_path_database_icon = tr(":/auto_pack_line_demo/pics/button_calibration_icon_副本.png");
QString pic_path_user_icon = tr(":/auto_pack_line_demo/pics/button_user_icon_副本.png");
QString pic_path_logInfo_icon = tr(":/auto_pack_line_demo/pics/button_Log_icon.png");
QString color_white = tr("rgb(255, 255, 255);");
QString color_black = tr("rgb(0, 0, 0);");
QString color_gray = tr("rgb(192,192,192);");
QString styleSheet_start = QString("QPushButton{border-image:url(%1);background-color:transparent;border:none;font: 28pt \"Microsoft YaHei UI\";color:%2}"
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%3}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 28pt \"Microsoft YaHei UI\";}"
).arg(pic_path_start, color_white, color_black);
QString styleSheet_pause = QString("QPushButton{border-image:url(%1);background-color:transparent;border:none;font: 28pt \"Microsoft YaHei UI\";color:%2}"
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%3}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 28pt \"楷体\";}"
).arg(pic_path_pause, color_white, color_black);
QString styleSheet_exit = QString("QPushButton{border-image:url(%1);border:none;font: 28pt \"Microsoft YaHei UI\";color:%2}"
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%3}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 28pt \"楷体\";}"
).arg(pic_path_user_change, color_white, color_black);
styleSheet_debug = QString("QPushButton{border-image:url(%1);border:none;font: 28pt \"Microsoft YaHei UI\";color:%2}"
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%3}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 28pt \"Microsoft YaHei UI\";}"
"QPushButton{image:url(%4);image-position: left center;text-align:center}"
).arg(pic_path_user_change, color_white, color_black, pic_path_debug_icon);
QString styleSheet_camera = QString("QPushButton{border-image:url(%1);border:none;font: 28pt \"Microsoft YaHei UI\";color:%2}"
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%3}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 28pt \"Microsoft YaHei UI\";}"
"QPushButton{image:url(%4);image-position: left center;text-align:center}"
).arg(pic_path_user_change, color_white, color_black, pic_path_camera_icon);
QString styleSheet_database = QString("QPushButton{border-image:url(%1);border:none;font: 28pt \"Microsoft YaHei UI\";color:%2}"
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%3}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 28pt \"Microsoft YaHei UI\";}"
"QPushButton{image:url(%4);image-position: left center;text-align:center}"
).arg(pic_path_user_change, color_white, color_black, pic_path_database_icon);
QString styleSheet_user = QString("QPushButton{border-image:url(%1);border:none;font: 28pt \"Microsoft YaHei UI\";color:%2}"
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%3}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 28pt \"Microsoft YaHei UI\";}"
"QPushButton{image:url(%4);image-position: left center;text-align:center}"
).arg(pic_path_user_change, color_white, color_black, pic_path_user_icon);
QString styleSheet_loginfo = QString("QPushButton{border-image:url(%1);border:none;font: 28pt \"Microsoft YaHei UI\";color:%2}"
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%3}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 28pt \"Microsoft YaHei UI\";}"
"QPushButton{image:url(%4);image-position: left center;text-align:center}"
).arg(pic_path_user_change, color_white, color_black, pic_path_logInfo_icon);
//image: url(:/img/pics/auto_pack_main2.ico);
// image-position: left center;
btn_start->setStyleSheet(styleSheet_start);
btn_logoff->setStyleSheet(styleSheet_start);
btn_pause->setStyleSheet(styleSheet_pause);
btn_prod_precheck->setStyleSheet(styleSheet_pause);
btn_exit->setStyleSheet(styleSheet_exit);
sytleSheet_gray_font = QString(
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%1}"
).arg(color_gray);
styleSheet_setting = QString("QPushButton{border-image:url(%1);border:none;font: 28pt \"Microsoft YaHei UI\";color:%2}"
"QPushButton:hover{background-color:transparent;font: 28pt \"Microsoft YaHei UI\";color:%3}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 28pt \"Microsoft YaHei UI\";}"
"QPushButton{image:url(%4);image-position: left center;text-align:center}"
).arg(pic_path_user_change, color_white, color_black, pic_path_setting_icon);
btn_setting->setStyleSheet(sytleSheet_gray_font);
btn_debug->setStyleSheet(sytleSheet_gray_font);
//btn_camera_debug->setStyleSheet(styleSheet_camera);
btn_database->setStyleSheet(styleSheet_database);
btn_log_in->setStyleSheet(styleSheet_user);
//btn_log_info->setStyleSheet(styleSheet_loginfo);
QString space2 = " ";
btn_debug->setText(space2 + btn_debug->text());
//btn_camera_debug->setText(space2 + btn_camera_debug->text());
btn_database->setText(space2 + btn_database->text());
btn_log_in->setText(space2 + btn_log_in->text());
//btn_log_info->setText(space2 + btn_log_info->text());
QStringList list;
// list.append("aa");
// list.append("bb");
// list.append("cc");
list << "aa" << "bb" << "cc";
// QTableView aa;
//table_view_msg->setModel(&model2);
model = new QStandardItemModel(this);
QStandardItemModel* model_box_label = new QStandardItemModel(this);
//QStandardItem aa = new QStandardItem();
model->setColumnCount(8);
model->setHeaderData(0, Qt::Horizontal, ("序号"));
model->setHeaderData(1, Qt::Horizontal, ("批次"));
model->setHeaderData(2, Qt::Horizontal, ("类型"));
model->setHeaderData(3, Qt::Horizontal, ("刻字"));
model->setHeaderData(4, Qt::Horizontal, ("数量(总)"));
model->setHeaderData(5, Qt::Horizontal, ("数量(单盘)"));
model->setHeaderData(6, Qt::Horizontal, ("卷盘总数"));
model->setHeaderData(7, Qt::Horizontal, ("入站时间"));
model->setItem(0, 0, new QStandardItem(("1")));
model->setItem(0, 1, new QStandardItem(("ABC2345670")));
model->setItem(0, 2, new QStandardItem(("SS355")));
model->setItem(0, 3, new QStandardItem(("A1")));
model->setItem(0, 4, new QStandardItem(("150000")));
model->setItem(0, 5, new QStandardItem(("1500")));
model->setItem(0, 6, new QStandardItem(("10")));
model->setItem(0, 7, new QStandardItem(("2024/4/22 11:20:30")));
model->setItem(1, 0, new QStandardItem(("2")));
model->setItem(1, 1, new QStandardItem(("CBC2345670")));
model->setItem(1, 2, new QStandardItem(("SS358")));
model->setItem(1, 3, new QStandardItem(("A2")));
model->setItem(1, 4, new QStandardItem(("150000")));
model->setItem(1, 5, new QStandardItem(("1500")));
model->setItem(1, 6, new QStandardItem(("10")));
model->setItem(1, 7, new QStandardItem(("2024/4/22 11:20:30")));
QList<QStandardItem*> qq1;
for (int i = 0; i < 6; i++)
{
QString value = "aa" + QString::number(i + 1); //数字转字符
qq1.append(new QStandardItem(value));
}
QString aa = "111";
int aa_int = aa.toInt();//字符转数字
model->appendRow(qq1);
table_view_msg->setModel(model);
table_view_msg->verticalHeader()->setVisible(false);
model_box_label->setColumnCount(6);
model_box_label->setHeaderData(0, Qt::Horizontal, ("序号"));
model_box_label->setHeaderData(1, Qt::Horizontal, ("批次"));
model_box_label->setHeaderData(2, Qt::Horizontal, ("类型"));
model_box_label->setHeaderData(3, Qt::Horizontal, ("dateCode"));
model_box_label->setHeaderData(4, Qt::Horizontal, ("数量(盒)"));
model_box_label->setHeaderData(5, Qt::Horizontal, ("查询时间"));
model_box_label->setItem(0, 0, new QStandardItem(("1")));
model_box_label->setItem(0, 1, new QStandardItem(("LA12345670")));
model_box_label->setItem(0, 2, new QStandardItem(("SOT23")));
model_box_label->setItem(0, 3, new QStandardItem(("DTE2351")));
model_box_label->setItem(0, 4, new QStandardItem(("10")));
model_box_label->setItem(0, 5, new QStandardItem(("2024/4/22 11:20:30")));
model_box_label->setItem(1, 0, new QStandardItem(("2")));
model_box_label->setItem(1, 1, new QStandardItem(("LA12345671")));
model_box_label->setItem(1, 2, new QStandardItem(("SOT123")));
model_box_label->setItem(1, 3, new QStandardItem(("DTE2351")));
model_box_label->setItem(1, 4, new QStandardItem(("10")));
model_box_label->setItem(1, 5, new QStandardItem(("2024/4/22 11:20:30")));
//table_view_box_label->setModel(model_box_label);
//table_view_box_label->verticalHeader()->setVisible(false);
}
设置:参数配置
调试界面测试:TCP通信
模拟连接PLC进行modbus TCP通信,线圈与寄存器的读写操作
http的get应用
串口RS485 modbus RTU通信
普通串口通信:
多线程的操作:
通过两种方法创建了多个线程,同时运行,实时更新主界面控件
void auto_pack_line_demo::StartTask()
{
// QMessageBox::information(this,"提示","ab");
// QMessageBox::information(this,"提示1","baaa");
AddToTableView_lot_info();
if (btn_start->text() == start_value)
{
QMessageBox::StandardButton select_ret = QMessageBox::information(this, ("提示"), (" 确认启动设备吗? "), QMessageBox::Yes, QMessageBox::No);
if (select_ret == ret_yes)
{
b_main_thread = true;
btn_start->setText(stop_value);
AddToMainLog("点击开始");
connect(main_thread, &MainTaskThread::sig_update_main_loginfo, this, &auto_pack_line_demo::AddToMainLog);
connect(main_thread, &MainTaskThread::sig_update_main_equ_info, this, &auto_pack_line_demo::EquStateChanged);
connect(main_thread, &MainTaskThread::sig_update_main_ocr_info, this, &auto_pack_line_demo::OcrChanged);
connect(main_thread, &MainTaskThread::sig_update_main_label_process_info, this, &auto_pack_line_demo::ProcessStateChanged);
main_thread->start();
QFuture<void> future1 = QtConcurrent::run(this, &auto_pack_line_demo::UpdateVisionComm); //窗口按钮里面写法
//QFuture<void> future1 = QtConcurrent::run(UpdateVisionComm); //控制台的代码
// future1.waitForFinished(); //表示阻塞模式
QFuture<void> future2 = QtConcurrent::run(this, &auto_pack_line_demo::UpdateVisionComm2); //窗口按钮里面写法
//QFuture<void> future1 = QtConcurrent::run(UpdateVisionComm); //控制台的代码
// future1.waitForFinished();
//QMessageBox::information(this, (" 提示 "), btn_start->text() + (",选择了是"));
QFuture<int> future3 = QtConcurrent::run(this, &auto_pack_line_demo::UpdateVisionComm3);
//future3.waitForFinished();//表示阻塞模式
//int fu3 = future3.result();
// qDebug() << "fu3的结果:" << QString::number(fu3);
}
else
{
QMessageBox::information(this, (" 提示 "), btn_start->text() + (",选择了否"));
}
}
else if (btn_start->text() == stop_value)
{
btn_start->setText(start_value);
disconnect(main_thread,nullptr, nullptr, nullptr);
AddToMainLog("已点击停止");
main_thread->stop();
b_main_thread = false;
}
}
数据库的查询: