QDBus发送int []

时间:2017-07-09 16:45:22

标签: c++ qt dbus

我目前正在使用QDBus。我正在尝试将一个int数组数组发送到另一个程序,该程序将对数组进行一些计算,并且应该在之后将其发回。

我尝试将发送地址的数组作为int发送。 我相信这里的问题是我必须以某种方式发送价值的副本,以便我的其他prgoram最终不会试图弄乱他的记忆。

我目前正在获取此调试输出:

sessionBus is connected...
Starting line conversion...
Done converting...
Data at:  -1077790240 
Reply invalid:  Message recipient disconnected from message bus without replying 
Starting line conversion...
Done converting...
Data at:  -1077790240 
Reply invalid:  The name domain.comSim was not provided by any .service files 
Starting line conversion...
Done converting...
Data at:  -1077790240 
Reply invalid:  The name domain.comSim was not provided by any .service files

从这段代码:

void MainWindow::on_btnSend_clicked()
{
    if (!QDBusConnection::sessionBus().isConnected())
    {
        qDebug() << "Cannot connect to the D-Bus session bus.\r\n";

        return;
    }

    qDebug() << "sessionBus is connected...\r\n";

    QDBusInterface iface(SERVICE_NAME, "/", "", QDBusConnection::sessionBus());

    if (iface.isValid())
    {
        int height = this->image->height();
        int width = this->image->width();

        for(int lineCounter = 0; lineCounter < height; lineCounter++)
        {
            qDebug() << "Starting line conversion...\r\n";

            QRgb* lineValues = (QRgb*)this->image->scanLine(lineCounter);

            /*

            QList<QVariant> args;

            args << QVariant::fromValue(width);

            for(int pixelCounter = 0; pixelCounter < width; pixelCounter++)
            {
                args << QVariant::fromValue(qAlpha(lineValues[pixelCounter]));
                args << QVariant::fromValue(qRed(lineValues[pixelCounter]));
                args << QVariant::fromValue(qGreen(lineValues[pixelCounter]));
                args << QVariant::fromValue(qBlue(lineValues[pixelCounter]));
                ]
            }*/


            int values[width][4];

            for(int c = 0; c < width; c++)
            {
                values[c][0] = qAlpha(lineValues[c]);
                values[c][1] = qRed(lineValues[c]);
                values[c][2] = qGreen(lineValues[c]);
                values[c][3] = qBlue(lineValues[c]);
            }

            qDebug() << "Done converting...\r\n";
            qDebug() << "Data at: " << (int)values << "\r\n";

            QDBusReply<int> reply = iface.call("processLine", (int)values, width);

            if (!reply.isValid())
            {
                qDebug() << "Reply invalid: " << qPrintable(reply.error().message()) << "\r\n";
            }
        }
    }
    else
    {
        qDebug() << "Interface is invalid: " << qPrintable(QDBusConnection::sessionBus().lastError().message());
    }
}

正如你所看到的,我也试图将我的值列入QVariants列表,并尝试通过callWithArgumentList发送它,但我还没有找到一个关于如何编写将处理这些调用的签名的示例。 / p>

目前我的班级导出他的插槽如下:

class Simulation : public QObject
{
    Q_OBJECT

public slots:
    Q_SCRIPTABLE int processLine(int dataPtr, int length);
};

和&#34; processLine&#34;像这样:

int Simulation::processLine(int dataPtr, int length)
{
    //int values[length][4];
    int** values = (int**)dataPtr;

    cout << "Data should be at: " << dataPtr << endl;

    cout << "Should start printing pixel values now:" << endl;

    for(int c = 0; c < length; c++)
    {
        cout << "Pixel #" << c << ": Alpha:" << values[c][0] << " Red:" << values[c][1] << " Green:" << values[c][2] << " Blue:" << values[c][3] << endl;
    }

    return dataPtr;
}

打印后崩溃&#34;现在应该开始打印像素值:&#34;。

所以我的主要问题是解决这个问题的正确和最小方法是什么(通过dbus发送一个int [])?

问候, 罗宾

1 个答案:

答案 0 :(得分:0)

因为当你开始使用某些东西时,你很可能不熟悉解决方案非常简单。一件事是包括QVariant

我现在可以使用以下代码传输所有数据:

 if (iface.isValid())
    {
        int height = this->image->height();
        int width = this->image->width();        

        QImage* recvImage = new QImage(width, height, QImage::Format_ARGB32);

        for(int lineCounter = 0; lineCounter < height; lineCounter++)
        {
            qDebug() << "Starting line conversion...\r\n";

            QRgb* lineValues = (QRgb*)this->image->scanLine(lineCounter);

            QList<int> values;

            for(int c = 0; c < width; c++)
            {
                values.append(lineValues[c]);
                //values.append(qAlpha(lineValues[c]));
                //values.append(qRed(lineValues[c]));
                //values.append(qGreen(lineValues[c]));
                //values.append(qBlue(lineValues[c]));
            }

            QVariant arg;
            arg.setValue(values);

            QDBusReply<QList<int> > reply = iface.call("processLine", arg);

            if (!reply.isValid())
            {
                qDebug() << "Reply invalid: " << qPrintable(reply.error().message()) << "\r\n";
            }

            QList<int> recvValues = reply.value();
            QRgb* recvLineValues = (QRgb*)recvImage->scanLine(lineCounter);

            for(int c = 0; c < width; c++)
            {
                //int alpha = recvValues.at(4*c);
                //int red = recvValues.at(4*c);
                //int green = recvValues.at(4*c);
                //int blue = recvValues.at(4*c);
                //recvLineValues[c] = qRgba(red, green, blue, alpha);
                recvLineValues[c] = recvValues.at(c);
            }

            QGraphicsScene* scene = new QGraphicsScene();
            QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(*(this->image)));

            scene->addItem(item);

            ui->gViewRecvImage->setScene(scene);
            ui->gViewRecvImage->show();

        }
    }