Building a Real-time Stock Data Display System with C++ and Qt

System Overview

This implementation demonstrates a real-time stock data display system using C++ and Qt framework. The system fetches stock market data from free online APIs, processes it locally, and presents it through a multi-window interface.

System Architecture

The system follows a modular architecture with clear separation of concerns:

Data Center Module

The data center manages stock subscriptions and ensures data consistency across multiple UI components. It implements the observer pattern for efficient data distribution.

class IMarketDataHub {
public:
    virtual ~IMarketDataHub() {}
    
    // Subscribe to stock data updates
    virtual void SubscribeStockData(IStockObserver* observer, 
                                  const StockIdentifier& stock) = 0;
    
    // Unsubscribe from all stock data
    virtual void UnsubscribeAll(IStockObserver* observer) = 0;
};

Data Retrieval System

The data retrieval module handles asynchronous fetching of stock data using libcurl, ensuring non-blocking operations through Qt's thread pool.

class NetworkManager {
public:
    NetworkManager() {
        curl_global_init(CURL_GLOBAL_DEFAULT);
        QThreadPool::globalInstance()->setMaxThreadCount(8);
    }
    
    void SubmitTask(NetworkTask* task) {
        QThreadPool::globalInstance()->start(task);
    }
};

class StockDataFetcher : public NetworkTask {
    Q_OBJECT
public:
    StockDataFetcher(const QString& outputPath);
    
signals:
    void DataReady(const QString& filePath);
    
protected:
    virtual void ExecuteRequest(CURL* curl) override;
    virtual void ProcessResponse(char* data, size_t length) override;
    
private:
    QString outputFilePath;
    QFile outputFile;
};

Core Services

The system provides essential services for market status checking and timing operations:

class MarketStatusService {
public:
    virtual bool IsMarketOpen(const StockIdentifier& stock, 
                            long long timestamp) const = 0;
};

class TimerService {
public:
    virtual QString ScheduleTask(std::function<void long=""> callback,
                               long interval = 3000) = 0;
};
</void>

User Interface Components

The UI displays stock data in three sections: basic trading information, bid-ask spreads, and raw data source.

class StockDisplayWidget : public IStockObserver {
public:
    void UpdateStockData(const StockData& data) override {
        UpdateBasicInfo(data);
        UpdateBidAskSpread(data);
        ShowRawData(data.source);
    }
    
private:
    void UpdateBasicInfo(const StockData& data);
    void UpdateBidAskSpread(const StockData& data);
};

Data Structure

The system uses comprehensive data structures to represent stock information:

struct StockData {
    QString symbol;
    QString name;
    double openPrice;
    double previousClose;
    double currentPrice;
    double highPrice;
    double lowPrice;
    
    struct PriceLevel {
        double price;
        double volume;
    };
    
    QVector<PriceLevel> bidLevels;  // Buy orders
    QVector<PriceLevel> askLevels;  // Sell orders
    
    QString tradingDate;
    QString updateTime;
    QString rawData;
};

Implementation Details

The system employs a 3-second polling mechanism to fetch updated stock data. Each data fetch operation creates a worker task that executes in a seperate thread, preventing UI blocking.

For the bid-ask spread display, the system uses a custom delegate for rendering:

class BidAskDelegate : public QStyledItemDelegate {
public:
    void paint(QPainter* painter, 
               const QStyleOptionViewItem& option,
               const QModelIndex& index) const override {
        const StockData& stockData = index.data().value<StockData>();
        RenderBidAskSpread(painter, option, stockData);
    }
};

The data center automatically manages subscriptions and ensures that duplicate stock requests are consolidated to minimize network traffic.

Tags: C++ Qt StockData RealTime libcurl

Posted on Fri, 07 Aug 2026 16:39:28 +0000 by Ilovetopk