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 add a functional "upload picture" button?
Forum Updated to NodeBB v4.3 + New Features

How to add a functional "upload picture" button?

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 6 Posters 2.6k Views 1 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.
  • R Offline
    R Offline
    Rangerguy128
    wrote on 5 May 2024, 13:12 last edited by
    #1

    I'm trying to design a UI page through QT Designer where in the middle, a button that says "Upload Picture"is offered, where it searches through the computer files and accepts only photos (png, jpeg, etc.) Once uploaded, the picture is shown. I'm not sure how I could make it work. Should I use StackedWidgets for this? Any tips?

    J 1 Reply Last reply 5 May 2024, 13:55
    0
    • R Rangerguy128
      5 May 2024, 13:12

      I'm trying to design a UI page through QT Designer where in the middle, a button that says "Upload Picture"is offered, where it searches through the computer files and accepts only photos (png, jpeg, etc.) Once uploaded, the picture is shown. I'm not sure how I could make it work. Should I use StackedWidgets for this? Any tips?

      J Offline
      J Offline
      JonB
      wrote on 5 May 2024, 13:55 last edited by JonB 5 May 2024, 13:58
      #2

      @Rangerguy128
      Not QStackedWidget, that allows you to show just one of a number of alternative widgets, I don't see a demand for that here. (Unless you want to replace the button with the image, but then the user can't do it again; but if that is the case/question then yes to QStackedWidget.) Just create a QLabel (simplest widget which can show an image) and a QPushButton or QToolButton. Lay them out as you please. On clicking the button display a QFileDialog, QFileDialog::getOpenFileName() should do you. You can set its filter so as to only display desired file types. When user picks file read it in and set label's pixmap to it.

      R 1 Reply Last reply 6 May 2024, 04:27
      2
      • J JonB
        5 May 2024, 13:55

        @Rangerguy128
        Not QStackedWidget, that allows you to show just one of a number of alternative widgets, I don't see a demand for that here. (Unless you want to replace the button with the image, but then the user can't do it again; but if that is the case/question then yes to QStackedWidget.) Just create a QLabel (simplest widget which can show an image) and a QPushButton or QToolButton. Lay them out as you please. On clicking the button display a QFileDialog, QFileDialog::getOpenFileName() should do you. You can set its filter so as to only display desired file types. When user picks file read it in and set label's pixmap to it.

        R Offline
        R Offline
        Rangerguy128
        wrote on 6 May 2024, 04:27 last edited by
        #3

        @JonB My idea is that in the middle of a widget, there should be abutton and when it opens an image file, the button disappears and the image takes up the page: 91c17480-1910-45bd-aeaa-3ffab5501002-image.png

        C 1 Reply Last reply 6 May 2024, 06:07
        0
        • R Rangerguy128
          6 May 2024, 04:27

          @JonB My idea is that in the middle of a widget, there should be abutton and when it opens an image file, the button disappears and the image takes up the page: 91c17480-1910-45bd-aeaa-3ffab5501002-image.png

          C Offline
          C Offline
          ChrisW67
          wrote on 6 May 2024, 06:07 last edited by
          #4

          @Rangerguy128 If you want to replace the Upload Image button with the image then, as @JonB already confirmed, QStackedWidget is one way to go about it. However, if the user might do this more than once you need to consider how the user returns to the Upload Image button after loading an image.

          R 1 Reply Last reply 6 May 2024, 15:34
          3
          • C ChrisW67
            6 May 2024, 06:07

            @Rangerguy128 If you want to replace the Upload Image button with the image then, as @JonB already confirmed, QStackedWidget is one way to go about it. However, if the user might do this more than once you need to consider how the user returns to the Upload Image button after loading an image.

            R Offline
            R Offline
            Rangerguy128
            wrote on 6 May 2024, 15:34 last edited by
            #5

            @ChrisW67 Thanks, will keep that in mind. I've decided to use QStackedWidgets, with index 0 where the button is placed, and the location of the uploaded image on index 1. Any other way to make it work? As in, how can I make the chosen uploaded image show up in the widget? Do I have to work around it through Python? Currently using VS Code as my editor. I'm also considering on adding a button under the image in case the user wants to reupload the image if they ever chose the wrong one.

            P J J 3 Replies Last reply 6 May 2024, 17:14
            0
            • R Rangerguy128
              6 May 2024, 15:34

              @ChrisW67 Thanks, will keep that in mind. I've decided to use QStackedWidgets, with index 0 where the button is placed, and the location of the uploaded image on index 1. Any other way to make it work? As in, how can I make the chosen uploaded image show up in the widget? Do I have to work around it through Python? Currently using VS Code as my editor. I'm also considering on adding a button under the image in case the user wants to reupload the image if they ever chose the wrong one.

              P Offline
              P Offline
              Pl45m4
              wrote on 6 May 2024, 17:14 last edited by Pl45m4 5 Jun 2024, 17:16
              #6

              @Rangerguy128 said in How to add a functional "upload picture" button?:

              I've decided to use QStackedWidgets, with index 0 where the button is placed, and the location of the uploaded image on index 1

              As @JonB and @ChrisW67 have mentioned, you need to provide a way to switch back to the button page of your QStackedWidget.
              Maybe via menu bar or you add another "Back" button below the label which shows your image to flip the QStackedWidget back to the button page.
              Otherwise the user uploads one image and then gets stuck there, since you can't return from your image view page.

              Also, I see no layouts. Seems like you just placed the button in the middle of your form.
              Better: Add a layout (horizontal), put a spacer item left and right, and then place the button in the middle...


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              R 1 Reply Last reply 7 May 2024, 02:44
              1
              • P Pl45m4
                6 May 2024, 17:14

                @Rangerguy128 said in How to add a functional "upload picture" button?:

                I've decided to use QStackedWidgets, with index 0 where the button is placed, and the location of the uploaded image on index 1

                As @JonB and @ChrisW67 have mentioned, you need to provide a way to switch back to the button page of your QStackedWidget.
                Maybe via menu bar or you add another "Back" button below the label which shows your image to flip the QStackedWidget back to the button page.
                Otherwise the user uploads one image and then gets stuck there, since you can't return from your image view page.

                Also, I see no layouts. Seems like you just placed the button in the middle of your form.
                Better: Add a layout (horizontal), put a spacer item left and right, and then place the button in the middle...

                R Offline
                R Offline
                Rangerguy128
                wrote on 7 May 2024, 02:44 last edited by
                #7

                @Pl45m4 I layout both the button and the widget like you said but I couldn't place the button in the middle:
                5a0566a6-a185-4396-ae4f-11ef10d980db-image.png

                Anyway to position it in the middle? Also, for the second page (index 1), this is how I laid it out (the buttons on the left are from an older file and will be changed soon):
                b9b1033e-9244-4cf9-86ef-98e809698167-image.png

                P 1 Reply Last reply 7 May 2024, 04:36
                0
                • R Rangerguy128
                  7 May 2024, 02:44

                  @Pl45m4 I layout both the button and the widget like you said but I couldn't place the button in the middle:
                  5a0566a6-a185-4396-ae4f-11ef10d980db-image.png

                  Anyway to position it in the middle? Also, for the second page (index 1), this is how I laid it out (the buttons on the left are from an older file and will be changed soon):
                  b9b1033e-9244-4cf9-86ef-98e809698167-image.png

                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 7 May 2024, 04:36 last edited by
                  #8

                  @Rangerguy128 said in How to add a functional "upload picture" button?:

                  I layout both the button and the widget like you said but I couldn't place the button in the middle:

                  Like this for your first StackedWidget page:
                  (my plain "Form" widget is your StackedWidget "page" widget in QtDesigner)
                  ButtonSpacerLayout.png

                  @Rangerguy128 said in How to add a functional "upload picture" button?:

                  Anyway to position it in the middle? Also, for the second page (index 1)

                  Same here. If you don't need anything else showing on your StackedWidget, you can rebuild my "Form" design here for your second StackedWidget page.
                  The page itself has a vertical layout, where a horizontal layout including two spacers and the image label takes the top spot, and below that, directly added to the vertical layout, sits the button.

                  "Design" it in this order:
                  Drag one spacer, the label and the other spacer on your page widget. Arrange them and select them all 3 together. Right-click this "group" and select "lay out", -> "horizontally".
                  Next, drag your button below this layout. After that just right click anywhere on the stackedwidget page and set the vertical base layout for the page.
                  The "page" widget should look like mine now.

                  Actually, maybe you don't even need spacers for your image label. Then just skip the first part and just drag the label and the pushbutton on the page2 and select the vertical layout.

                  It's difficult to explain with words, but QtDesigner is very easy to handle once you know how to do these layouts (and cascade them, for example). Might be tricky at first, but actually not that hard.

                  LabelButtonLayout.png


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  R 1 Reply Last reply 8 May 2024, 03:12
                  1
                  • R Rangerguy128
                    6 May 2024, 15:34

                    @ChrisW67 Thanks, will keep that in mind. I've decided to use QStackedWidgets, with index 0 where the button is placed, and the location of the uploaded image on index 1. Any other way to make it work? As in, how can I make the chosen uploaded image show up in the widget? Do I have to work around it through Python? Currently using VS Code as my editor. I'm also considering on adding a button under the image in case the user wants to reupload the image if they ever chose the wrong one.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 7 May 2024, 05:28 last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • R Rangerguy128
                      6 May 2024, 15:34

                      @ChrisW67 Thanks, will keep that in mind. I've decided to use QStackedWidgets, with index 0 where the button is placed, and the location of the uploaded image on index 1. Any other way to make it work? As in, how can I make the chosen uploaded image show up in the widget? Do I have to work around it through Python? Currently using VS Code as my editor. I'm also considering on adding a button under the image in case the user wants to reupload the image if they ever chose the wrong one.

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 7 May 2024, 07:34 last edited by
                      #10

                      @Rangerguy128 said in How to add a functional "upload picture" button?:

                      Do I have to work around it through Python

                      the action of the button and the resulting image change/upload you will have to do with code, c++ or Python. You can't do that purely via the Designer

                      QML and QML-Designer may offer more here, but I'm not sure


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      3
                      • P Pl45m4
                        7 May 2024, 04:36

                        @Rangerguy128 said in How to add a functional "upload picture" button?:

                        I layout both the button and the widget like you said but I couldn't place the button in the middle:

                        Like this for your first StackedWidget page:
                        (my plain "Form" widget is your StackedWidget "page" widget in QtDesigner)
                        ButtonSpacerLayout.png

                        @Rangerguy128 said in How to add a functional "upload picture" button?:

                        Anyway to position it in the middle? Also, for the second page (index 1)

                        Same here. If you don't need anything else showing on your StackedWidget, you can rebuild my "Form" design here for your second StackedWidget page.
                        The page itself has a vertical layout, where a horizontal layout including two spacers and the image label takes the top spot, and below that, directly added to the vertical layout, sits the button.

                        "Design" it in this order:
                        Drag one spacer, the label and the other spacer on your page widget. Arrange them and select them all 3 together. Right-click this "group" and select "lay out", -> "horizontally".
                        Next, drag your button below this layout. After that just right click anywhere on the stackedwidget page and set the vertical base layout for the page.
                        The "page" widget should look like mine now.

                        Actually, maybe you don't even need spacers for your image label. Then just skip the first part and just drag the label and the pushbutton on the page2 and select the vertical layout.

                        It's difficult to explain with words, but QtDesigner is very easy to handle once you know how to do these layouts (and cascade them, for example). Might be tricky at first, but actually not that hard.

                        LabelButtonLayout.png

                        R Offline
                        R Offline
                        Rangerguy128
                        wrote on 8 May 2024, 03:12 last edited by
                        #11

                        @Pl45m4 Thanks for the tips. I also laid out the first page horizontally (before I had it vertically): 1b7adb1e-c2ad-4744-9071-122d9f78336a-image.png

                        As for the second page, I have it laid out horizontally, and I want the button to change back to be under the image: 55734483-8031-4b98-a07f-25155d19591f-image.png

                        J J J 3 Replies Last reply 8 May 2024, 06:56
                        0
                        • R Rangerguy128
                          8 May 2024, 03:12

                          @Pl45m4 Thanks for the tips. I also laid out the first page horizontally (before I had it vertically): 1b7adb1e-c2ad-4744-9071-122d9f78336a-image.png

                          As for the second page, I have it laid out horizontally, and I want the button to change back to be under the image: 55734483-8031-4b98-a07f-25155d19591f-image.png

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 8 May 2024, 06:56 last edited by
                          #12

                          @Rangerguy128 said in How to add a functional "upload picture" button?:

                          and I want the button to change back to be under the image

                          Wasn't the idea that after clicking the button you load the image in another page in the stacked widget and then show that page?

                          R 1 Reply Last reply 8 May 2024, 13:09
                          0
                          • R Rangerguy128
                            8 May 2024, 03:12

                            @Pl45m4 Thanks for the tips. I also laid out the first page horizontally (before I had it vertically): 1b7adb1e-c2ad-4744-9071-122d9f78336a-image.png

                            As for the second page, I have it laid out horizontally, and I want the button to change back to be under the image: 55734483-8031-4b98-a07f-25155d19591f-image.png

                            J Offline
                            J Offline
                            J.Hilk
                            Moderators
                            wrote on 8 May 2024, 07:07 last edited by
                            #13

                            @Rangerguy128 said in How to add a functional "upload picture" button?:

                            As for the second page, I have it laid out horizontally, and I want the button to change back to be under the image:

                            stacking stuff on top of each other using layouts is not really a thing. At least not in designer. You can get around it in code and using Gridlayout. Then you can assign multiple widgets to a cell to stack them. But thats a workaround I would not necessarily recommend.


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            0
                            • R Rangerguy128
                              8 May 2024, 03:12

                              @Pl45m4 Thanks for the tips. I also laid out the first page horizontally (before I had it vertically): 1b7adb1e-c2ad-4744-9071-122d9f78336a-image.png

                              As for the second page, I have it laid out horizontally, and I want the button to change back to be under the image: 55734483-8031-4b98-a07f-25155d19591f-image.png

                              J Offline
                              J Offline
                              JonB
                              wrote on 8 May 2024, 07:50 last edited by
                              #14

                              @Rangerguy128
                              It's up to you, but I don't really understand what you are trying to achieve or why. As I wrote originally

                              Just create a QLabel (simplest widget which can show an image) and a QPushButton or QToolButton. Lay them out as you please.

                              If you replace (e.g. with a QStackedWidget) the "Upload Image" button/link by the image then the user does not have a chance to pick a different image once they have picked one. Assuming you intend them to be able to replace whenever they wish, why don't you just make a widget with space for the image and the button below it (or wherever)?

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                Pl45m4
                                wrote on 8 May 2024, 12:09 last edited by Pl45m4 5 Aug 2024, 12:11
                                #15

                                @J-Hilk @jsulm @JonB
                                If I understand @Rangerguy128 last post right, this was just a notice that he exchanged horizontal and vertical layout from my recommendation. I don't know if there's an issue or question left?
                                Does it look like you expect now @Rangerguy128 ?


                                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                ~E. W. Dijkstra

                                R 1 Reply Last reply 8 May 2024, 13:17
                                1
                                • J jsulm
                                  8 May 2024, 06:56

                                  @Rangerguy128 said in How to add a functional "upload picture" button?:

                                  and I want the button to change back to be under the image

                                  Wasn't the idea that after clicking the button you load the image in another page in the stacked widget and then show that page?

                                  R Offline
                                  R Offline
                                  Rangerguy128
                                  wrote on 8 May 2024, 13:09 last edited by
                                  #16

                                  @jsulm My idea is that once the user uploads an image, the page will change to the one with the image and a button below that allows the user to go back. Meanwhile, the button to upload the image will be in the center of the stackedwidget in the first page. Is that probable or maybe somewhat messy or unnecesary?

                                  1 Reply Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    JonB
                                    wrote on 8 May 2024, 13:14 last edited by
                                    #17

                                    You can do that if you wish. Seems unnecessarily complex to me compared to just doing what I said. Up to you.

                                    1 Reply Last reply
                                    0
                                    • P Pl45m4
                                      8 May 2024, 12:09

                                      @J-Hilk @jsulm @JonB
                                      If I understand @Rangerguy128 last post right, this was just a notice that he exchanged horizontal and vertical layout from my recommendation. I don't know if there's an issue or question left?
                                      Does it look like you expect now @Rangerguy128 ?

                                      R Offline
                                      R Offline
                                      Rangerguy128
                                      wrote on 8 May 2024, 13:17 last edited by
                                      #18

                                      @Pl45m4 For the first page, it worked fine enough. For the second one, which is what you used as an example, I tried using horizontal spacers between the image label but it seems that there's an issue: 0ab4b796-e7ea-4066-b95c-70b6af90b5a3-image.png
                                      Anyway to fix this? Is it due to the image label's size? As a reminder, I have it laid out vertically here.

                                      P 1 Reply Last reply 8 May 2024, 13:20
                                      0
                                      • R Rangerguy128
                                        8 May 2024, 13:17

                                        @Pl45m4 For the first page, it worked fine enough. For the second one, which is what you used as an example, I tried using horizontal spacers between the image label but it seems that there's an issue: 0ab4b796-e7ea-4066-b95c-70b6af90b5a3-image.png
                                        Anyway to fix this? Is it due to the image label's size? As a reminder, I have it laid out vertically here.

                                        P Offline
                                        P Offline
                                        Pl45m4
                                        wrote on 8 May 2024, 13:20 last edited by Pl45m4 5 Aug 2024, 13:22
                                        #19

                                        @Rangerguy128

                                        You need to use vertical spacers then. To push up/down instead of left/right.
                                        Makes sense? :D

                                        edit:
                                        Or check if it still looks ok for you when removing them completely. So you just have the vertical layout with the image abovr the button left.


                                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                        ~E. W. Dijkstra

                                        R 1 Reply Last reply 8 May 2024, 20:55
                                        1
                                        • Pl45m4P Pl45m4
                                          8 May 2024, 13:20

                                          @Rangerguy128

                                          You need to use vertical spacers then. To push up/down instead of left/right.
                                          Makes sense? :D

                                          edit:
                                          Or check if it still looks ok for you when removing them completely. So you just have the vertical layout with the image abovr the button left.

                                          R Offline
                                          R Offline
                                          Rangerguy128
                                          wrote on 8 May 2024, 20:55 last edited by
                                          #20

                                          @Pl45m4 Like this? 5796f4ff-967d-4e96-81b7-1973ef45cf48-image.png

                                          Is the image positioned well enough? Should the text be centered or is it not necessary? The uploaded image should be the only thing to be shown in the label.

                                          Pl45m4P 1 Reply Last reply 8 May 2024, 23:29
                                          0

                                          1/21

                                          5 May 2024, 13:12

                                          • Login

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