wumu
2025-02-21 987254bf420571a7b89391d8227569a1b5774019
250221
6个文件已修改
355 ■■■■■ 已修改文件
clientmainwindow.cpp 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
getstocklist.cpp 74 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
getstocklist.h 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
historydata.cpp 96 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
historydata.h 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
historydata.ui 164 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
clientmainwindow.cpp
@@ -54,4 +54,7 @@
    connect(m_getStockList,SIGNAL(sendCodeNames(QMap<QString,QString>&)),m_history,SLOT(putCodeNames(QMap<QString,QString>&)));
    connect(m_history,SIGNAL(sendHistoryModel(QVector<QVector<QString>> &)),m_getStockList,SLOT(showHistoryModelData(QVector<QVector<QString>> &)));
    connect(m_history,SIGNAL(sendHistoryModel(QStandardItemModel*)),m_getStockList,SLOT(showHistoryModelData(QStandardItemModel*)));
}
getstocklist.cpp
@@ -7,6 +7,7 @@
#include <QJsonObject>
#include <QJsonArray>
#include <QLibrary> // 用于加载dll动态库的类
#include <QDateTime>
#pragma execution_character_set("utf-8")
@@ -19,6 +20,8 @@
    ui->pushButton_get->hide();
    ui->statusbar->hide();
    // 注册元类型
    qRegisterMetaType<QVector<QVector<QString>>>("QVector<QVector<QString>> &");
    // 方案2:
    // 如果https 访问的时候,还是爬取失败,可以代码加载动态库来支持
    // 前提条件是把2个dll动态库拷贝的生成目录中:libeay32.dll 和 ssleay32.dll
@@ -985,3 +988,74 @@
{
    on_pushButton_search_clicked();
}
void GetStockList::showHistoryModelData(QVector<QVector<QString>> &model) // 从历史那边将数据发过来
{
    m_model->setRowCount(model.size());
    for(int i=0;i<model.size();++i){
        qDebug()<<"i="<<i;
        for(int j=0;j<model.at(0).size();++j){
            //QString str = model[i][j];
            //qDebug()<<str;
            m_model->setItem(i,j,new QStandardItem(model[i][j]));
            if(j<3){
                m_model->setItem(i,j,new QStandardItem(model[i][j]));
                if(j==2){
                    m_model->setItem(i,j+1,new QStandardItem(model[i][j]));
                }
            }else{
                m_model->setItem(i,j+1,new QStandardItem(model[i][j]));
            }
        }
    }
    qDebug()<<"历史到信息赋值完成";
}
void GetStockList::showHistoryModelData(QStandardItemModel *model)
{
    //ui->tableView->setModel(model);
    QVector<QVector<QString>> vec;
    for(int i=0;i<model->rowCount();++i){
        QString row;
        QVector<QString> tmp;
        m_codeNames[model->item(i,1)->text()] = model->item(i,0)->text();
        for(int j=0;j<model->columnCount();++j){
            row+=model->item(i,j)->text()+" ";
            tmp.append(model->item(i,j)->text());
        }
        qDebug()<<i<<row;
        vec.append(tmp);
    }
    qDebug()<<"vec size:"<<vec.size();
    qDebug()<<"历史到信息赋值完成";
    qDebug()<<QDateTime::currentDateTime();
    m_model->setRowCount(0);
    for(int i=0;i<vec.size();++i){
        QList<QStandardItem*> items;
        for(int j=0;j<vec.at(0).size();++j){
            items.append(new QStandardItem(vec.at(i).at(j)));
            if(j==2){
                items.append(new QStandardItem(vec.at(i).at(j)));
            }
            if(j==3){
                QStandardItem *percentItem =  items.back();
                if(percentItem->text().toDouble() > 0){
                    percentItem->setData(QColor("red"),Qt::DecorationRole); // 添加一个装饰的颜色为红色
                    percentItem->setData(QColor("red"),Qt::TextColorRole);  // 将字体颜色设置为红色
                    items.at(0)->setData(QColor("red"),Qt::TextColorRole);  // 将股票名字设置为红色
                 }
                else if(percentItem->text().toDouble() < 0){
                    percentItem->setData(QColor("green"),Qt::BackgroundColorRole);
                    items.at(0)->setData(QColor("green"),Qt::TextColorRole);
                }
            }
        }
        m_model->appendRow(items);
    }
    qDebug()<<QDateTime::currentDateTime();
}
getstocklist.h
@@ -77,6 +77,9 @@
    void on_lineEdit_search_returnPressed();
    void showHistoryModelData(QVector<QVector<QString>> &model);
    void showHistoryModelData(QStandardItemModel *model);
