how to print 'basic_string<char> std::string' in qt?



  • hi,
    I wrote very simple program as below:-

    #include "widget.h"
    #include "ui_widget.h"
    #include <iostream>
    #include <string>
    using namespace std;
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    }
    Widget::~Widget()
    {
        delete ui;
    }
    void Widget::on_showButton_clicked()
    {
        qDebug() <<"Hello World by qDebug";
        ui->label->setText("hello world in QLabel");
        string teststring = "Hello World by String";
      // how to print teststring?
    }
    

    I am not qt or c++ expert but I found that
    How to print a string in C++
    But given ways not working for me!
    Need help.



  • @Mandar-Khire
    Like this?

    qDebug() <<"Test string " <<teststring;


  • Thanks Ratzz,
    But when I follow your instruction then got following error!

    /workplaces/QT_workplace/hello_world/widget.cpp:27: error: no match for 'operator<<' (operand types are 'QDebug' and 'std::string {aka std::basic_string<char>}')
         qDebug() <<"Test string " <<teststring;
                                   ^
    

    But I got way:-

     cout << teststring<<endl;
    

    I found that above line works fine.
    But when I tried

     cout << teststring;
    

    Its not working atoll though there is no error while compilation.
    Do you know significance of 'endl' in C++ & QT?


  • Lifetime Qt Champion

    Hi,

    Qt is written in C++, it's not a different language. endl is explained here.

    If you would like to print a std::string with QDebug, the simplest way is to do something like:

    qDebug() << QString::fromStdString(myStdString);
    

  • Moderators

    @Mandar-Khire "Its not working atoll though there is no error while compilation." - what is not working?



  • Thanks @SGaist,

    qDebug() << QString::fromStdString(teststring);
    

    It works very properly.

    @jsulm I wrote "Its not working atoll though there is no error while compilation." for following command:-

     cout << teststring;
    

    This command work in basic c++ 'helloworld.cpp' program but not work in qt program. If there is specific reason behind this then it will be helpful to understand c++ & qt better way.

    But Thanks both.


  • Moderators

    @Mandar-Khire Do you start event loop (a.exec()) in your Qt program after

    cout << teststring;
    

    If so then that's the reason: cout is buffered, that means if you write to it it is not visible immediately - it is written into a buffer first. If you then start Qt event loop then this buffer is not flushed.



  • @Mandar-Khire said:

    @jsulm I wrote "Its not working atoll though there is no error while compilation." for following command:-

     cout << teststring;
    

    This command work in basic c++ 'helloworld.cpp' program but not work in qt program. If there is specific reason behind this then it will be helpful to understand c++ & qt better way.

    This has nothing to do with Qt: in a "pure" C++ program you can reproduce the same behaviour. As @jsulm told you, the stream is buffered: in order to actually print the stream, you can either use cout << teststring << flush or cout << teststring << endl, the first one will force to empty the buffer, the second will do the same after adding a line feed.



  • Thanks @JohanSolo ,
    As you wrote

    This has nothing to do with Qt: in a "pure" C++ program you can reproduce the same behaviour. 
    

    I seen without '<<flush' or '<<endl' cout works in basic C++ program,
    example of program as below:-

    
    	// A hello world program in C++
    	#include<iostream>
    	using namespace std;
    	int main()
    	{
    		cout << "Hello World by C++!";
    		return 0;
    	}
    

    but in QT program same 'cout' not work without '<<flush' or '<<endl' . Example as below:-

    #include "widget.h"
    #include "ui_widget.h"
    #include <iostream>
    #include <string>
    using namespace std;
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    }
    Widget::~Widget()
    {
        delete ui;
    }
    void Widget::on_showButton_clicked()
    {
    cout << "Hello World by QT!";
    }
    

    Why QT compulsory for '<<flush' or '<<endl' while using 'cout'?
    Thats the basic question!



  • @Mandar-Khire said:

    Why QT compulsory for '<<flush' or '<<endl' while using 'cout'?
    Thats the basic question!

    Again, it's not related to Qt. It depends on what you're doing after the cout statement. In some cases where you use cout without flushing and and make an intensive use of cerr it is common you don't see the output of cout. I'm not telling you this will always or never work, but you might have problems if you don't flush a buffered stream. So the best practise is to always add a endl statement (or flush if you don't care about the line feed).


  • Moderators

    @Mandar-Khire If you change your basic C++ program like this then you will not see the output as well:

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "Hello World by C++!";
        while(true);
        return 0;
    }
    

    Qt uses an event loop, so after executing

    void Widget::on_showButton_clicked()
    {
    cout << "Hello World by QT!";
    }
    

    your program is "sleeping" in the event loop and not flashing cout buffer.



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