Results 1 to 8 of 8

Thread: how to print string??

  1. #1
    Join Date
    Jul 2007
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to print string??

    i want to know how we can print any string in qt as cout and qwarning are not working.

    Is there any other way for this??

    please help

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to print string??

    Look for qPrintable(...) ;-)

  3. #3
    Join Date
    Jul 2007
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to print string??

    Is QString::toLatin1() what you are looking for??

    QString text="text";
    int ret = QMessageBox::warning(this, tr("My Application"), text.toLatin1(), QMessageBox::Ok);

    J.

  4. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: how to print string??

    Quote Originally Posted by Shaitan View Post
    Is QString::toLatin1() what you are looking for??

    QString text="text";
    int ret = QMessageBox::warning(this, tr("My Application"), text.toLatin1(), QMessageBox::Ok);

    J.
    QString::toLatin1() returns QByteArray neither QString nor const char *.
    Also QMessageBox takes QString as parameter which doesn't cause problem. But qWarning, qDebug takes const char * as parameter and you can use qPrintable to convert QString to const char *
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  5. #5
    Join Date
    Apr 2007
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to print string??

    Hi,

    If you want to use cout to print, I have attached two files to this email...qstd.cpp and qstd.h. To use the standard cout syntax include these files in your project and add the namespace using command - using namespace qstd - to any scope in which you want to use cout (and include the "qstd.h" header file of course).

    I did not write these files myself, but stumbled upon them somewhere on the web while trying to solve a problem similiar to yours when I first started with Qt. I give full credit, thanks and kudo's to the unamed author....cout has worked flawlessly for me.

    Hope this helps....

    tim
    Attached Files Attached Files

  6. #6
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: how to print string??

    If you just want to print one or two you can always use the toStdString() method. So:
    Qt Code:
    1. QString str = "hello";
    2. std::cout<<str.toStdString()<<std::endl;
    To copy to clipboard, switch view to plain text mode 

    would print
    Qt Code:
    1. hello
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    84
    Thanks
    7
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to print string??

    i still like using the good old printf:

    QString str;
    printf("%s\n", str.toLatin1().constData());
    fflusg(stdout);

    regards,
    jh

  8. The following 2 users say thank you to jh for this useful post:

    alok9871 (5th March 2013), ntp (2nd August 2007)

  9. #8
    Join Date
    Dec 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to print string??

    i use
    Qt Code:
    1. qDebug() << "hello world";
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  2. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40
  3. string to char*?
    By nass in forum General Programming
    Replies: 3
    Last Post: 2nd January 2007, 15:47
  4. Create pixmap image from string
    By Morea in forum Qt Programming
    Replies: 5
    Last Post: 17th November 2006, 16:38
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.