How to output the data from tablemodel?

  • In the function sendSettingParameters(void) of class RemoteControl, I want to get all the data from QList<QList<QVariant> > stepTable in the class steptablemodel output to QDataStream "out" so that it can be send out through UDP socket. The way I tried with function stepTable2Stream() is not working. I tried to overload << for class steptablemode, but it cannot access stepTable from the overload function <<. How can I do it? Thanks. The data inside QList<QList<QVariant> > stepTable are int or float

    @class RemoteContol : public QWidget

    explicit RemoteContol(QWidget *parent = 0);

    Ui::RemoteContol *ui;

    QUdpSocket udpSocket;
    quint8 baseTime;    // 0:GPS 1: UTC
    quint8 opMode;      // 0:Manual 1:Auto
    quint8 waveform;    // 0:FD 1:TD50 2:TD33 3:TD25
    steptablemodel StepTableModel;

    class steptablemodel : public QAbstractTableModel
    explicit steptablemodel(QObject *parent = 0);
    void setStepTable(QList<QList<QVariant> > &tablelist);
    friend QDataStream& operator <<(QDataStream& out, const steptablemodel& steptable);
    QDataStream stepTable2Stream();

    public slots:
    void insertAStep(QList<QVariant> step, const QModelIndex &index);
    QList<QList<QVariant> > stepTable;

    void RemoteContol::sendSettingParameters(void)
    float a=2.3;
    QByteArray datagram;
    QDataStream out(&datagram,QIODevice::WriteOnly);
    out << baseTime;
    if (opMode==0) { //manual mode
    out << opMode;
    if (ui->FreqSetcomboBox->currentIndex()==0)
    out << (float)ui->FREQSetBox->value();
    out << 1/(float)ui->FREQSetBox->value();
    out << (float) ui->CurrentSetBox->value()
    << (quint8) 0 << waveform
    << (quint16) ui->VoltageSetBox->value()
    << (quint8) ui->PowerspinBox->value();
    } else { // auto stepping table mode
    out = StepTableModel.stepTable2Stream(); <---- here causes the problem

    QDataStream steptablemodel::stepTable2Stream()
    QDataStream stream;
    stream << stepTable.size();
    for (int i=0; i<stepTable.size(); i++) {
    stream <<stepTable[i][0].toFloat()<<stepTable[i][1].toFloat()
    if (stepTable[i][3].toString().compare("FD")==0)
    stream << (quint8) 0;
    else if (stepTable[i][3].toString().compare("TD50")==0)
    stream << (quint8) 1;
    else if (stepTable[i][3].toString().compare("TD33")==0)
    stream << (quint8) 2;
    else if (stepTable[i][3].toString().compare("TD25")==0)
    stream << (quint8) 3;
    stream <<stepTable[i][4].toInt()<<stepTable[i][5].toInt();
    return stream;

  • I guess your problem is that the QDataStream (as the name already says) is a stream, which means it does not store the data itself, it is just a class used for serialization into a QIODevice.
    So you should either use the stream to write to such a device inside stepTable2Stream() or return your data as something like a QByteArray and put that into a stream outside...

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.