PDA

View Full Version : Converting GUI to CONSOLE



timmu
4th December 2011, 14:45
Hi,

I've created a GUI application and I'd like to convert it into a CONSOLE application. I took out some lines (see //) and addes some (see //ADDED). However, the simple test program failed to compile with this error message: "main.cpp:29: error: ‘myFunction’ was not declared in this scope". Below are my main.cpp, functions.cpp and tom.h

main.cpp


#include <QCoreApplication>//ADD
#include <QApplication>
#include <QtGui>
#include "tom.h"
#include "functions.cpp"

MyWidget::MyWidget(QWidget* parent): QWidget(parent)
{
//mainLayout = new QVBoxLayout;
//buttonLayout = new QHBoxLayout;
//runButton = new QPushButton("run");
//connect(runButton, SIGNAL(clicked()), this, SLOT(myFunction()));
//mainLayout->addLayout(buttonLayout);
//buttonLayout->addWidget(runButton);
//setLayout(mainLayout);
}

////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
//QApplication application(argc, argv);
QCoreApplication application(argc, argv);//ADD
myFunction();//ADD

//Widget window;
//window.show();
//return application.exec();

return 0;//ADD
}

functions.cpp


#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

/////////////////////////////////////////////////////
void MyWidget::myFunction()
{
FILE *output; if((output=fopen("output.txt","w")) == 0) {exit(1);}
fprintf(output,"hello\n");
fclose(output);
}

tom.h


#ifndef TOM_H
#define TOM_H
#include <QtGui>

class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget (QWidget* parent = 0);
void myFunction();//ADD

public slots:
//void myFunction();

signals:

protected:

private:
//QVBoxLayout* mainLayout;
//QHBoxLayout* buttonLayout;
//QPushButton* runButton;
};

#endif


I'd be most thankful for any help!

Oleg
4th December 2011, 15:58
QWidget is part of QtGui, so it can't be used in console app.
You get error, as there's really no such function as myFunction() in main.cpp.
In .pro file you should have


QT -= gui
CONFIG += console

Qt Creator can create sample console project for you

timmu
5th December 2011, 07:52
Hi,

Thank you very much for your helpful answer. However, even when I make these changes in the pro file, I still get the same error. There has to be something else wrong with the code. Perhaps I'm not including the correct libraries?

If QWidget is not possible in console applications, then how do you define a class so that all member function could share the same functions and data? For example I want all of my functions (that are located in separate cpp files) to have access to the same QString.

Thanks again!

FelixB
5th December 2011, 08:24
Thank you very much for your helpful answer. However, even when I make these changes in the pro file, I still get the same error. There has to be something else wrong with the code. Perhaps I'm not including the correct libraries?

As Oleg already mentioned, "myFunction" is not declared in main.cpp


If QWidget is not possible in console applications, then how do you define a class so that all member function could share the same functions and data? For example I want all of my functions (that are located in separate cpp files) to have access to the same QString.

I don't understand why you need a QWidget for that. Maybe you should read some tutorials about C++ and object oriented programming before you continue working with Qt.

But, to answer your question: in a class, all member methods have access to all member variables. Thats basic c++, nothing Qt-related.

Felix