首页 \ 问答 \ JavaFX:ImageView只显示一次Image(JavaFX: ImageView shows Image only once)

JavaFX:ImageView只显示一次Image(JavaFX: ImageView shows Image only once)

在我的JavaFX应用程序中,我使用TreeView。 单个TreeItems包含一个图像。 不幸的是,图像只显示一次。

一个显示问题的GIF

加载图像的代码如下所示。 每次TreeView更改时都会调用它。 Image被缓存在Map (“图标”)中。 因此,每次都会创建一个新的ImageView

public final ImageView getImage(final String imageConstant) {
    final Image img;
    if (icons.containsKey(imageConstant)) {
        img = icons.get(imageConstant);
    }
    else {
        if (isRunFromJAR) {
           img = new Image("/path/" + imageConstant, iconSizeWidth,
                    iconSizeHeight, false, false);
        }
        else {
            img = new Image(getClass().getResourceAsStream(imageConstant), iconSizeWidth,
                    iconSizeHeight, false, false);
        }
        icons.put(imageConstant, img);
    }
    return new ImageView(img);

在我的TreeCellupdateItem方法中,上面返回的ImageView存储在名为icon的字段中。 该字段在以下代码中使用,也来自updateItem

if (icon == null) {
        setGraphic(label);
    }
    else {
        final HBox hbox = new HBox(ICON_SPACING);
        final Label iconLabel = new Label("", icon);
        hbox.getChildren().addAll(iconLabel, label);
        setGraphic(hbox);
    }

但由于某种原因,gif中出现的问题出现了。

更新

实际上,ImageViews是在内部缓存的,这导致了重复数据删除。 使用多个ImageView是解决方案。


In my JavaFX application I use a TreeView. The single TreeItems contain an image. Unfortunately, the image is shown only once.

a gif showing the problem

The code for loading the image looks like the following. It is called every time the TreeView changes. The Images are cached in a Map ("icons"). Thus, a new ImageView is created every time.

public final ImageView getImage(final String imageConstant) {
    final Image img;
    if (icons.containsKey(imageConstant)) {
        img = icons.get(imageConstant);
    }
    else {
        if (isRunFromJAR) {
           img = new Image("/path/" + imageConstant, iconSizeWidth,
                    iconSizeHeight, false, false);
        }
        else {
            img = new Image(getClass().getResourceAsStream(imageConstant), iconSizeWidth,
                    iconSizeHeight, false, false);
        }
        icons.put(imageConstant, img);
    }
    return new ImageView(img);

In the updateItem method of my TreeCells, the ImageView returned above is stored into a field called icon. The field is used in the following code, also from updateItem:

if (icon == null) {
        setGraphic(label);
    }
    else {
        final HBox hbox = new HBox(ICON_SPACING);
        final Label iconLabel = new Label("", icon);
        hbox.getChildren().addAll(iconLabel, label);
        setGraphic(hbox);
    }

But for some reason, the problem shown in the gif occurs.

Update

Actually, the ImageViews were cached internally, which led to de-duplication. Using multiple ImageViews is the solution.


原文:https://stackoverflow.com/questions/36280987
更新时间:2022-10-24 15:10

最满意答案

问题在于你试图定义一个名为Signal()的函数,

Qt为你生成“信号”函数的主体,如果你尝试创建你自己的定义,你会得到你所描述的错误。

(作为一个方面说明,你的connect语句似乎被破坏了s/signal/Signal/


The problem is that you're trying to define a function called Signal()

Qt generates the body of the "signal" functions for you, and if you try to create your own definition, you will get the error that you're describing.

(As a side note, your connect statement appears to be broken s/signal/Signal/)

相关问答

更多
  • 这里的问题是有两个信号,名称为: QSpinBox::valueChanged(int)和QSpinBox::valueChanged(QString) 。 你需要告诉Qt你想要选择哪一个,通过将其转换为正确的类型: connect(spinbox, static_cast(&QSpinBox::valueChanged), slider, &QSlider::setValue); 我知道这很丑陋 但是没有办法解决这个问题。 今天的课程是: ...
  • Qt的许多功能(包括信号)需要使用Qt元对象编译器(MOC)预处理源。 如果您使用Qt的信号功能,您可以在Qt支持的任何编译器和平台上编译源代码。 如果你需要在Qt不支持的平台上进行编译,那么你可能会走运,或者至少在很多有趣的集成工作中(当然这对于任何库或框架都是如此)。 如果你正在寻找一个跨平台的信号实现,你也可以考虑Boost.Signals 。 Many features of Qt, including signals, require preprocessing the source using ...
  • 您可以尝试Enterprise Architect,因为它支持UML 2.1并允许创建用户定义的图表。 You can try Enterprise Architect as it supports UML 2.1 and allow to create user defined diagrams.
  • 小睡一会后,我再次修改了项目中使用的库。 问题是其中一些库处于调试模式,一些处于发布模式。 当使用信号和插槽时, moc发疯了。 在构建OSG调试库之后,我再次尝试了我的小例子并且工作了。 所以它完成了! After a little nap, I revised again the libraries used in the project. The problem was that some of those libraries were in debug mode and some in releas ...
  • connect(this, SIGNAL(paint_signal(double, double)), this, SLOT(PaintSomething(x,y))); 删除参数名称,它应该工作。 如果这个不起作用,这个将: connect(this, SIGNAL(paint_signal(double, double)), this, SLOT(PaintSomething(double, ...
  • 有一篇博客文章写了一段时间,称为调试Qt信号和插槽的20种方法 它解决了我认为问题的第一和第三。 对于#2,我认为使用或不使用信号/插槽确实有一个硬性和快速的原因,因为它们是GUI框架的一个非常核心的概念。 信号是将一个组件的知识分离到另一个组件的完美方式,允许您设计可重用的小部件,只需声明状态更改或通知。 通过发出主线程可以看到的信号,这也是从非GUI线程循环传递GUI更改的一种非常好的方法。 可能有时候您真正想要的而不是信号/槽是使用事件,例如当父窗口小部件应该成为许多子窗口小部件的事件过滤器时。 孩子 ...
  • 问题在于你试图定义一个名为Signal()的函数, Qt为你生成“信号”函数的主体,如果你尝试创建你自己的定义,你会得到你所描述的错误。 (作为一个方面说明,你的connect语句似乎被破坏了s/signal/Signal/ ) The problem is that you're trying to define a function called Signal() Qt generates the body of the "signal" functions for you, and if you tr ...
  • 在选择“转到插槽...”之前右键单击设计视图中的对话框,从对话框的信号到对话框的插槽连接,这对QMetaObject :: connectSlotsByName()不起作用,因为该方法搜索对于所有子对象,但不对于对象本身。 你想要做的是右键单击OK按钮,然后从那里选择“Go to slot ...”。 然后它将创建一个带有按钮小部件名称的插槽,并且将在运行时正确建立连接。 QtDesigner让您从Dialog中选择“Go to slot ...”是没有意义的。 你可能想为Qt的开发者提交一个bug。 Ri ...
  • 1.我写的代码有什么问题? 这一行: QObject::connect(&mySig, testSig::sig_1(int), &mySlot, testSlot::slot1(int)); 这是非法的C ++。 如果要使用新的信号/槽语法,则需要获取信号和槽功能的指针: QObject::connect(&mySig, &testSig::sig_1, &mySlot, &testSlot::slot1); 2. Qt connect函数是否需要指向对象引用(&mySig)的指针,而不是直接指向对象 ...
  • 基于注释,在connect 之前在server_构造函数中发出(因为如果在server_指针中有有效值,则返回构造函数)。 发送自己不会排队,必须在发出要调用的插槽(或排队的呼叫)之前完成连接。 Based on comments, you emit in constructor of server_, before you do connect (because constructor has returned if you have valid value in server_ pointer). Em ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。