Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QIODevice cannot open .ui file from resources.
Forum Updated to NodeBB v4.3 + New Features

QIODevice cannot open .ui file from resources.

Scheduled Pinned Locked Moved Solved Qt for Python
13 Posts 3 Posters 1.9k 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.
  • Z Offline
    Z Offline
    zspinelli
    wrote on 26 Jul 2021, 19:15 last edited by
    #1

    I'm trying to use the FormBuilder class to load ui files from project resources I get the error:

    QIODevice::read (QFile, ":\_qtresources\ui\NewDocumentForm.ui"): device not open
    Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document.
    

    Here is what is loading it:

    class NewDocumentForm(QDialog):
        accepted = Signal()
    
        def __init__(self):
            super().__init__()
    
            form = QFile(":/_qtresources/ui/NewDocumentForm.ui")
            form.open(QFile.ReadOnly)
    
            builder = QFormBuilder()
            builder.load(form, self)
    
    

    I can only guess that it's somehow not finding the ui file since the qiodevice turns up effectively empty.

    J 1 Reply Last reply 26 Jul 2021, 19:22
    0
    • Z zspinelli
      26 Jul 2021, 19:15

      I'm trying to use the FormBuilder class to load ui files from project resources I get the error:

      QIODevice::read (QFile, ":\_qtresources\ui\NewDocumentForm.ui"): device not open
      Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document.
      

      Here is what is loading it:

      class NewDocumentForm(QDialog):
          accepted = Signal()
      
          def __init__(self):
              super().__init__()
      
              form = QFile(":/_qtresources/ui/NewDocumentForm.ui")
              form.open(QFile.ReadOnly)
      
              builder = QFormBuilder()
              builder.load(form, self)
      
      

      I can only guess that it's somehow not finding the ui file since the qiodevice turns up effectively empty.

      J Offline
      J Offline
      JonB
      wrote on 26 Jul 2021, 19:22 last edited by
      #2

      @zspinelli
      For the error message, that comes from trying to load from a non-open file. form.open(QFile.ReadOnly) returns a boolean to indicate success/failure, and I would guess its returning False. QIODevice.errorString() might provide more information.

      Z 1 Reply Last reply 26 Jul 2021, 19:33
      0
      • J JonB
        26 Jul 2021, 19:22

        @zspinelli
        For the error message, that comes from trying to load from a non-open file. form.open(QFile.ReadOnly) returns a boolean to indicate success/failure, and I would guess its returning False. QIODevice.errorString() might provide more information.

        Z Offline
        Z Offline
        zspinelli
        wrote on 26 Jul 2021, 19:33 last edited by zspinelli
        #3

        @JonB
        The errorString gives me "No such file or directory"

        J 1 Reply Last reply 26 Jul 2021, 19:34
        0
        • Z zspinelli
          26 Jul 2021, 19:33

          @JonB
          The errorString gives me "No such file or directory"

          J Offline
          J Offline
          JonB
          wrote on 26 Jul 2021, 19:34 last edited by
          #4

          @zspinelli
          So that's the explanation for the error! That's all I was saying.

          Z 1 Reply Last reply 26 Jul 2021, 19:44
          0
          • J JonB
            26 Jul 2021, 19:34

            @zspinelli
            So that's the explanation for the error! That's all I was saying.

            Z Offline
            Z Offline
            zspinelli
            wrote on 26 Jul 2021, 19:44 last edited by
            #5

            @JonB
            I just found something now for a tool called rcc. Is that absolutely always neccessary to use qrc stuff?
            If so, I think this might be the problem.

            J S 2 Replies Last reply 26 Jul 2021, 19:50
            0
            • Z zspinelli
              26 Jul 2021, 19:44

              @JonB
              I just found something now for a tool called rcc. Is that absolutely always neccessary to use qrc stuff?
              If so, I think this might be the problem.

              J Offline
              J Offline
              JonB
              wrote on 26 Jul 2021, 19:50 last edited by
              #6

              @zspinelli
              I assume your form resource is not at :/_qtresources/ui/NewDocumentForm.ui.

              Z 1 Reply Last reply 26 Jul 2021, 20:06
              0
              • J JonB
                26 Jul 2021, 19:50

                @zspinelli
                I assume your form resource is not at :/_qtresources/ui/NewDocumentForm.ui.

                Z Offline
                Z Offline
                zspinelli
                wrote on 26 Jul 2021, 20:06 last edited by
                #7

                @JonB
                That's the exact path to it from the top of the source directory in the file system.
                That's the path resources gave it when I added the form to the project.
                I don't know where it would be if it's not there.

                J 1 Reply Last reply 26 Jul 2021, 20:17
                0
                • Z zspinelli
                  26 Jul 2021, 19:44

                  @JonB
                  I just found something now for a tool called rcc. Is that absolutely always neccessary to use qrc stuff?
                  If so, I think this might be the problem.

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 26 Jul 2021, 20:12 last edited by
                  #8

                  Hi,

                  @zspinelli said in QIODevice cannot open .ui file from resources.:

                  @JonB
                  I just found something now for a tool called rcc. Is that absolutely always neccessary to use qrc stuff?
                  If so, I think this might be the problem.

                  Yes that is mandatory. Resources must be first created like in C++. See here.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Z 1 Reply Last reply 26 Jul 2021, 20:30
                  1
                  • Z zspinelli
                    26 Jul 2021, 20:06

                    @JonB
                    That's the exact path to it from the top of the source directory in the file system.
                    That's the path resources gave it when I added the form to the project.
                    I don't know where it would be if it's not there.

                    J Offline
                    J Offline
                    JonB
                    wrote on 26 Jul 2021, 20:17 last edited by
                    #9

                    @zspinelli said in QIODevice cannot open .ui file from resources.:

                    That's the exact path to it from the top of the source directory in the file system.

                    That's not what you want at runtime. The :/... file path syntax is treated by Qt as referring to a resource compiled into your executable, not a real, external file. Follow @SGaist's link to achieve that.

                    Z 1 Reply Last reply 27 Jul 2021, 06:25
                    1
                    • S SGaist
                      26 Jul 2021, 20:12

                      Hi,

                      @zspinelli said in QIODevice cannot open .ui file from resources.:

                      @JonB
                      I just found something now for a tool called rcc. Is that absolutely always neccessary to use qrc stuff?
                      If so, I think this might be the problem.

                      Yes that is mandatory. Resources must be first created like in C++. See here.

                      Z Offline
                      Z Offline
                      zspinelli
                      wrote on 26 Jul 2021, 20:30 last edited by
                      #10

                      @SGaist
                      The link you posted shows an example using a tool called "pyside6-rcc".
                      If that's supposed to be installed with PySide6 then for some reason I don't have it. I found this page https://stackoverflow.com/questions/22479581/cannot-use-pyside-rcc-to-compile-qrc-file mentioning some locations where it might be but it isn't present in any of them.

                      However I found another page: https://doc.qt.io/qtforpython/overviews/resources.html showing the use of the regular "rcc" which I do have as part of the Qt installation. Before I try the example shown there, should I know if one is one tool is more preferable than the other?

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 26 Jul 2021, 21:11 last edited by
                        #11

                        If the one you have supports Python code generation, then use it.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • Z Offline
                          Z Offline
                          zspinelli
                          wrote on 26 Jul 2021, 22:59 last edited by
                          #12

                          I found pyside6-rcc. I'm going to note for posterity that it was found under C:\Users\<username>\AppData\Roaming\Python\Python39\Scripts

                          I ran it, got the py output as per the demonstration, and imported it. The errorString for the QFile is now "Unknown error".

                          1 Reply Last reply
                          0
                          • J JonB
                            26 Jul 2021, 20:17

                            @zspinelli said in QIODevice cannot open .ui file from resources.:

                            That's the exact path to it from the top of the source directory in the file system.

                            That's not what you want at runtime. The :/... file path syntax is treated by Qt as referring to a resource compiled into your executable, not a real, external file. Follow @SGaist's link to achieve that.

                            Z Offline
                            Z Offline
                            zspinelli
                            wrote on 27 Jul 2021, 06:25 last edited by
                            #13

                            @JonB @SGaist
                            After lot of internet searching and fistfighting the code, I got my project back on track and got some other stuff fixed as well.
                            I had pyside6 module installed wrong and was using the wrong approach for integrating the UI form.
                            @SGaist Your recommendation for pyside6-rss led me to pyside6-uic, which was what I really needed.

                            I'm thankful to you both for tolerating and assisting me.

                            1 Reply Last reply
                            2

                            5/13

                            26 Jul 2021, 19:44

                            8 unread
                            • Login

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