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. how to print 'basic_string<char> std::string' in qt?
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Solved General and Desktop
qstringstringc++qt5.5.1qt creator 3.6
11 Posts 5 Posters 16.6k 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.
  • M Offline
    M Offline
    Mandar Khire
    wrote on 1 Jun 2016, 13:07 last edited by
    #1

    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.

    R 1 Reply Last reply 1 Jun 2016, 13:20
    0
    • M Mandar Khire
      1 Jun 2016, 13:07

      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.

      R Offline
      R Offline
      Ratzz
      wrote on 1 Jun 2016, 13:20 last edited by
      #2

      @Mandar-Khire
      Like this?

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

      --Alles ist gut.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mandar Khire
        wrote on 1 Jun 2016, 13:59 last edited by Mandar Khire 6 Jan 2016, 14:05
        #3

        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?

        J 1 Reply Last reply 2 Jun 2016, 04:44
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 1 Jun 2016, 20:42 last edited by
          #4

          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);
          

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

          1 Reply Last reply
          2
          • M Mandar Khire
            1 Jun 2016, 13:59

            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?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 2 Jun 2016, 04:44 last edited by
            #5

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

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mandar Khire
              wrote on 2 Jun 2016, 05:52 last edited by
              #6

              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.

              J J 2 Replies Last reply 2 Jun 2016, 06:02
              0
              • M Mandar Khire
                2 Jun 2016, 05:52

                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.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 2 Jun 2016, 06:02 last edited by
                #7

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

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

                1 Reply Last reply
                0
                • M Mandar Khire
                  2 Jun 2016, 05:52

                  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.

                  J Offline
                  J Offline
                  JohanSolo
                  wrote on 2 Jun 2016, 06:09 last edited by JohanSolo 6 Feb 2016, 06:09
                  #8

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

                  `They did not know it was impossible, so they did it.'
                  -- Mark Twain

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mandar Khire
                    wrote on 2 Jun 2016, 06:51 last edited by
                    #9

                    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!

                    J J 2 Replies Last reply 2 Jun 2016, 06:58
                    0
                    • M Mandar Khire
                      2 Jun 2016, 06:51

                      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!

                      J Offline
                      J Offline
                      JohanSolo
                      wrote on 2 Jun 2016, 06:58 last edited by
                      #10

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

                      `They did not know it was impossible, so they did it.'
                      -- Mark Twain

                      1 Reply Last reply
                      1
                      • M Mandar Khire
                        2 Jun 2016, 06:51

                        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!

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 2 Jun 2016, 08:43 last edited by
                        #11

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

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

                        1 Reply Last reply
                        0

                        8/11

                        2 Jun 2016, 06:09

                        • Login

                        • Login or register to search.
                        8 out of 11
                        • First post
                          8/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved