Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to assign getOpenFileName to a global variable

How to assign getOpenFileName to a global variable

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 567 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MAX001
    wrote on last edited by MAX001
    #1

    In frame i have such code

        image_import_btn.clicked.connect(partial(display_image, grid_image_detect, image_detect_btn))
        #image_detect_btn.clicked.connect(partial(main, image_file, grid_image_detect))   ( wanna use before but image path doesn't have any parameter inside)
    

    And such function

    def display_image(grid_image_detect, image_detect_btn):
        global image_file
        load_image = QLabel()
        image_file = ""
        image_file = QFileDialog.getOpenFileName(load_image, 'Open file', 'Images', "Image Files (*.jpg *.gif *.bmp *.png)")
        loaded_image = QPixmap(image_file[0])
        load_image.setPixmap(loaded_image.scaled(800, 500, Qt.AspectRatioMode.KeepAspectRatio))
        grid_image_detect.addWidget(load_image, 0, 1)
        image_detect_btn.clicked.connect(partial(main, image_file))    #That event stores previos images how i can fix it 
        # I open first one it detects when import second it detects first after second. How to clear repeated calls?
        return grid_image_detect
    

    And the result of detecting image I wanna show in grid how i can implement it ?
    I had an idea to return detected image as a variable from OpenCV on clicked event button but don't know how to pass variables in OpenCV function and return right image variable without repeated calls.

    JonBJ 1 Reply Last reply
    0
    • M MAX001

      In frame i have such code

          image_import_btn.clicked.connect(partial(display_image, grid_image_detect, image_detect_btn))
          #image_detect_btn.clicked.connect(partial(main, image_file, grid_image_detect))   ( wanna use before but image path doesn't have any parameter inside)
      

      And such function

      def display_image(grid_image_detect, image_detect_btn):
          global image_file
          load_image = QLabel()
          image_file = ""
          image_file = QFileDialog.getOpenFileName(load_image, 'Open file', 'Images', "Image Files (*.jpg *.gif *.bmp *.png)")
          loaded_image = QPixmap(image_file[0])
          load_image.setPixmap(loaded_image.scaled(800, 500, Qt.AspectRatioMode.KeepAspectRatio))
          grid_image_detect.addWidget(load_image, 0, 1)
          image_detect_btn.clicked.connect(partial(main, image_file))    #That event stores previos images how i can fix it 
          # I open first one it detects when import second it detects first after second. How to clear repeated calls?
          return grid_image_detect
      

      And the result of detecting image I wanna show in grid how i can implement it ?
      I had an idea to return detected image as a variable from OpenCV on clicked event button but don't know how to pass variables in OpenCV function and return right image variable without repeated calls.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @MAX001
      I don't know about any OpenCV stuff, but Qt slots cannot return any value (it is simply discarded). You can either do whatever you need called from the slot, or possibly you could emit your own signal from the slot so that the outside world can in turn place its own slot on that signal and do the work and such a slot.

      M 1 Reply Last reply
      1
      • JonBJ JonB

        @MAX001
        I don't know about any OpenCV stuff, but Qt slots cannot return any value (it is simply discarded). You can either do whatever you need called from the slot, or possibly you could emit your own signal from the slot so that the outside world can in turn place its own slot on that signal and do the work and such a slot.

        M Offline
        M Offline
        MAX001
        wrote on last edited by
        #3

        @JonB Is it an idea to work with QThread Slots Emit function? And this needs to be added to the display_image() function itself?
        I managed to display the image in the grid, but the processing of the recognition algorithm is still repeated.
        And I don't really understand why image_file is always empty when I want to call outside of the function display_image()

        JonBJ 1 Reply Last reply
        0
        • M MAX001

          @JonB Is it an idea to work with QThread Slots Emit function? And this needs to be added to the display_image() function itself?
          I managed to display the image in the grid, but the processing of the recognition algorithm is still repeated.
          And I don't really understand why image_file is always empty when I want to call outside of the function display_image()

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @MAX001
          All I know is don't use threads unless you really need to. They often cause problems. I do not know whether you need to put your Open CV in a thread though.

          image_file should have a value after display_image has been called and the getOpenFileName() been executed, but not before. Don't forget you need to use global image_file again elsewhere wherever you want to access it?

          M 1 Reply Last reply
          1
          • JonBJ JonB

            @MAX001
            All I know is don't use threads unless you really need to. They often cause problems. I do not know whether you need to put your Open CV in a thread though.

            image_file should have a value after display_image has been called and the getOpenFileName() been executed, but not before. Don't forget you need to use global image_file again elsewhere wherever you want to access it?

            M Offline
            M Offline
            MAX001
            wrote on last edited by
            #5

            @JonB When I make a display_image function call, I declare a global image_file variable, after getOpenFileName() I return a string with two parameters, I take the first one which is the path and if I call image_detect_btn.clicked.connect(partial(main, image_file, grid_image_detect)) in a function, it works but repeats with the previous images. When I make a call outside the function, IndexError occurs: string index out of range
            How it works

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              It looks like you are over-engineering things a bit.

              What is your exact workflow ?

              • click button
              • select file
              • load image
              • show image
              • process image with OpenCV
              • show processed image

              ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                It looks like you are over-engineering things a bit.

                What is your exact workflow ?

                • click button
                • select file
                • load image
                • show image
                • process image with OpenCV
                • show processed image

                ?

                M Offline
                M Offline
                MAX001
                wrote on last edited by
                #7

                @SGaist Idea, I have a frame in which on the right side just a picture will open on the left side a picture with recognized objects.
                Then I can import another picture, it will open on the right side, then I press the Detect button and the recognized picture will appear on the left side.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Then you can simply store the image path as class member variable and reuse it in the slot that will do the OpenCV processing.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved