用qt c++重新写一次
时间: 2023-08-12 19:06:31 浏览: 156
Qt重写事件
好的,以下是用Qt C++重新编写的示例代码:
```c++
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTextDocument>
#include <QTextCursor>
#include <QTextTable>
#include <QTextTableCell>
#include <QTextCharFormat>
#include <QTextBlockFormat>
#include <QTextLength>
#include <QChartView>
#include <QLineSeries>
#include <QValueAxis>
#include <QDateTimeAxis>
#include <QChart>
#include <QUrl>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QMessageBox>
class StockAnalysisWidget : public QWidget
{
Q_OBJECT
public:
StockAnalysisWidget(QWidget *parent = nullptr);
~StockAnalysisWidget();
private slots:
void onAnalysisButtonClicked();
private:
void initUI();
void getStockData();
void handleStockData(QNetworkReply* reply);
void analyzeStockData(const QByteArray& data);
void displayResult();
void showError(const QString& error);
QGridLayout* m_layout;
QPushButton* m_analysisButton;
QLabel* m_resultLabel;
QNetworkAccessManager* m_networkManager;
QByteArray m_stockData;
};
StockAnalysisWidget::StockAnalysisWidget(QWidget *parent)
: QWidget(parent)
{
initUI();
}
StockAnalysisWidget::~StockAnalysisWidget()
{
}
void StockAnalysisWidget::initUI()
{
// 设置窗口的标题和大小
setWindowTitle(tr("股票分析"));
setGeometry(200, 200, 800, 600);
// 添加按钮和标签
m_analysisButton = new QPushButton(tr("分析"), this);
connect(m_analysisButton, &QPushButton::clicked, this, &StockAnalysisWidget::onAnalysisButtonClicked);
m_resultLabel = new QLabel(this);
m_resultLabel->setAlignment(Qt::AlignCenter);
// 使用网格布局
m_layout = new QGridLayout();
m_layout->addWidget(m_analysisButton, 0, 0);
m_layout->addWidget(m_resultLabel, 1, 0);
setLayout(m_layout);
}
void StockAnalysisWidget::onAnalysisButtonClicked()
{
// 获取股票数据
getStockData();
}
void StockAnalysisWidget::getStockData()
{
// 发送网络请求获取股票数据
QUrl url("https://finance.yahoo.com/quote/GOOG/history?p=GOOG");
QNetworkRequest request(url);
m_networkManager = new QNetworkAccessManager(this);
connect(m_networkManager, &QNetworkAccessManager::finished, this, &StockAnalysisWidget::handleStockData);
m_networkManager->get(request);
}
void StockAnalysisWidget::handleStockData(QNetworkReply* reply)
{
// 处理股票数据
if (reply->error() == QNetworkReply::NoError) {
m_stockData = reply->readAll();
analyzeStockData(m_stockData);
} else {
showError(reply->errorString());
}
reply->deleteLater();
}
void StockAnalysisWidget::analyzeStockData(const QByteArray& data)
{
// 对股票数据进行分析
QTextDocument doc;
doc.setHtml(QString::fromUtf8(data));
QTextCursor cursor(&doc);
QTextTable* table = cursor.currentTable();
if (table) {
int rowCount = table->rows();
QVector<double> closeData(rowCount), ma5Data(rowCount), ma10Data(rowCount), ma20Data(rowCount);
for (int i = 1; i < rowCount; ++i) {
QTextTableCell dateCell = table->cellAt(i, 0);
QTextTableCell closeCell = table->cellAt(i, 4);
closeData[i] = closeCell.toPlainText().toDouble();
if (i >= 5) {
ma5Data[i] = std::accumulate(closeData.begin() + i - 4, closeData.begin() + i + 1, 0.0) / 5;
}
if (i >= 10) {
ma10Data[i] = std::accumulate(closeData.begin() + i - 9, closeData.begin() + i + 1, 0.0) / 10;
}
if (i >= 20) {
ma20Data[i] = std::accumulate(closeData.begin() + i - 19, closeData.begin() + i + 1, 0.0) / 20;
}
}
// 显示分析结果
m_resultLabel->setText(tr("分析完成"));
// 绘制图表
QLineSeries* closeSeries = new QLineSeries(this);
for (int i = 1; i < rowCount; ++i) {
closeSeries->append(i, closeData[i]);
}
QLineSeries* ma5Series = new QLineSeries(this);
for (int i = 5; i < rowCount; ++i) {
ma5Series->append(i, ma5Data[i]);
}
QLineSeries* ma10Series = new QLineSeries(this);
for (int i = 10; i < rowCount; ++i) {
ma10Series->append(i, ma10Data[i]);
}
QLineSeries* ma20Series = new QLineSeries(this);
for (int i = 20; i < rowCount; ++i) {
ma20Series->append(i, ma20Data[i]);
}
QChart* chart = new QChart();
chart->addSeries(closeSeries);
chart->addSeries(ma5Series);
chart->addSeries(ma10Series);
chart->addSeries(ma20Series);
chart->setTitle(tr("股票走势图"));
chart->setAnimationOptions(QChart::SeriesAnimations);
chart->createDefaultAxes();
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
QDateTimeAxis* axisX = new QDateTimeAxis(this);
axisX->setFormat("MM/dd");
axisX->setTitleText(tr("日期"));
axisX->setTickCount(rowCount);
axisX->setRange(QDateTime::fromString(table->cellAt(rowCount - 1, 0).toPlainText(), "MM/dd/yy"),
QDateTime::fromString(table->cellAt(1, 0).toPlainText(), "MM/dd/yy"));
chart->setAxisX(axisX, closeSeries);
QValueAxis* axisY = new QValueAxis(this);
axisY->setTitleText(tr("股价"));
chart->setAxisY(axisY, closeSeries);
QChartView* chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->show();
} else {
showError(tr("股票数据解析失败"));
}
}
void StockAnalysisWidget::displayResult()
{
// 显示分析结果
m_resultLabel->setText(tr("分析完成"));
}
void StockAnalysisWidget::showError(const QString& error)
{
// 显示错误信息
QMessageBox::critical(this, tr("错误"), error);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StockAnalysisWidget w;
w.show();
return a.exec();
}
```
在这个示例代码中,我们同样使用了Qt C++创建一个窗口,添加了一个按钮和标签,并通过按钮的点击事件获取股票数据并进行简单的分析,最后显示分析结果。
需要注意的是,由于C++和Python的语法和特性不同,因此在实现过程中有些细节会有所不同。例如,我们使用了Qt提供的网络模块和图表库来获取股票数据并绘制图表。如果您对C++编程有一定的经验,相信您可以轻松理解这段代码并进行修改和扩展。
希望这个示例代码能够帮助到您,祝您编写成功!
阅读全文