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. Giving file location from parent of the parent folder(Dynamically) in QT

Giving file location from parent of the parent folder(Dynamically) in QT

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 3.2k Views
  • 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.
  • A Offline
    A Offline
    amarism
    wrote on 7 Jun 2018, 10:02 last edited by
    #1

    I need to read the file location from parent folder but I don't know how.
    My executed file is in
    C:\test\project\icon\icon1.png

    Next time the same file placed in another folder
    D:\icon\icon1.png

    I want to do this dynamically. So config of the icon1.png should give a path to test then into project and icon. Because this program can be located anywhere and I don't want to give a path starting from C:\

    Any help would be appreciated. I'm using QT 5.10.1. Thanks

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 7 Jun 2018, 10:26 last edited by
      #2

      Don't use hard-coded path. Just check if the QCoreApplication::applicationDirPath() function help you to place the images and access the relative path.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      A 1 Reply Last reply 7 Jun 2018, 11:10
      1
      • M Offline
        M Offline
        mad-hatter
        wrote on 7 Jun 2018, 10:41 last edited by
        #3

        Have a look at QDir header.

        #include <QDir>

        QDir QDir::current()
        Returns the application's current directory.

        1 Reply Last reply
        0
        • D dheerendra
          7 Jun 2018, 10:26

          Don't use hard-coded path. Just check if the QCoreApplication::applicationDirPath() function help you to place the images and access the relative path.

          A Offline
          A Offline
          amarism
          wrote on 7 Jun 2018, 11:10 last edited by
          #4

          @dheerendra using this code to get the file path:- QFile File(QCoreApplication::applicationDirPath() + "/LineMeas.jpg");
          but i am not able to display the image inside the button

          J 1 Reply Last reply 7 Jun 2018, 11:52
          0
          • D Offline
            D Offline
            dheerendra
            Qt Champions 2022
            wrote on 7 Jun 2018, 11:17 last edited by
            #5

            Print the QCoreApplication::applicationDirPath().
            Confirm whether LineMeas.jpg is present in the above directory. If it exist, there may be a problem while setting it for button. Please check

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            A 1 Reply Last reply 7 Jun 2018, 11:21
            0
            • D dheerendra
              7 Jun 2018, 11:17

              Print the QCoreApplication::applicationDirPath().
              Confirm whether LineMeas.jpg is present in the above directory. If it exist, there may be a problem while setting it for button. Please check

              A Offline
              A Offline
              amarism
              wrote on 7 Jun 2018, 11:21 last edited by amarism 6 Jul 2018, 11:24
              #6

              @dheerendra LineMeas.jpg is present in the directory (C:\test\project\icon\LineMeas.jpg).
              and i am try to use the file to display image on button :-(ui->pushButton->setWindowFilePath(File);)

              1 Reply Last reply
              0
              • A amarism
                7 Jun 2018, 11:10

                @dheerendra using this code to get the file path:- QFile File(QCoreApplication::applicationDirPath() + "/LineMeas.jpg");
                but i am not able to display the image inside the button

                J Offline
                J Offline
                JonB
                wrote on 7 Jun 2018, 11:52 last edited by
                #7

                @amarism said in Giving file location from parent of the parent folder(Dynamically) in QT:

                @dheerendra using this code to get the file path:- QFile File(QCoreApplication::applicationDirPath() + "/LineMeas.jpg");

                So since I assume your application path is not in the icon sub-folder, but is actually C:\test\project, then obviously you need to specify:

                QFile File(QCoreApplication::applicationDirPath() + "/icon/LineMeas.jpg");
                

                or whatever to address the correct desired path relative to QCoreApplication::applicationDirPath() .

                A 1 Reply Last reply 7 Jun 2018, 11:58
                0
                • J JonB
                  7 Jun 2018, 11:52

                  @amarism said in Giving file location from parent of the parent folder(Dynamically) in QT:

                  @dheerendra using this code to get the file path:- QFile File(QCoreApplication::applicationDirPath() + "/LineMeas.jpg");

                  So since I assume your application path is not in the icon sub-folder, but is actually C:\test\project, then obviously you need to specify:

                  QFile File(QCoreApplication::applicationDirPath() + "/icon/LineMeas.jpg");
                  

                  or whatever to address the correct desired path relative to QCoreApplication::applicationDirPath() .

                  A Offline
                  A Offline
                  amarism
                  wrote on 7 Jun 2018, 11:58 last edited by
                  #8

                  @JonB But how to display the file image on button

                  J 1 Reply Last reply 7 Jun 2018, 12:07
                  0
                  • A amarism
                    7 Jun 2018, 11:58

                    @JonB But how to display the file image on button

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 7 Jun 2018, 12:07 last edited by J.Hilk 6 Jul 2018, 12:07
                    #9

                    @amarism

                    A QPushButton is probably not the correct widget to use, to display a dynamic icon.

                    However there are 2 ways I can think of

                    QPushButton::setIcon(QIcon(dynamicPath));
                    
                    and
                    
                    QString btnStyle = "border-image:url(%1);"
                    btnStyle.arg(dynamicPath);
                    
                    QPushButton::setStyleSheet(btnStyle);
                    

                    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.

                    A 1 Reply Last reply 7 Jun 2018, 12:13
                    0
                    • J J.Hilk
                      7 Jun 2018, 12:07

                      @amarism

                      A QPushButton is probably not the correct widget to use, to display a dynamic icon.

                      However there are 2 ways I can think of

                      QPushButton::setIcon(QIcon(dynamicPath));
                      
                      and
                      
                      QString btnStyle = "border-image:url(%1);"
                      btnStyle.arg(dynamicPath);
                      
                      QPushButton::setStyleSheet(btnStyle);
                      
                      A Offline
                      A Offline
                      amarism
                      wrote on 7 Jun 2018, 12:13 last edited by
                      #10

                      @J.Hilk said in Giving file location from parent of the parent folder(Dynamically) in QT:

                      QPushButton::setIcon(QIcon(dynamicPath));

                      how to get the dynamic path

                      J J 2 Replies Last reply 7 Jun 2018, 12:16
                      0
                      • A amarism
                        7 Jun 2018, 12:13

                        @J.Hilk said in Giving file location from parent of the parent folder(Dynamically) in QT:

                        QPushButton::setIcon(QIcon(dynamicPath));

                        how to get the dynamic path

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 7 Jun 2018, 12:16 last edited by
                        #11

                        @amarism said in Giving file location from parent of the parent folder(Dynamically) in QT:

                        @J.Hilk said in Giving file location from parent of the parent folder(Dynamically) in QT:

                        QPushButton::setIcon(QIcon(dynamicPath));

                        how to get the dynamic path

                        ?

                        You should read the other posts more carefully

                        with for example

                        QCoreApplication::applicationDirPath()

                        or

                        QStandardPaths


                        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
                        1
                        • A amarism
                          7 Jun 2018, 12:13

                          @J.Hilk said in Giving file location from parent of the parent folder(Dynamically) in QT:

                          QPushButton::setIcon(QIcon(dynamicPath));

                          how to get the dynamic path

                          J Offline
                          J Offline
                          JonB
                          wrote on 7 Jun 2018, 12:19 last edited by
                          #12

                          @amarism said in Giving file location from parent of the parent folder(Dynamically) in QT:

                          how to get the dynamic path

                          We have explained this repeatedly in the examples above.

                          If your dynamic path means "it's inside the Qt application's directory, in an icon sub-folder", try either plain "icon/LineMeas.jpg" or QCoreApplication::applicationDirPath() + "/icon/LineMeas.jpg".

                          If you mean somewhere else, you'd better explain where....

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            amarism
                            wrote on 7 Jun 2018, 12:34 last edited by
                            #13

                            @JonB said in Giving file location from parent of the parent folder(Dynamically) in QT:

                            icon/LineMeas.jpg

                            My icon folder place outside the QT application.

                            J 1 Reply Last reply 7 Jun 2018, 12:50
                            0
                            • A amarism
                              7 Jun 2018, 12:34

                              @JonB said in Giving file location from parent of the parent folder(Dynamically) in QT:

                              icon/LineMeas.jpg

                              My icon folder place outside the QT application.

                              J Offline
                              J Offline
                              JonB
                              wrote on 7 Jun 2018, 12:50 last edited by
                              #14

                              @amarism

                              My icon folder place outside the QT application.

                              None of the posters here would have guessed that!

                              This does not seem like a good start for you! You sure you wouldn't be better rearranging so that it is inside the application? It would make things a lot easier....

                              OK, so if it is to be external: neither we nor your Qt application can possibly guess where the file(s) might be, can we? So, how do you/your users/your application actually know where this file is?? You need to specify this, else you'll never get anywhere! So what are your rules for where this file is to be found?

                              A 1 Reply Last reply 7 Jun 2018, 13:05
                              1
                              • J JonB
                                7 Jun 2018, 12:50

                                @amarism

                                My icon folder place outside the QT application.

                                None of the posters here would have guessed that!

                                This does not seem like a good start for you! You sure you wouldn't be better rearranging so that it is inside the application? It would make things a lot easier....

                                OK, so if it is to be external: neither we nor your Qt application can possibly guess where the file(s) might be, can we? So, how do you/your users/your application actually know where this file is?? You need to specify this, else you'll never get anywhere! So what are your rules for where this file is to be found?

                                A Offline
                                A Offline
                                amarism
                                wrote on 7 Jun 2018, 13:05 last edited by
                                #15

                                @JonB, For example, I am placing icon folder inside the QT Application but still this one not working (ui->pushButton->setIcon(QIcon(QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg"));)

                                J 1 Reply Last reply 7 Jun 2018, 13:13
                                0
                                • A amarism
                                  7 Jun 2018, 13:05

                                  @JonB, For example, I am placing icon folder inside the QT Application but still this one not working (ui->pushButton->setIcon(QIcon(QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg"));)

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 7 Jun 2018, 13:13 last edited by JonB 6 Jul 2018, 14:15
                                  #16

                                  @amarism

                                  (ui->pushButton->setIcon(QIcon(QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg"));)

                                  But earlier on you showed the sub-folder was named icon, and now you show you're using name icons in the path... ???

                                  I'm sorry, but no-one will want to help if you cannot give a consistent, literal code example with correct paths which actually matches the paths on your disk. We don't want to spend time helping if this proves to be the problem area.

                                  Meanwhile... you're doing too much in a single line of code to spot where the problem is. Break it down into 3 steps:

                                  1. Use QFileInfo on QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg" or whatever, or open it for read. Until that is finding the file correctly.

                                  2. Then verify that QIcon(QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg")) is returning an icon, not 0 or error or whatever.

                                  3. Then pass it to ui->pushButton->setIcon().

                                  One test one at a time....

                                  A 1 Reply Last reply 7 Jun 2018, 14:03
                                  1
                                  • J JonB
                                    7 Jun 2018, 13:13

                                    @amarism

                                    (ui->pushButton->setIcon(QIcon(QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg"));)

                                    But earlier on you showed the sub-folder was named icon, and now you show you're using name icons in the path... ???

                                    I'm sorry, but no-one will want to help if you cannot give a consistent, literal code example with correct paths which actually matches the paths on your disk. We don't want to spend time helping if this proves to be the problem area.

                                    Meanwhile... you're doing too much in a single line of code to spot where the problem is. Break it down into 3 steps:

                                    1. Use QFileInfo on QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg" or whatever, or open it for read. Until that is finding the file correctly.

                                    2. Then verify that QIcon(QCoreApplication::applicationDirPath() + "/icons/LineMeas.jpg")) is returning an icon, not 0 or error or whatever.

                                    3. Then pass it to ui->pushButton->setIcon().

                                    One test one at a time....

                                    A Offline
                                    A Offline
                                    amarism
                                    wrote on 7 Jun 2018, 14:03 last edited by
                                    #17

                                    @JonB said in Giving file location from parent of the parent folder(Dynamically) in QT:

                                    ui->pushButton->setIcon()

                                    my bad a small change in the code as of your prediction it is icons only thanks for correcting.

                                    1 Reply Last reply
                                    0

                                    1/17

                                    7 Jun 2018, 10:02

                                    • Login

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