-1

My problem is that i didnt want to save an image locally to display it in my ui program so I converted the image to bytecode to then be stored in the code itself and display from there itself. However when I do so, It doesn't show it on the ui when I try to display it using .raise_() but displays when using .show(). I don't want to use .show() as it launches a new window to display just the image. .raise_() works when loading the image when stored locally in a directory.

What originally worked, I load the image file from the path and it displays perfectly where it should

        self.label_3 = QtWidgets.QLabel(parent=self.tab)
        self.label_3.setGeometry(QtCore.QRect(-80, -30, 291, 221))
        self.label_3.setText("")
        self.label_3.setPixmap(QtGui.QPixmap("icon.png"))
        self.label_3.setScaledContents(True)
        self.label_3.setObjectName("label_3")

What I wanted to switch to so that I didnt have to save the image locally

        self.base64_icon= '''
        *Image converted to base64 code*
        '''
        self.label_3 = QLabel()
        self.label_3.setPixmap(QPixmap.fromImage(QImage.fromData(base64.b64decode(self.base64_icon))))
      

When I use the above code and then display the label using .show(), it shows it normally in a new screen however when I use .raise_(), so that I can display it where I want as .show() just launches a new window to display the image, it doesn't display the image and that area is just blank.

Here is the link to the full code without the stylesheet and some irrelevant things removed: Link I hope it helps you help me

the way i want it to work but with .raise_() instead of .show():

import sys
import base64
from PyQt6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PyQt6.QtGui import QPixmap, QImage
from PyQt6.QtCore import Qt

if __name__ == "__main__":
    base64_image = ''' base64 encoded image '''
    app = QApplication(sys.argv)
    label = QLabel()
    image = QImage.fromData(base64.b64decode(base64_image))
    pixmap = QPixmap.fromImage(image)
    label.setPixmap(pixmap)
    layout = QVBoxLayout()
    layout.addWidget(label)
    window = QWidget()
    window.setLayout(layout)
    window.setWindowTitle("Image Display")
    window.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint)
    window.resize(pixmap.width(), pixmap.height())
    window.show()
    sys.exit(app.exec())
tirthjain
  • 1
  • 2
  • If the label already exists, you must not recreate it. Otherwise, you must set a proper parent as argument for the QLabel constructor, if you want to show it within an existing window/widget, which is exactly what that `parent=self.tab` of the first code does. – musicamante Jun 21 '23 at 19:30
  • @musicamante, I'm not able to completely understand what you said, I'm quite new to pyqt and I had made the ui using qt designer so would it be possible if you can explain by writing the right line of code, and pointing out the one I did wrong? – tirthjain Jun 22 '23 at 01:57
  • Unfortunately, you didn't provide a valid [mre], so there is no proper context to help you with, we could just make assumptions (and that's not the purpose of StackOverflow answers). Note that if you're editing the pyuic generated file, that's considered a bad practice (there should be a warning at the top of that file that clearly says that you should **not** modify them unless you really know what you're doing), and you should follow the official guidelines about [using Designer](//www.riverbankcomputing.com/static/Docs/PyQt6/designer.html) instead. – musicamante Jun 22 '23 at 02:01
  • Sorry but questions must always be self-contained and not rely on external services, especially regarding code. Also, ensure that you provide a valid MRE as requested: it must be **both** minimal *and* reproducible, that is some code we can easily copy, paste and run without any substantial modifications. – musicamante Jun 24 '23 at 17:47
  • Is the new addition to the question sufficient? @musicamante – tirthjain Jun 24 '23 at 18:09
  • Sorry, but, no. You're just showing one window that contains a single label, and that is completely unrelated to the problem you're referring to (the label showing in a *separate* window). Please don't rush things, questions require time in order to be well written (and, therefore, properly answered). Create an example that *specifically* reproduces your issue, as the new addition does *not*. – musicamante Jun 24 '23 at 18:15
  • well idk what the problem is, the only thing i want to know is how do i display an image, without locally saving the file, and using .raise_() to display it instead of .show(). I can using base64 code to encode the image or directly load raw image from a url. The new code i added does just that standalone by using a base64 converted image – tirthjain Jun 24 '23 at 18:27
  • That's exactly the point: in order to help you, we need the code that does **NOT** work for you (the one that *reproduces* the problem!), so that we can tell you what's wrong with it. – musicamante Jun 24 '23 at 19:02
  • And I've already told you that StackOverflow does *not* work like that. The MRE **must** be included in the question, and it **must** be *both* minimal *and* reproducible. So, you need to create a code that reproduces your issue, make efforts in making it as short as possible but still runnable, and put *that* code into the question. – musicamante Jun 25 '23 at 13:54
  • someone on discord helped me and I found the answer so I'm posting the right code here, thanks for your help anyway! – tirthjain Jun 27 '23 at 07:36
  • You don't seem to understand. This question, as it is, is completely useless to anybody, not only those who may be able to help you, but even those who may face the same problem, and the solution you posted is also completely pointless, because without a proper question and a lack of context, nobody would be able to see how that would help them. I'm glad you found a solution, but, for future reference, you ***must*** provide what's been asked: a valid example code *in the question body*, that reproduces *the problem*, and with the *shortest* possible code. – musicamante Jun 27 '23 at 08:59
  • ok thanks ill make sure i do that in the future! – tirthjain Jun 30 '23 at 08:26

1 Answers1

-1

I found the right answer:

 self.base64_icon= '''
    *base64 converted image here*
    '''
    self.pixmap = QtGui.QPixmap()
    self.pixmap.loadFromData(base64.b64decode(self.base64_icon))
    self.label_3.setPixmap(self.pixmap)

and then I can use .show() or .raise_() doesn't matter it works as normal

Thanks for your help!

tirthjain
  • 1
  • 2