1

I am having trouble displaying a simple image into my application. Here's the main.py:

import sys
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QObject, QUrl


if __name__ == '__main__':
    sys.argv += ['--style', 'material']
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine('basic.qml')
    sys.exit(app.exec_())  

And the basic.qml:

import QtQuick 2.0
import QtQuick.Controls 2.1
import QtQuick.Controls.Material 2.1

ApplicationWindow {
    visible: true
    width: 200
    height: 400
    title: qsTr("Hello World")
    Material.theme: Material.Light
    Material.accent: Material.Orange
    Column {
        anchors.centerIn: parent
        Button {
            width: 200; height:50;
            font.capitalization: Font.MixedCase
            text: qsTr("Button Name")
            objectName: "button_obj_name"
            highlighted: true
            Material.background: Material.Orange

        }
        Image{
                width: 100; height: 100
                fillMode: Image.PreserveAspectFit
                source: "logo_name.jpg"
            }
    }
}

In the application window it seems that space is allocated but nothing is displaying. [logo_name.jpg is located in the same folder where basic.qml and main.py are in ]

HaR
  • 987
  • 7
  • 23
  • Can you try adding `sourceSize.width: 100; sourceSize.height: 100;` in the image? `Image` has this weird `sourceSize` property... – Ross Rogers Feb 05 '18 at 16:28
  • to me it works correctly, you could explain how you execute your application. – eyllanesc Feb 05 '18 at 16:42
  • 1
    As a sanity check can you please post the image you're trying to display & image/listing of folder contents? – Jason R. Mick Feb 05 '18 at 21:31
  • @eyllanesc I'm running it directly from IDLE (F5) and I tried creating an executable with cx_freeze and the image is still not showing. – HaR Feb 06 '18 at 10:38
  • 1
    @Jason R. Mick I think you might be right. I've tried with a random logo from the internet and it displayed correctly. http://brandmark.io/logo-rank/random/pepsi.png – HaR Feb 06 '18 at 10:46

3 Answers3

0

Your code looks right. I was having a similar problem on my windows box. Code that worked on the raspberry did not work on the windows machine. Removing and reinstalling PyQt5 go it working again.

pip3 uninstall pyqt5 
0

It had something to do with the image I was trying to display(as @Jason R. Mick mentioned),I believe that the image I was trying to use was converted by changing it's extension name.

As a sanity check can you please post the image you're trying to display & image/listing of folder contents? – Jason R. Mick

HaR
  • 987
  • 7
  • 23
0

You should better check current path. You can print out the current path with sys.argv[0]. After getting the path, copy your image file to that path. Then, run again.

I also think this is not a problem of source code, but development environment.

uthline
  • 102
  • 7