QDBus 发送 int[]

QDBus send int[]

我目前正在开始使用 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 发送它,但我还没有找到一个关于如何编写签名来处理这些调用的示例。

目前我的 class 导出他的插槽如下所示:

class Simulation : public QObject
{
    Q_OBJECT

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

和"processLine"像这样:

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;
}

打印 "Should start printing pixel values now:" 后崩溃。

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

您好, 罗宾

因为最有可能在您开始使用您不熟悉的东西时,解决方案非常简单。一件事是包括 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();

        }
    }