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 Widget App and libusb
Qt 6.11 is out! See what's new in the release blog

Qt Widget App and libusb

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 3.7k 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.
  • A Offline
    A Offline
    AleFachini
    wrote on last edited by AleFachini
    #1

    Hello, I am very new to QT and i managed to run a QT Helloworld! GUI app.
    I also was able to compile LIBUSB library and run a function, INSIDE the qt widget app.
    The problem is that the function will run and print to the console of Qt Creator only after I close(QUIT) the GUI window.

    So i was wondering how can i do to print the "printf" lines that my function executes into a Qlabel or Qtextfield in the GUI.
    I found a post talking about using QThreads where the threas report the printable lines from printf's to the Qlabel or Qtextfield, or a variable.
    I just need to get pointed towards the right way to do it, if someone can help me i really apreciate it!

    Thank you

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You can also just use
      qDebug() << "hello";

      if you really must use printf , you can
      use redirection to file and then load the file using a timer, into a QT widget.

      freopen("desiredFileName", "w", stdout);

      But that said, printf in GUI programs is not super.

      A 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        You can also just use
        qDebug() << "hello";

        if you really must use printf , you can
        use redirection to file and then load the file using a timer, into a QT widget.

        freopen("desiredFileName", "w", stdout);

        But that said, printf in GUI programs is not super.

        A Offline
        A Offline
        AleFachini
        wrote on last edited by
        #3

        @mrjj said in Qt Widget App and libusb:

        Hi
        You can also just use
        qDebug() << "hello";

        if you really must use printf , you can
        use redirection to file and then load the file using a timer, into a QT widget.

        freopen("desiredFileName", "w", stdout);

        But that said, printf in GUI programs is not super.

        Im using print f because i need to present the data from the USB devices in a HEX format and of course i dont know how to do it in qt. its been a weeks since i installed the Qt creator and im starting to learn.
        If there is another way im open to try it

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          The QString can convert to/from hex. And so can ByteArray
          The simple form is
          QString hex = QString::number( nValue, 16 );
          and for more control

          uint decimal = 203888026;
          QString hexvalue = QString("0x%1").arg(decimal, 8, 16, QLatin1Char( '0' ));

          For byte array , please see
          http://doc.qt.io/qt-5/qbytearray.html#toHex

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AleFachini
            wrote on last edited by
            #5

            OK ive fixed my problem by doing this:
            qDebug() << "Vendor = " << hex << desc.idVendor << "|" << hex << desc.idProduct;

            BUT, this is printed when i click the button (WHICH is what i want) .
            NOW, my issue is that, when i EXIT the app i get a second Print, like the function executes once more, why is that?

            mrjjM 1 Reply Last reply
            0
            • A AleFachini

              OK ive fixed my problem by doing this:
              qDebug() << "Vendor = " << hex << desc.idVendor << "|" << hex << desc.idProduct;

              BUT, this is printed when i click the button (WHICH is what i want) .
              NOW, my issue is that, when i EXIT the app i get a second Print, like the function executes once more, why is that?

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

              @AleFachini
              well I would place a break point and see if its really the case.
              It could also be you still have a printf left and its buffered, so you need to call
              fflush(stdout);
              to see it.
              Or add a newline to printf

              http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AleFachini
                wrote on last edited by
                #7

                Of Course i did not remove the printf!! now i only get the print wehen i click and not the buffered print!!!

                My last question and ill mark this as SOLVED, why is that the printf appear when i CLOSE the app? it has smething to do with the thread being used for the GUI? or why?

                mrjjM 1 Reply Last reply
                0
                • A AleFachini

                  Of Course i did not remove the printf!! now i only get the print wehen i click and not the buffered print!!!

                  My last question and ill mark this as SOLVED, why is that the printf appear when i CLOSE the app? it has smething to do with the thread being used for the GUI? or why?

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

                  @AleFachini
                  Hi
                  As the link talks about its about
                  "printf" being buffered. (inside GUI)
                  So u must call
                  fflush(stdout); ( after printf)
                  or have newline in the printf.

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    AleFachini
                    wrote on last edited by
                    #9

                    OK, This is SOLVED then, but i dont find the option to mark it, Thanks!

                    mrjjM 1 Reply Last reply
                    0
                    • A AleFachini

                      OK, This is SOLVED then, but i dont find the option to mark it, Thanks!

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

                      @AleFachini
                      Hi
                      Inside the Topic Tool button on the first post.
                      If not there, u need to convert it to question first:)

                      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