JavaFX:ImageView只显示一次Image(JavaFX: ImageView shows Image only once)
在我的JavaFX应用程序中,我使用TreeView。 单个TreeItems包含一个图像。 不幸的是,图像只显示一次。
加载图像的代码如下所示。 每次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);
在我的
TreeCell
的updateItem
方法中,上面返回的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.
The code for loading the image looks like the following. It is called every time the TreeView changes. The
Image
s are cached in aMap
("icons"). Thus, a newImageView
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 myTreeCell
s, theImageView
returned above is stored into a field calledicon
. The field is used in the following code, also fromupdateItem
: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
最满意答案
问题在于你试图定义一个名为
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 ...
-
信号和插槽QT(Signals and slots QT)[2022-10-19]
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 ...
-
使用Qt Creator创建信号和插槽时出错(Getting errors when using Qt Creator to create signals and slots)[2022-05-10]
在选择“转到插槽...”之前右键单击设计视图中的对话框,从对话框的信号到对话框的插槽连接,这对QMetaObject :: connectSlotsByName()不起作用,因为该方法搜索对于所有子对象,但不对于对象本身。 你想要做的是右键单击OK按钮,然后从那里选择“Go to slot ...”。 然后它将创建一个带有按钮小部件名称的插槽,并且将在运行时正确建立连接。 QtDesigner让您从Dialog中选择“Go to slot ...”是没有意义的。 你可能想为Qt的开发者提交一个bug。 Ri ... -
Qt插槽和信号语法(Qt Slots and Signals Syntax)[2022-03-26]
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)的指针,而不是直接指向对象 ... -
Qt信号和插槽有问题(Qt signals and slots trouble)[2022-04-02]
基于注释,在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 ...