private:
    Ui::GetStockList *ui;
    // 这2个用于爬取数据的
historydata.cpp
@@ -19,6 +19,26 @@
{
    ui->setupUi(this);
    // 注册元类型
    qRegisterMetaType<QVector<QVector<QString>>>("QVector<QVector<QString>>");
    qRegisterMetaType<QVector<QVector<QString>>>("QVector<QVector<QString>> &");
    // ui相关
    ui->dateEdit->setDate(QDate::currentDate());
    m_model = new QStandardItemModel(1,9,this);
    m_customModel = new CustomSortProxyModel; // 可自定义排序的模型
    m_customModel->setSourceModel(m_model); // 设置源模型
    //ui->tableView->setModel(m_model);
    ui->tableView->setModel(m_customModel);
    // 表格列排序
    ui->tableView->setSortingEnabled(true);
    QStringList labels;
    labels<<"股票名字"<<"股票代号"<<"市值(亿)"<<"涨跌幅"<<"收盘价"<<"成交额(亿)"<<"成交量"<<"换手率"<<"PE_TTM";
    m_model->setHorizontalHeaderLabels(labels);
    initMySQL(); // 初始化MySQL
    // 处理数据爬取的操作
@@ -202,3 +222,79 @@
    }
}
void HistoryData::on_pushButton_search_clicked()
{
    // 提示查询等待
    ui->statusbar->showMessage("查询中...请耐心等待结果...");
    QString searchTime = ui->dateEdit->text();
    qDebug()<<"查询的时间:"<<searchTime;
    QString sql = QString("select name,code,market_capital,percent,close,amount,volume,turnover_rate,pe_ttm from stock_day_info where time_trade = '%1'").arg(searchTime);
    qDebug()<<"sql:"<<sql;
    m_modelDatas.clear(); // 清空数组中的数据
    qint64 tt = QDateTime::currentMSecsSinceEpoch();
    QSqlQuery que(db);
    if(que.exec(sql)){
        qDebug()<<"select ok";
        m_model->setRowCount(0); // 重置模型行数
        int rows = 0;
        while (que.next()) {
            QString name = que.value(0).toString();
            QString code = que.value(1).toString();
            QString market_capital = QString::number(que.value(2).toDouble()/100000000);
            QString percent = que.value(3).toString();
            QString close = que.value(4).toString();
            QString amount = QString::number(que.value(5).toDouble()/100000000);
            QString volume = que.value(6).toString();
            QString turnover_rate = que.value(7).toString();
            QString pe_ttm = que.value(8).toString();
            m_modelDatas.append({name,code,market_capital,percent,close,amount,volume,turnover_rate,pe_ttm});
            QList<QStandardItem*> items;
            items.append(new QStandardItem(name));
            items.append(new QStandardItem(code));
            items.append(new QStandardItem(market_capital));
            //items.append(new QStandardItem(percent));
            QStandardItem *percentItem =  new QStandardItem(percent);
            if(percent.toDouble() > 0){
                percentItem->setData(QColor("red"),Qt::DecorationRole); // 添加一个装饰的颜色为红色
                percentItem->setData(QColor("red"),Qt::TextColorRole);  // 将字体颜色设置为红色
                items.at(0)->setData(QColor("red"),Qt::TextColorRole);  // 将股票名字设置为红色
             }
            else if(percent.toDouble() < 0){
                percentItem->setData(QColor("green"),Qt::BackgroundColorRole);
                items.at(0)->setData(QColor("green"),Qt::TextColorRole);
            }
            items.append(percentItem);
            items.append(new QStandardItem(close));
            items.append(new QStandardItem(amount));
            items.append(new QStandardItem(volume));
            items.append(new QStandardItem(turnover_rate));
            items.append(new QStandardItem(pe_ttm));
            m_model->appendRow(items);
            rows++;
        }
        qDebug()<<"查询到行数:"<<rows;
    }else{
        qDebug()<<"select fail"<<que.lastError().text();
    }
    // 最后提示完成
    qint64 need = QDateTime::currentMSecsSinceEpoch()-tt;
    ui->statusbar->showMessage(QString("耗时:%1 毫秒,查询完成.").arg(QString::number(need)),10*1000);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //emit sendHistoryModel(m_modelDatas); // 给信息展示发模型
    ui->textBrowser->append(QString("查询日期:%1 耗时: %2 毫秒 查到条数:%3").arg(searchTime).arg(QString::number(need)).arg(QString::number(m_model->rowCount())));
}
void HistoryData::on_pushButton_2_clicked()
{
    //emit sendHistoryModel(m_modelDatas); // 给信息展示发模型
    emit sendHistoryModel(m_model);
}
historydata.h
@@ -8,6 +8,8 @@
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QStandardItemModel>
#include "customsortproxymodel.h"
namespace Ui {
class HistoryData;
@@ -23,6 +25,10 @@
    void initMySQL();
signals:
    void sendHistoryModel(QVector<QVector<QString>> &); // 将模型发给信息展示
    void sendHistoryModel(QStandardItemModel *); // 将模型发给信息展示
private slots:
    void on_pushButton_clicked();
@@ -33,6 +39,10 @@
    void showAplyData(QNetworkReply* reply);
    void getOneStock(QByteArray &buffer);
    
