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.3k 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.
  • 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

                      17/17

                      7 Jun 2018, 14:03

                      • Login

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