0

I'm trying to learn C++ OOP and thought would try a simple example using Qt Creator. I clicked New Project > Projects > application > Qt Console Application

I then added a new class, test.cpp. Both test.cpp and main.cpp are in the Sources folder and test.h is in the Headers foler.

Here is test.h

#ifndef TEST_H
#define TEST_H


class test
{
 public:
    test();
 };

#endif // TEST_H

test.cpp

#include "test.h"
#include "iostream"

using namespace std;

test::test()
{
   cout<<"Inside test's constructor "<<endl;
}

main.cpp

#include "iostream"
#include "test.h"

using namespace std;

int main()
{

   test ts;

   return 0;
}

When I click the run button, it's built and run. The console window displays but "Inside test's constructor" never gets to be printed to the screen. What am I doing wrong?

WhatIf
  • 653
  • 2
  • 8
  • 18
  • Most likely, it just closes too fast before you can see the output. Try running your executable from already open console window. – SergeyA Dec 08 '15 at 18:24
  • The console window remains open and I have to close it by either Ctrl+C or by clicking the x button. – WhatIf Dec 08 '15 at 18:26
  • Is this the problem? [Running my C++ code gives me a blank cmd](http://stackoverflow.com/questions/33690697/running-my-c-code-gives-me-a-blank-cmd) – Bo Persson Dec 08 '15 at 18:36
  • Check the application output tab on the bottom of Qt Creator. Also try using `qDebug() << Q_FUNC_INFO;` instead. It is very clean and it is context sensitive. You would also need `#include ` for it to work. – phyatt Dec 08 '15 at 18:37
  • I’d rather debug this with cout than qDebug, because it does as advertised regardless of platform (no debug channel on Windows) and build type (works in both release and debug). @WhatIf, which platform are you on? Maybe also add your .pro file. – Frank Osterfeld Dec 08 '15 at 21:39
  • Qt Creator's build environment uses special stub, which outputs to console only if run from qt creator – Swift - Friday Pie May 18 '23 at 17:17

2 Answers2

1

Go to Preferences in Qt Creator, select Environment. You will see two boxes under the General Tab. They are User Interface and System.

Under System you will see

Terminal :

Mine says

/usr/X11/bin/xterm -e

Try a different terminal from the one you have now. This has been a problem on some systems for a while.

john elemans
  • 2,578
  • 2
  • 15
  • 26
-1

I stopped bothering about dealing with cout, the console, etc and just use QDebug

qDebug() << "Date:" << QDate::currentDate();
qDebug() << "Types:" << QString("String") << QChar('x') << QRect(0, 10, 50, 40);
qDebug() << "Custom coordinate type:" << coordinate;

#include "test.h"
#include "iostream"
#include <QDebug>

using namespace std;

test::test()
{
   qDebug()<<"Inside test's constructor ";
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
spring
  • 18,009
  • 15
  • 80
  • 160