如何将信号附加到 qPixmap?

发布于 2024-12-08 12:51:01 字数 1187 浏览 0 评论 0原文

我正在使用 C++ Qt 创建一个应用程序,并且想要加载多个图像。我想为每个图像附加一个信号,以便之后可以启用或禁用它们。

有什么帮助吗?

编辑1:

imageDlg = new QFileDialog();
imageList =  imageDlg->getOpenFileNames(this,
                         tr("Open Document"),
                         QDir::currentPath(),
                         tr("Image Files (*.png *.jpg);;All files (*.*)"));


QString imageName;
int x = -50;
int y = -50;
int n = 1;
double size = imageList.size();

if(imageList.isEmpty())
    return;

scene->clear();
setCursor(Qt::WaitCursor);


foreach(imageName,imageList)
{
    double val = (n/size)*100;
    ui->progressBar->setValue((int)val);


    image.load(imageName,"4",Qt::AutoColor);

    image = image.scaled(100,100,Qt::KeepAspectRatio,Qt::FastTransformation);
    imageNames.push_back(imageName.toStdString());

   // scene->setSceneRect(x,y,100,100);
    item = scene->addPixmap(image);
    item->setPos(x,y);
    x = x + 110;

    if(n%4 == 0)
    {
        x = -50;
        y = y + 90;
    }

    n++;
}

//ui->label_2->setText(strcat("10","image(s) loaded successfully"));
setCursor(Qt::ArrowCursor);
ui->imageGraphicsView->setScene(scene);

I'm creating an application using C++ Qt and I want to load multiple images. I would like to attach a signal to each image so that I can enable or disable them afterwards.

Any help?

Edit 1:

imageDlg = new QFileDialog();
imageList =  imageDlg->getOpenFileNames(this,
                         tr("Open Document"),
                         QDir::currentPath(),
                         tr("Image Files (*.png *.jpg);;All files (*.*)"));


QString imageName;
int x = -50;
int y = -50;
int n = 1;
double size = imageList.size();

if(imageList.isEmpty())
    return;

scene->clear();
setCursor(Qt::WaitCursor);


foreach(imageName,imageList)
{
    double val = (n/size)*100;
    ui->progressBar->setValue((int)val);


    image.load(imageName,"4",Qt::AutoColor);

    image = image.scaled(100,100,Qt::KeepAspectRatio,Qt::FastTransformation);
    imageNames.push_back(imageName.toStdString());

   // scene->setSceneRect(x,y,100,100);
    item = scene->addPixmap(image);
    item->setPos(x,y);
    x = x + 110;

    if(n%4 == 0)
    {
        x = -50;
        y = y + 90;
    }

    n++;
}

//ui->label_2->setText(strcat("10","image(s) loaded successfully"));
setCursor(Qt::ArrowCursor);
ui->imageGraphicsView->setScene(scene);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不寐倦长更 2024-12-15 12:51:01

您应该存储从中返回的 QGraphicsPixmapItem* 指针:(

scene->addPixmap();

例如使用 QList 或您选择的其他容器。)

这些是场景中显示的对象。您可以通过这些指针更改它们的外观、显示或隐藏它们、更改它们的不透明度等。

有关详细信息,请参阅 QGraphicsItem 的文档您如何操作这些项目。

QGraphicsItem 不是从 QObject 继承的,它没有信号或槽(从它派生的类也没有)。如果您想处理鼠标事件,则需要创建一个自定义图形项(例如,从QGraphicsPixmapItem派生)并重新实现您感兴趣的事件处理函数。

查看< href="http://doc.qt.nokia.com/latest/graphicsview-elasticnodes.html" rel="nofollow">弹性节点示例获取如何处理图形鼠标事件的示例项目。

You should be storing the QGraphicsPixmapItem* pointers you're getting back from:

scene->addPixmap();

(Use e.g. a QList<QGraphicsPixmapItem*> or another container of your choice.)

These are the objects that are displayed on your scene. You can change their appearance, show or hide them, change their opacity etc. through those pointers.

Look at the documentation for QGraphicsItem for detailed information about how you can manipulate these items.

QGraphicsItem doesn't inherit from QObject, it doesn't have signals or slots (the classes derived from it don't either). If you want to handle mouse events, you'll need to create a custom graphics item (derived from QGraphicsPixmapItem for example) and re-implement the event handling functions you're interested in.

Look at the Elastic Nodes example to get a sample of how you can handle mouse events for graphics items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文