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
Forum Updated to NodeBB v4.3 + New Features

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.5k 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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on 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
    1
    • M Offline
      M Offline
      mad-hatter
      wrote on 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
      • dheerendraD dheerendra

        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 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

        JonBJ 1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on 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
          0
          • dheerendraD dheerendra

            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 last edited by amarism
            #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

              @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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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
              0
              • JonBJ JonB

                @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 last edited by
                #8

                @JonB But how to display the file image on button

                J.HilkJ 1 Reply Last reply
                0
                • A amarism

                  @JonB But how to display the file image on button

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #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
                  0
                  • J.HilkJ J.Hilk

                    @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 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.HilkJ JonBJ 2 Replies Last reply
                    0
                    • A amarism

                      @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.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on 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

                        @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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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 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.

                          JonBJ 1 Reply Last reply
                          0
                          • A amarism

                            @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.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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
                            1
                            • JonBJ JonB

                              @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 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"));)

                              JonBJ 1 Reply Last reply
                              0
                              • A amarism

                                @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"));)

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #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
                                1
                                • JonBJ JonB

                                  @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 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

                                  • Login

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