Need help for using qDebug() with Qt plugin for Visual Studio 2008
-
Hello there, I am trying to use the qDebug(). Here i am sharing a small sample code which i used for File IO in Qt. The code is working well as expected. But upon read or write action i see debug messages on build window. But i want to get them on windows console. I have tried setting the Linker>System>SubSystem : to "Console (/SUBSYSTEM:CONSOLE)". Getting the messages on console is necessary to serve a future purpose
Environment:
Windows 8.1
Visual Studio 2008 SP1 trial with Qt 4.8.4 Plugin
Platform: Win32
//qtfileio.h #ifndef QTFILEIO_H #define QTFILEIO_H #include <QtGui/QWidget> #include "ui_qtfileio.h" #include <QFile> #include <QString> #include <QDebug> #include <QTextStream> #include <QMessageBox> class QtFileIO : public QWidget { Q_OBJECT public: QtFileIO(QWidget *parent = 0, Qt::WFlags flags = 0); ~QtFileIO(); void write(QString filename); void read(QString filename); private: Ui::QtFileIOClass ui; public slots: void on_btnRead_clicked(); void on_btnWrite_clicked(); }; #endif // QTFILEIO_H //qtfileio.cpp /*---------------------------------------------------------------------------- * Author: Eshwar * Created on 06 December, 2017, 04:01 PM * Description: File IO in Qt * Ref. https://youtu.be/tY6nW3Wm3NE *----------------------------------------------------------------------------*/ #include "qtfileio.h" QFile file("./disk.txt"); // file location as parameter QTextStream stream(&file); // create text stream variable QtFileIO::QtFileIO(QWidget *parent, Qt::WFlags flags) : QWidget(parent, flags) { ui.setupUi(this); } QtFileIO::~QtFileIO() { } void QtFileIO::on_btnRead_clicked() { if(!file.open(QFile::ReadOnly | QFile::Text)) // read only mode QMessageBox::warning(this,"File error","cannot read file"); // throw error if file not accessible QString text = stream.readAll(); // read the plain text ui.plainTextEdit->setPlainText(text); file.close(); // close file qDebug() << "File read"; } void QtFileIO::on_btnWrite_clicked() { if(!file.open(QFile::WriteOnly | QFile::Text)) // write only mode QMessageBox::warning(this,"File error","cannot write to file"); QString text = ui.plainTextEdit->toPlainText(); stream << text; file.flush(); // flush the text data in the file file.close(); // close file qDebug() << "File written"; } //main.cpp #include "qtfileio.h" #include <QtGui/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); QtFileIO w; w.show(); return a.exec(); }
-
Where do you expect to see the output?
Did you create a console application?Visual Studio 2008 SP1 trial with Qt 4.8.4 Plugin
Nice and modern ;)
-
@VRonin This is a GUI application. I just found out that i am getting debug messages on the small output window, the window where you see the build messages. However i want to see these messages on a console. This is necessary because i will be porting the application to Win CE7 where only console is helpful for actual debugging. This is a future embedded system code exercise therefore i have to use these nice and modern things only ;)
-
@VRonin / "Lee Van Cleef" :)
Given that the OP has said "This is a GUI application", how does your link which assumes a console application help him?
I know nothing about "Win CE7". But it may be that anything written via
qDebug()
which appears within Visual Studio in its Debug output window will automatically go to whatever Win CE has in the way of a Console window? Has the OP actually just tried existing code under Win CE instead of inside VS? Unless I'm misunderstanding what the OP wishes to achieve.... -
You could go in a hard way. Do you familiar with interprocess communication or anonymous pipes or bi directional pipes? Your GUI app could interact with a small console app and display only that stuff you send via one of those approaches. So basicly you would have a GUI app and a console (for debugging purpose) app and the GUI should start the console.
-
@JNBarchan
I was referring to:
- Choose Configuration Properties>Linker>System.
- For the "Subsystem" property in the right-hand pane, click the drop-down box in the right hand column.
- Choose "Console (/SUBSYSTEM:CONSOLE)"
Having the console subsytem does not prevent you from creating Qt widgets.
@MikhailG Possible but It's like using nuclear bombs to get rid of a bird's nest
-
@VRonin said in Need help for using qDebug() with Qt plugin for Visual Studio 2008:
@JNBarchan
Having the console subsytem does not prevent you from creating Qt widgets.
I was not aware of this. I had thought
/SUBSYSTEM:CONSOLE
meant no Windows windows at all. I see that it only means create a console as well at startup.However, I note that (Visual Studio at least) has
/SUBSYSTEM:WINDOWSCE
option. If you need that for your app (I don't know), then you can't have the other....I will say one thing to the OP about this. If you go for
/SUBSYSTEM:CONSOLE
it means your app will always open a console. If you compile for release, and yourqDebug()
statements have no effect, you will still get a console. Is that what you want?If not, you have two choices:
- Ensure that you have
/SUBSYSTEM:CONSOLE
only in the Debug configuration, not the Release configuration. - Use Windows
::AllocConsole()
to allocate a console, dynamically and as needed or not. This may get you round all the other issues.
- Ensure that you have
-
Similarly while executing by the os i want the console window to show debug messages
@VRonin's of changing the linker option to
/SUBSYSTEM:CONSOLE
should do this.i want the GUI to open up a console [when running from Visual Studio]
Again, the above should do this.
In VS the debug messages appear at output window.
Yup! Qt's
qDebug
will, I imagine, go to the "debug window", which VS displays in its Output window. I don't know of/think there is a way to change that behaviour in VS --- at least, not without writing code in "VS extensions" themselves, which is another matter.Your requirement for Visual Studio output of debug lines is "unusual". Unless you/someone else knows better, you may have to settle for the above behaviour while in VS. Does that really matter? If it does, the only other way I can see is for you to cease using
qDebug()
statements in your code, and use something similar of your own which writes to a "console" explicitly rather than to the debug stream.