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. QT image display is not working without declaring openCV namedWindow in windows 10
Forum Update on Monday, May 27th 2025

QT image display is not working without declaring openCV namedWindow in windows 10

Scheduled Pinned Locked Moved Solved General and Desktop
qt 5.14.2c++ qtopencvwindows 10
17 Posts 5 Posters 3.4k 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.
  • S SGaist
    2 Jul 2020, 06:30

    You're blocking the event loop with your technique hence your issue.

    Use a timer to trigger the read of the next frame.

    I Offline
    I Offline
    Imran Hassan
    wrote on 2 Jul 2020, 06:58 last edited by
    #8

    @SGaist You are right. Let me check this with timer and then I will get back to you.

    1 Reply Last reply
    0
    • S SGaist
      2 Jul 2020, 06:45

      @Imran-Hassan yes: it's sheer luck. The wait for key you put there might act differently on Linux than on Windows but in any case the issue remains: you're doing it wrong. Since you wait 25 milliseconds anyway, refactor your code to use a QTimer so it does not block the event loop on any platform.

      I Offline
      I Offline
      Imran Hassan
      wrote on 2 Jul 2020, 07:40 last edited by
      #9

      @SGaist said in QT image display is not working without declaring openCV namedWindow in windows 10:

      25 milliseconds anyway

      I did it using timer with 1000 ms and its working fine.

      There is one issue dear. This was just a sample code, actually I my main code is too big and a lot of things to do in while loop very long while loop and there timers will not work as sync issue will occur. Actually that main code was also working perfect in Linux and when that was switched to windows this all happened. I am also using threads in that code.

      Dear your guidance and help is required in this matter.
      Regards

      J 1 Reply Last reply 2 Jul 2020, 08:09
      0
      • I Imran Hassan
        2 Jul 2020, 07:40

        @SGaist said in QT image display is not working without declaring openCV namedWindow in windows 10:

        25 milliseconds anyway

        I did it using timer with 1000 ms and its working fine.

        There is one issue dear. This was just a sample code, actually I my main code is too big and a lot of things to do in while loop very long while loop and there timers will not work as sync issue will occur. Actually that main code was also working perfect in Linux and when that was switched to windows this all happened. I am also using threads in that code.

        Dear your guidance and help is required in this matter.
        Regards

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 2 Jul 2020, 08:09 last edited by
        #10

        @Imran-Hassan said in QT image display is not working without declaring openCV namedWindow in windows 10:

        Dear your guidance and help is required in this matter.

        What guidance and help exactly?

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

        I 1 Reply Last reply 2 Jul 2020, 08:39
        0
        • J jsulm
          2 Jul 2020, 08:09

          @Imran-Hassan said in QT image display is not working without declaring openCV namedWindow in windows 10:

          Dear your guidance and help is required in this matter.

          What guidance and help exactly?

          I Offline
          I Offline
          Imran Hassan
          wrote on 2 Jul 2020, 08:39 last edited by Imran Hassan 7 Feb 2020, 09:47
          #11

          @jsulm Here about this issue ....

          I am displaying the image captured by webcam to QT label. It was working perfect in Ubuntu but after migrating to windows it is not working. It work only if I create openCV namedWindow("frame",0). It creates extra window of openCV. I installed openCV 4.20 with_FFMPEG open checked.

          The code shown in above thread was just a sample code, actually I my main code is too big and a lot of things to do in while loop very long while loop and there timers will not work as sync issue will occur. Actually that main code was also working perfect in Linux and when that was switched to windows this all happened. I am also using threads in that code.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 2 Jul 2020, 08:57 last edited by
            #12

            As the code you show is just an example mostly not related to your actual code:

            • Do not block the event loop
            • Do not use tight loops like that in the main thread because it blocks the event loop
            • Refactor your code
            • Do not use threads unless it's really really really necessary and before using them check once more
            • If you really need to use threads: start read the documentation and literature about them. It shall be considered as a nuclear bomb to shoot your foot with. Then if you use the QThread, thoroughly read its latest documentation as it has severely improved over the years and shows the two common ways to use them. If you use signals and slots with QThread and start to specify the connection type then you are extremely likely to be doing something wrong.
            • Because something works on Linux, it does not mean it will work on any other platform
            • The fact that FFMPEG is used as backend for OpenCV is unrelated to the issue at hand

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

            I 1 Reply Last reply 2 Jul 2020, 11:00
            2
            • S SGaist
              2 Jul 2020, 08:57

              As the code you show is just an example mostly not related to your actual code:

              • Do not block the event loop
              • Do not use tight loops like that in the main thread because it blocks the event loop
              • Refactor your code
              • Do not use threads unless it's really really really necessary and before using them check once more
              • If you really need to use threads: start read the documentation and literature about them. It shall be considered as a nuclear bomb to shoot your foot with. Then if you use the QThread, thoroughly read its latest documentation as it has severely improved over the years and shows the two common ways to use them. If you use signals and slots with QThread and start to specify the connection type then you are extremely likely to be doing something wrong.
              • Because something works on Linux, it does not mean it will work on any other platform
              • The fact that FFMPEG is used as backend for OpenCV is unrelated to the issue at hand
              I Offline
              I Offline
              Imran Hassan
              wrote on 2 Jul 2020, 11:00 last edited by
              #13

              @SGaist Yes I have checked the whole code again.

              I am confused with one thing that If I run the existing code in Linux it works fine. In window also if I call namedWindow() function of openCV it start working. It will show video on two windows one in QT windows and other openCV windows, even after closing the openCV window by pressing cross it is still working. If I do not call namedWindow() function it is not working.

              namedWindow() funtion is triggering something which makes it work.

              I even replaced loop with timer in my main code but problem remains same there ....

              B 1 Reply Last reply 2 Jul 2020, 15:36
              0
              • I Imran Hassan
                2 Jul 2020, 11:00

                @SGaist Yes I have checked the whole code again.

                I am confused with one thing that If I run the existing code in Linux it works fine. In window also if I call namedWindow() function of openCV it start working. It will show video on two windows one in QT windows and other openCV windows, even after closing the openCV window by pressing cross it is still working. If I do not call namedWindow() function it is not working.

                namedWindow() funtion is triggering something which makes it work.

                I even replaced loop with timer in my main code but problem remains same there ....

                B Offline
                B Offline
                Bonnie
                wrote on 2 Jul 2020, 15:36 last edited by Bonnie 7 Feb 2020, 15:45
                #14

                @Imran-Hassan
                Hey, I've tried your code with opencv 4.3.0 vc15 (downloaded from the official website) and it works fine with my cam (except that "waitKey" so I can't exit the loop by pressing Esc).
                And, wow, that loop doesn't block the UI. So magical.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Imran Hassan
                  wrote on 2 Jul 2020, 16:49 last edited by
                  #15

                  @Imran-Hassan said in QT image display is not working without declaring openCV namedWindow in windows 10:

                  ui->label->setPixmap(QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped()));

                  Is it working in windows?
                  Ok I will try with openCV 4.3.0 in windows.

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    Imran Hassan
                    wrote on 2 Jul 2020, 16:54 last edited by
                    #16

                    @SGaist @Bonnie @jsulm

                    The problem is solved by adding

                    qApp->processEvents(); after the

                    ui->label->setPixmap(QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped()));

                    @SGaist as you said this is not very good organized code having loops but for now this is temporary solution we found out. We will reorganize the code and remove the unnecessary loops.

                    Anyway Thanks you all for such a great help.

                    P 1 Reply Last reply 2 Jul 2020, 16:55
                    0
                    • I Imran Hassan
                      2 Jul 2020, 16:54

                      @SGaist @Bonnie @jsulm

                      The problem is solved by adding

                      qApp->processEvents(); after the

                      ui->label->setPixmap(QPixmap::fromImage(QImage(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped()));

                      @SGaist as you said this is not very good organized code having loops but for now this is temporary solution we found out. We will reorganize the code and remove the unnecessary loops.

                      Anyway Thanks you all for such a great help.

                      P Offline
                      P Offline
                      Pablo J. Rogina
                      wrote on 2 Jul 2020, 16:55 last edited by
                      #17

                      @Imran-Hassan said in QT image display is not working without declaring openCV namedWindow in windows 10:

                      The problem is solved

                      great, so please don't forget to mark your post as such!

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0

                      17/17

                      2 Jul 2020, 16:55

                      • 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