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?
QtWS25 Last Chance

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.
  • Mandar KhireM Offline
    Mandar KhireM Offline
    Mandar Khire
    wrote on 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.

    RatzzR 1 Reply Last reply
    0
    • Mandar KhireM Mandar Khire

      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.

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #2

      @Mandar-Khire
      Like this?

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

      --Alles ist gut.

      1 Reply Last reply
      0
      • Mandar KhireM Offline
        Mandar KhireM Offline
        Mandar Khire
        wrote on last edited by Mandar Khire
        #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?

        jsulmJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          • Mandar KhireM Mandar Khire

            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?

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on 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
            • Mandar KhireM Offline
              Mandar KhireM Offline
              Mandar Khire
              wrote on 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.

              jsulmJ JohanSoloJ 2 Replies Last reply
              0
              • Mandar KhireM Mandar Khire

                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.

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on 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
                • Mandar KhireM Mandar Khire

                  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.

                  JohanSoloJ Offline
                  JohanSoloJ Offline
                  JohanSolo
                  wrote on last edited by JohanSolo
                  #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
                  • Mandar KhireM Offline
                    Mandar KhireM Offline
                    Mandar Khire
                    wrote on 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!

                    JohanSoloJ jsulmJ 2 Replies Last reply
                    0
                    • Mandar KhireM Mandar Khire

                      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!

                      JohanSoloJ Offline
                      JohanSoloJ Offline
                      JohanSolo
                      wrote on 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
                      • Mandar KhireM Mandar Khire

                        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!

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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

                        • Login

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