    void on_pushButton_search_clicked();
    void on_pushButton_2_clicked();
private:
    Ui::HistoryData *ui;
@@ -43,6 +53,11 @@
    QNetworkRequest m_request; // 用于管理请求头的
    QString m_cookie; // 缓存
    QNetworkAccessManager m_manager; // 用于管理http请求及响应的
    QStandardItemModel *m_model;
    CustomSortProxyModel *m_customModel; // 用于排序的自定义模型
    QVector<QVector<QString>> m_modelDatas; // 行列存储
};
#endif // HISTORYDATA_H
historydata.ui
@@ -6,47 +6,153 @@
   <rect>
    <x>0</x>
    <y>0</y>
    <width>827</width>
    <height>711</height>
    <width>859</width>
    <height>779</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>540</x>
      <y>20</y>
      <width>101</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>Test</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_update">
    <property name="geometry">
     <rect>
      <x>650</x>
      <y>20</y>
      <width>111</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>一键更新数据</string>
    </property>
   </widget>
   <layout class="QGridLayout" name="gridLayout_3">
    <item row="0" column="0">
     <widget class="QGroupBox" name="groupBox">
      <property name="title">
       <string>操作相关</string>
      </property>
      <layout class="QGridLayout" name="gridLayout">
       <item row="0" column="0">
        <layout class="QHBoxLayout" name="horizontalLayout">
         <item>
          <widget class="QPushButton" name="pushButton">
           <property name="text">
            <string>Test</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="pushButton_update">
           <property name="enabled">
            <bool>false</bool>
           </property>
           <property name="text">
            <string>一键更新数据</string>
           </property>
          </widget>
         </item>
        </layout>
       </item>
       <item row="1" column="0">
        <layout class="QHBoxLayout" name="horizontalLayout_2">
         <item>
          <widget class="QDateEdit" name="dateEdit">
           <property name="displayFormat">
            <string>yyyy-MM-dd</string>
           </property>
           <property name="calendarPopup">
            <bool>true</bool>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="pushButton_search">
           <property name="text">
            <string>查询</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="pushButton_2">
           <property name="text">
            <string>发送历史到信息展示</string>
           </property>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QTableView" name="tableView"/>
    </item>
    <item row="2" column="0">
     <layout class="QHBoxLayout" name="horizontalLayout_3">
      <item>
       <layout class="QVBoxLayout" name="verticalLayout_3">
        <item>
         <widget class="QTextBrowser" name="textBrowser"/>
        </item>
        <item>
         <widget class="QGroupBox" name="groupBox_2">
          <property name="title">
           <string>添加到监测股票池</string>
          </property>
          <layout class="QGridLayout" name="gridLayout_2">
           <item row="0" column="0">
            <layout class="QVBoxLayout" name="verticalLayout">
             <item>
              <widget class="QLineEdit" name="lineEdit">
               <property name="placeholderText">
                <string>输入股票名称</string>
               </property>
              </widget>
             </item>
             <item>
              <widget class="QLineEdit" name="lineEdit_2">
               <property name="placeholderText">
                <string>输入股票代号</string>
               </property>
              </widget>
             </item>
             <item>
              <widget class="QLineEdit" name="lineEdit_3">
               <property name="placeholderText">
                <string>输入股票价格</string>
               </property>
              </widget>
             </item>
            </layout>
           </item>
           <item row="0" column="1">
            <layout class="QVBoxLayout" name="verticalLayout_2">
             <item>
              <widget class="QDateEdit" name="dateEdit_2">
               <property name="displayFormat">
                <string>yyyy-MM-dd</string>
               </property>
               <property name="calendarPopup">
                <bool>true</bool>
               </property>
              </widget>
             </item>
             <item>
              <widget class="QPushButton" name="pushButton_3">
               <property name="text">
                <string>添加</string>
               </property>
              </widget>
             </item>
            </layout>
           </item>
          </layout>
         </widget>
        </item>
       </layout>
      </item>
      <item>
       <widget class="QTableView" name="tableView_2"/>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>827</width>
     <width>859</width>
     <height>26</height>
    </rect>
   </property>