Open In App

How to Print String Literal and Qstring With Qdebug in C++?

Last Updated : 01 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Printing string literals and QString with QDebug in C++ can be a useful tool for debugging. By printing out the contents of a string or QString, you can quickly identify any issues that may be present in your code. In this article, we’ll discuss how to print string literals and QString with QDebug in C++. 

The first step is to include the header file <QDebug>. This header file provides access to the debug output stream, which is used for printing out data. Once you have included this header file, you can use the qDebug() function to print out data from a string literal or a QString object. When printing out data from a string literal, you simply need to pass it as an argument into qDebug(). 

For example:

qDebug("This is my string literal"); 

This will print “This is my string literal” to the debug output stream. You can use the qDebug() function to print both string literal and QString with QDebug. 

For example:

Printing a string literal 
const char* str = "Hello World!"; 
qDebug() << str; 

Printing a QString 
QString qstr = "Hello World!"; 
qDebug() << qstr;

C++




#include <QDebug>
#include <QString>
   
int main() {
  
   // Print String Literal 
   qDebug() << "This is a string literal";
  
   // Print QString 
   QString str = "This is a QString";
   qDebug() << str;
  
   return 0;
}


Output:

This is a string literal
This is a QString

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads