Qt Widget App and libusb



  • 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


  • Qt Champions 2016

    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.



  • @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


  • Qt Champions 2016

    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



  • 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?


  • Qt Champions 2016

    @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



  • 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?


  • Qt Champions 2016

    @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.



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


  • Qt Champions 2016

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.