当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 1

Admin/GETTAOBAO
暂停

forked from EVA/GETTAOBAO
暂停
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mainwindow.cpp 13.91 KB
一键复制 编辑 原始数据 按行查看 历史
kiki 提交于 2019-06-01 09:12 . 修改替换(天猫)
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QJsonDocument>
#include <QJsonParseError>
#include <QFile>
#include <QJsonObject>
#include <QDebug>
#include <QJsonArray>
#include <fstream>
#include <iostream>
#include <QMessageBox>
#include <QThread>
#include <QThreadPool>
#include <QtConcurrent>
#include <QFileDialog>
#include <QFile>
pythonTest test;//python 代码
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),m_keyPath(""),m_id(0),myCmpWork(NULL),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tableResult table;
table.keyWord = "kkkkkkkkk";
table.result ="rrrrrrrrrrrrrr";
table.url ="uuuuuuuuuuuu";
table.title ="tttttt";
ui->lineEdit_num->setText("3");
//////////////////////////////////////////////
ui->pushButton_cmp->setEnabled(false);
ui->pushButton_cmpstop->setEnabled(false);
ui->pushButton_stop->setEnabled(false);
}
void MainWindow::readText2()
{
ifstream infile;
m_list.clear();
infile.open("list.txt"); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
string s;
QString temp;
while(getline(infile,s))
{
//cout<<s<<endl;
temp = QString::fromStdString(s);
}
infile.close(); //关闭文件输入流
QJsonParseError json_error;
//QJsonDocument jsonDoc(QJsonDocument::fromJson(s.c_str(), &json_error));
// temp = QString::fromStdString(s);
temp=temp.trimmed();
cout<<temp.toStdString().c_str()<<endl;
QJsonDocument jsonDoc(QJsonDocument::fromJson(temp.toStdString().c_str(), &json_error));
if(json_error.error == QJsonParseError::NoError)
{
if(jsonDoc.isArray())
{
QJsonArray rootary = jsonDoc.array();
for (int i = 0 ; i< rootary.size() ; i++)
{
tableResult table;
QString rootObj = rootary.at(i).toString();
table.url = rootObj;
m_list.append(table);
}
}
}
showTable2();
}
void MainWindow::readText()
{
m_listkey.clear();
ifstream infile;
infile.open("list.txt"); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
string s;
QString temp;
while(getline(infile,s))
{
//cout<<s<<endl;
temp = QString::fromStdString(s);
}
infile.close(); //关闭文件输入流
QJsonParseError json_error;
temp=temp.trimmed();
cout<<temp.toStdString().c_str()<<endl;
sqlite lite;
lite.cleakeyData();
QJsonDocument jsonDoc(QJsonDocument::fromJson(temp.toStdString().c_str(), &json_error));
if(json_error.error == QJsonParseError::NoError)
{
if(jsonDoc.isArray())
{
QJsonArray rootary = jsonDoc.array();
for (int i = 0 ; i< rootary.size() ; i++)
{
//QJsonObject rootObj = rootary.at(i).toObject();
QString rootObj = rootary.at(i).toString();
tableResult table;
table.url = rootObj;
m_listkey.append(table);
QString sql ="INSERT INTO key VALUES('"+QString::number(i)+"', '"+table.url+"')";
lite.insertData(sql);
ui->label_info->setText(QString::number(i));
}
}
}
else
qDebug() << json_error.error;
//showTable();
}
MainWindow::~MainWindow()
{
delete ui;
thread.quit();
thread.wait();
/* if(myCmpWork)
{
myCmpWork->quit();
myCmpWork->wait();
}*/
}
//打开匹配表
void MainWindow::showTable()
{
ui->tableWidget->clear();
for(int i =ui->tableWidget->rowCount()-1; i>=0; i--)
{
ui->tableWidget->removeRow(i);
}
ui->tableWidget->setColumnCount(4); //设置列数
ui->tableWidget->horizontalHeader()->setDefaultSectionSize(150);
QStringList header;
header<<tr("关键字")<<tr("")<<tr("")<<tr("");
ui->tableWidget->setHorizontalHeaderLabels(header);
int row_count = ui->tableWidget->rowCount(); //获取表单行数
for (int i = 0; i < m_listkey.size(); i++)
{
ui->tableWidget->insertRow(row_count); //插入新行
QTableWidgetItem *item = new QTableWidgetItem();
QTableWidgetItem *item1 = new QTableWidgetItem();
QTableWidgetItem *item2 = new QTableWidgetItem();
QTableWidgetItem *item3 = new QTableWidgetItem();
//设置对应的图标、文件名称、最后更新时间、对应的类型、文件大小
item->setText(m_listkey[i].keyWord);
item1->setText(m_listkey[i].title);
item2->setText(m_listkey[i].result); //type为调用系统的类型,以后缀来区分
item3->setText(m_listkey[i].url);
ui->tableWidget->setItem(row_count, 0, item);
ui->tableWidget->setItem(row_count, 1, item1);
ui->tableWidget->setItem(row_count, 2, item2);
ui->tableWidget->setItem(row_count, 3, item3);
}
}
//打开匹配表
void MainWindow::showTable2()
{
ui->tableWidget_2->clear();
deleteRoew2();
ui->tableWidget_2->setColumnCount(4); //设置列数
ui->tableWidget_2->horizontalHeader()->setDefaultSectionSize(150);
QStringList header;
header<<"链接"<<tr("标题")<<tr("获取状态")<<tr("匹配度");
ui->tableWidget_2->setHorizontalHeaderLabels(header);
int row_count = ui->tableWidget_2->rowCount(); //获取表单行数
for (int i = 0; i < m_list.size(); i++)
{
ui->tableWidget_2->insertRow(row_count); //插入新行
QTableWidgetItem *item = new QTableWidgetItem();
QTableWidgetItem *item1 = new QTableWidgetItem();
QTableWidgetItem *item2 = new QTableWidgetItem();
QTableWidgetItem *item3 = new QTableWidgetItem();
//设置对应的图标、文件名称、最后更新时间、对应的类型、文件大小
item->setText(m_list[i].url);
item1->setText(m_list[i].title);
item2->setText(m_list[i].keyWord); //type为调用系统的类型,以后缀来区分
item3->setText(m_list[i].result);
ui->tableWidget_2->setItem(row_count, 0, item);
ui->tableWidget_2->setItem(row_count, 1, item1);
ui->tableWidget_2->setItem(row_count, 2, item2);
ui->tableWidget_2->setItem(row_count, 3, item3);
}
}
void MainWindow::refresh()
{
for (int i = 0; i < m_list.size(); i++)
{
QTableWidgetItem *item3 = new QTableWidgetItem();
item3->setText(m_list.at(i).title);
//设置对应的图标、文件名称、最后更新时间、对应的类型、文件大小
//qDebug() << msg<<id;
ui->tableWidget_2->setItem(i, 1, item3);
}
}
void MainWindow::on_pushButton_start_clicked()
{
sqlite lite;
lite.clearData();
if(m_list.size() == 0)
return;
m_id = 0;
ui->pushButton_start->setEnabled(false);
ui->pushButton_stop->setEnabled(true);
myNetWork = new mythead(this);
connect(this, SIGNAL(stoppool()),myNetWork, SLOT(poolstop()));
// myNetWork->getHtml("https://item.taobao.com/item.htm?id=588586438027", 1);
connect(&thread, SIGNAL(started()),myNetWork, SLOT(run()));
connect(myNetWork, SIGNAL(finished()), this, SLOT(deleteLater()));
connect(&thread, SIGNAL(mySignal()), this, SLOT(refresh()));
myNetWork->moveToThread(&thread);
thread.start();
//设置最大线程数为3的一个线程池
}
void MainWindow::deleteRoew()
{
for (int i = ui->tableWidget->rowCount()-1; i >= 0; i--)
{
ui->tableWidget->removeRow(i);
}
// showTable();
}
void MainWindow::deleteRoew2()
{
for (int i = ui->tableWidget_2->rowCount()-1; i >= 0; i--)
{
ui->tableWidget_2->removeRow(i);
}
// showTable();
}
void MainWindow::httpgetid(int id, QString msg)
{
static QMutex mutex;
mutex.lock();
while (m_queue.size()>0)
{
QStringList strUrl =m_queue.front();
m_queue.pop();
// qDebug() <<"ui"<<strUrl.at(0)<<strUrl.at(1);
for (int i = 0; i < ui->tableWidget_2->rowCount(); i++)
{
QString str = ui->tableWidget_2->item(i,0)->text();
//qDebug() <<"ui55"<<m_list.at(i).url;
if(str.contains(strUrl.at(0)))
{
qDebug() <<"ui55ddd"<<strUrl.at(1)<<i;
//m_list[i].title = strUrl.at(1);
QTableWidgetItem *item3 = new QTableWidgetItem();
item3->setText(strUrl.at(1));
ui->tableWidget_2->setItem(i, 1, item3);
QString tilie =strUrl.at(1);
tilie= tilie.replace("-淘宝网","");
tilie= tilie.replace("-tmall.com","");
tilie= tilie.replace("天猫","");
sqlite lite;
if(strUrl.at(0)!="")
{
QString sql ="INSERT INTO website VALUES('"+QString::number(m_id)+"', '"+strUrl.at(0)+"','"+tilie+"', '-1','')";
lite.insertData(sql);
}
break;
}
/*
;
//设置对应的图标、文件名称、最后更新时间、对应的类型、文件大小
//qDebug() << msg<<id;
ui->tableWidget_2->setItem(i, 1, item3);*/
}
m_id++;
ui->label_info->setText(QString("%1共%2").arg(m_id).arg(ui->tableWidget_2->rowCount()));
}
if (m_id == ui->tableWidget_2->rowCount())
{
ui->pushButton_cmp->setEnabled(true);
ui->pushButton_cmpstop->setEnabled(true);
ui->pushButton_stop->setEnabled(false);
}
mutex.unlock();
}
void MainWindow::on_pushButton_stop_clicked()
{
ui->pushButton_start->setEnabled(true);
ui->pushButton_stop->setEnabled(false);
// emit stoppool();
myNetWork->poolstop();
// myNetWork->deleteLater();
// thread.quit();
// thread.wait();
}
//保存数据
void MainWindow::on_pushButton_save_clicked()
{
//保存成JSON给 PYTHON调用
QString path = QFileDialog::getSaveFileName(this,
tr("Open File"),
".",
tr("xlsx Files(*.xlsx)"));
if(!path.isEmpty()) {
//读取之前setPlainText的内容,或直接输出字符串内容QObject::tr()
QString str = test.save(path);
QMessageBox::information(NULL, "Title", str, QMessageBox::Yes, QMessageBox::Yes);
}
}
//打开一个
void MainWindow::on_pushButton_clicked()
{
//删除 list
QFile::remove("list.txt");
QString fileName;
fileName = QFileDialog::getOpenFileName(this,"Open File","","Text File(*.xlsx)");
if(fileName == "")
{
return ;
}
else
{
// QString dd = "";
test.getExcel(fileName);
readText2();
}
}
void MainWindow::on_pushButton_key_clicked()
{
QString fileName;
fileName = QFileDialog::getOpenFileName(this,"Open File","","Text File(*.xlsx)");
if(fileName == "")
{
return ;
}
else
{
m_keyPath = fileName;
ui->label_key->setText(m_keyPath);
QString dd = "";
test.getExcel(fileName);
readText();
}
//删除 list
/* QFile::remove("list.txt");
*/
}
void MainWindow::refreshOK()
{
qDebug()<<"主"<<QThread::currentThreadId();
sqlite lite;
QList<QStringList> kiki = lite.queryData("",7);
ui->label_info->setText(QString("%1").arg(kiki.size()));
// showTableOK();
}
void MainWindow::on_pushButton_cmp_clicked()
{
myNetWork =new mythead(this);
m_listlist.clear();
ui->pushButton_save->setEnabled(false);
ui->pushButton_cmp->setEnabled(false);
//将表存入sqlite
int num = ui->lineEdit_num->text().toInt();
if (num <= 0)
{
QMessageBox::information(NULL, "Title", "匹配数字不能小于等于0", QMessageBox::Yes, QMessageBox::Yes);
return;
}
myCmpWork =new MyNetwork(this);
QObject::connect(myCmpWork, SIGNAL(finished()), myCmpWork, SLOT(deleteLater()));
QObject::connect(myCmpWork, SIGNAL(getid()), this, SLOT(refreshOK()));
QObject::connect(myCmpWork, SIGNAL(finished()), this, SLOT(refreshOKcomplete()));
myCmpWork->start();
}
void MainWindow::refreshOKcomplete()
{
ui->pushButton_save->setEnabled(true);
ui->pushButton_cmp->setEnabled(true);
showTableOK();
}
////
void MainWindow::showTableOK()
{
deleteRoew();
ui->tableWidget->setColumnCount(4); //设置列数
ui->tableWidget->horizontalHeader()->setDefaultSectionSize(150);
QStringList header;
header<<tr("关键字")<<tr("标题")<<tr("相识度")<<tr("id");
ui->tableWidget->setHorizontalHeaderLabels(header);
int row_count = ui->tableWidget->rowCount(); //获取表单行数
sqlite lite;
QList<QStringList> kiki = lite.queryData("",7);
ui->label_info->setText(QString("%1").arg(kiki.size()));
for (int i = 0; i < kiki.size(); i++)
{
ui->tableWidget->insertRow(row_count); //插入新行
QStringList sqlitem = kiki.at(i);
QTableWidgetItem *item = new QTableWidgetItem();
QTableWidgetItem *item1 = new QTableWidgetItem();
QTableWidgetItem *item2 = new QTableWidgetItem();
QTableWidgetItem *item3 = new QTableWidgetItem();
//设置对应的图标、文件名称、最后更新时间、对应的类型、文件大小
item->setText(sqlitem[1]);
item1->setText(sqlitem[4]);
item2->setText(sqlitem[6]); //type为调用系统的类型,以后缀来区分
item3->setText(sqlitem[3]);
ui->tableWidget->setItem(row_count, 0, item);
ui->tableWidget->setItem(row_count, 1, item1);
ui->tableWidget->setItem(row_count, 2, item2);
ui->tableWidget->setItem(row_count, 3, item3);
}
}
void MainWindow::on_pushButton_cmpstop_clicked()
{
ui->pushButton_save->setEnabled(true);
ui->pushButton_cmp->setEnabled(true);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/perfect_1_748750778/GETTAOBAO.git
git@gitee.com:perfect_1_748750778/GETTAOBAO.git
perfect_1_748750778
GETTAOBAO
GETTAOBAO
master

搜索帮助