C ++ / Qt中的简单条带图

时间:2014-01-04 13:50:57

标签: c++ qt qtgui qtcore stripchart

我有一些数据通过串行连接进入我的C ++应用程序。现在我不想用这些数据的条形图加上一些按钮来制作一个简单的GUI。 (像10Hz刷新率那样)

按钮不是真正的问题。但是我没有为条形图找到任何Qt插件。我可以从c ++调用任何或其他库吗?考虑到它是相当简单和常见的任务应该有很多。

操作系统:Ubuntu

CC:g ++

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

这不一定需要单独的库,但是对于此类用例,通常会将QtSerialPortQwt组合在一起。

基本原则是使用异步读取。您可以在内部以固定的周期运行计时器,然后您可以在指定的每个间隔中绘制“条形图”的下一部分。

您可以在满足某个条件之前执行此操作,例如没有更多可用数据等等。你没有提到你是否正在使用QtSerialPort,但这几乎是切向的,即使在Qt项目中使用它可能是有意义的。

对于我们的异步阅读器示例,您可以在QtSerialPort中编写类似下面的代码。我们的想法是您定期附加到图形小部件中。

SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent)
    : QObject(parent)
    , m_serialPort(serialPort)
    , m_standardOutput(stdout)
{
    connect(m_serialPort, SIGNAL(readyRead()), SLOT(handleReadyRead()));
    connect(m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), SLOT(handleError(QSerialPort::SerialPortError)));
    connect(&m_timer, SIGNAL(timeout()), SLOT(handleTimeout()));

    m_timer.start(5000);
}

SerialPortReader::~SerialPortReader()
{
}

void SerialPortReader::handleReadyRead()
{
    m_readData.append(m_serialPort->readAll());
    // *** This will display the next part of the strip chart ***
    // *** Optionally make the use of a plotting library here as 'Qwt' ***
    myWidget.append('=');

    if (!m_timer.isActive())
        m_timer.start(5000);
}

void SerialPortReader::handleTimeout()
{
    if (m_readData.isEmpty()) {
        m_standardOutput << QObject::tr("No data was currently available for reading from port %1").arg(m_serialPort->portName()) << endl;
    } else {
        m_standardOutput << QObject::tr("Data successfully received from port %1").arg(m_serialPort->portName()) << endl;
        m_standardOutput << m_readData << endl;
    }

    QCoreApplication::quit();
}

void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError)
{
    if (serialPortError == QSerialPort::ReadError) {
        m_standardOutput << QObject::tr("An I/O error occurred while reading the data from port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
        QCoreApplication::exit(1);
    }
}