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. Check file is open or not
Qt 6.11 is out! See what's new in the release blog

Check file is open or not

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 7 Posters 12.3k Views 3 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.
  • G Gaurav Badgujar

    Hi,

    I Opened some file from local machine and want to check that file is open or not.
    How i can achieve this with coding.

    Regards,
    Gaurav Badgujar.

    K Offline
    K Offline
    koahnig
    wrote on last edited by
    #2

    @Gaurav-Badgujar

    There are a couple of different ways to open a file. Therefore, it is helpful which you are using?

    For QFile it is isOpen()

    Vote the answer(s) that helped you to solve your issue(s)

    G 1 Reply Last reply
    2
    • K koahnig

      @Gaurav-Badgujar

      There are a couple of different ways to open a file. Therefore, it is helpful which you are using?

      For QFile it is isOpen()

      G Offline
      G Offline
      Gaurav Badgujar
      wrote on last edited by
      #3

      @koahnig i use this but its notworking.
      give me some another solution.

      J.HilkJ jsulmJ 2 Replies Last reply
      0
      • G Gaurav Badgujar

        @koahnig i use this but its notworking.
        give me some another solution.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #4

        @Gaurav-Badgujar
        try

        #include<QFile>
        #include<QDebug>
        
        QFile file(pathToFile);
        if(!file.open(QIODevice::readonly){
            qDebug() << "File not open" << file.error();
        }else{
           qDebug() << "File is open";
        }
        

        that should tell you why Qfile could not open the file.


        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.

        G 1 Reply Last reply
        1
        • G Gaurav Badgujar

          @koahnig i use this but its notworking.
          give me some another solution.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @Gaurav-Badgujar What is not working? QFile is the most simple way in Qt to open a file. Just saying "its not working" is not very helpful.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #6

            @Gaurav-Badgujar
            can u post the code which u have tried.

            what is the return value u are getting?.

            Thanks,

            Pradeep Kumar
            Qt,QML Developer

            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @Gaurav-Badgujar
              try

              #include<QFile>
              #include<QDebug>
              
              QFile file(pathToFile);
              if(!file.open(QIODevice::readonly){
                  qDebug() << "File not open" << file.error();
              }else{
                 qDebug() << "File is open";
              }
              

              that should tell you why Qfile could not open the file.

              G Offline
              G Offline
              Gaurav Badgujar
              wrote on last edited by
              #7

              @J.Hilk its not working with this.

              i opened excel file in ms-excel and i want to check excel file is open or not.

              jsulmJ 1 Reply Last reply
              0
              • Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by Pradeep Kumar
                #8

                @Gaurav-Badgujar

                u may look into the below link

                https://forum.qt.io/topic/51169/how-to-check-whether-file-already-open-or-not-in-qt

                i hope this is what ur topic says.

                Thanks,

                Pradeep Kumar
                Qt,QML Developer

                G 1 Reply Last reply
                0
                • Pradeep KumarP Pradeep Kumar

                  @Gaurav-Badgujar

                  u may look into the below link

                  https://forum.qt.io/topic/51169/how-to-check-whether-file-already-open-or-not-in-qt

                  i hope this is what ur topic says.

                  Thanks,

                  G Offline
                  G Offline
                  Gaurav Badgujar
                  wrote on last edited by
                  #9

                  @Pradeep-Kumar yup .. but i want solution.

                  1 Reply Last reply
                  0
                  • G Gaurav Badgujar

                    @J.Hilk its not working with this.

                    i opened excel file in ms-excel and i want to check excel file is open or not.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #10

                    @Gaurav-Badgujar said in Check file is open or not:

                    i opened excel file in ms-excel and i want to check excel file is open or not.

                    What? You open an Excel file in Excel and then you want to check whether Excel opened the file?
                    Do you open this Excel file manually or from your Qt app? Why do you want to check this?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    G 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @Gaurav-Badgujar said in Check file is open or not:

                      i opened excel file in ms-excel and i want to check excel file is open or not.

                      What? You open an Excel file in Excel and then you want to check whether Excel opened the file?
                      Do you open this Excel file manually or from your Qt app? Why do you want to check this?

                      G Offline
                      G Offline
                      Gaurav Badgujar
                      wrote on last edited by
                      #11

                      @jsulm yes open manually and want to check for requirement.

                      jsulmJ 1 Reply Last reply
                      0
                      • G Gaurav Badgujar

                        @jsulm yes open manually and want to check for requirement.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @Gaurav-Badgujar Do you want to check this in your Qt app?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        G 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @Gaurav-Badgujar Do you want to check this in your Qt app?

                          G Offline
                          G Offline
                          Gaurav Badgujar
                          wrote on last edited by
                          #13

                          @jsulm yes

                          mrjjM 1 Reply Last reply
                          1
                          • G Gaurav Badgujar

                            @jsulm yes

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #14

                            @Gaurav-Badgujar

                            There is no Qt way of doing this.
                            Its pr. operating system and you will need to use native API to do it.

                            for windows
                            https://stackoverflow.com/questions/1951791/how-to-check-if-a-file-is-already-open-by-another-process-in-c

                            1 Reply Last reply
                            4
                            • EddyE Offline
                              EddyE Offline
                              Eddy
                              wrote on last edited by
                              #15

                              You can check if a specific Excel file is open using ActiveQt

                              get all WorkBooks, iterate over them and check if the name of the workbook you touch corresponds to the one you want.
                              If that is the case, your excel file is already open.

                              Qt Certified Specialist
                              www.edalsolutions.be

                              mrjjM 1 Reply Last reply
                              2
                              • EddyE Eddy

                                You can check if a specific Excel file is open using ActiveQt

                                get all WorkBooks, iterate over them and check if the name of the workbook you touch corresponds to the one you want.
                                If that is the case, your excel file is already open.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by mrjj
                                #16

                                @Eddy
                                That is not a bad idea.
                                I even think that
                                ThisWorkbook.FullName
                                Workbook.FullName
                                will give full path to file.

                                1 Reply Last reply
                                1
                                • EddyE Offline
                                  EddyE Offline
                                  Eddy
                                  wrote on last edited by
                                  #17

                                  @mrjj
                                  Yes, that is possible, but other files can be open already and the one we want to check is not necessarily the active one.

                                  Eddy

                                  Qt Certified Specialist
                                  www.edalsolutions.be

                                  mrjjM 1 Reply Last reply
                                  2
                                  • EddyE Eddy

                                    @mrjj
                                    Yes, that is possible, but other files can be open already and the one we want to check is not necessarily the active one.

                                    Eddy

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @Eddy
                                    Oh bad naming. I did mean when he looped over all open.

                                    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