如何将信号附加到 qPixmap?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该存储从中返回的
QGraphicsPixmapItem*
指针:(例如使用
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:(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 fromQObject
, 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 fromQGraphicsPixmapItem
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.