Results 1 to 4 of 4

Thread: Converting GUI to CONSOLE

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Question Converting GUI to CONSOLE

    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
    Qt Code:
    1. #include <QCoreApplication>//ADD
    2. #include <QApplication>
    3. #include <QtGui>
    4. #include "tom.h"
    5. #include "functions.cpp"
    6.  
    7. MyWidget::MyWidget(QWidget* parent): QWidget(parent)
    8. {
    9. //mainLayout = new QVBoxLayout;
    10. //buttonLayout = new QHBoxLayout;
    11. //runButton = new QPushButton("run");
    12. //connect(runButton, SIGNAL(clicked()), this, SLOT(myFunction()));
    13. //mainLayout->addLayout(buttonLayout);
    14. //buttonLayout->addWidget(runButton);
    15. //setLayout(mainLayout);
    16. }
    17.  
    18. ////////////////////////////////////////////////////////////
    19. int main(int argc, char *argv[])
    20. {
    21. //QApplication application(argc, argv);
    22. QCoreApplication application(argc, argv);//ADD
    23. myFunction();//ADD
    24.  
    25. //Widget window;
    26. //window.show();
    27. //return application.exec();
    28.  
    29. return 0;//ADD
    30. }
    To copy to clipboard, switch view to plain text mode 
    functions.cpp
    Qt Code:
    1. #include <iostream>
    2. #include <cstring>
    3. #include <cstdlib>
    4.  
    5. using namespace std;
    6.  
    7. /////////////////////////////////////////////////////
    8. void MyWidget::myFunction()
    9. {
    10. FILE *output; if((output=fopen("output.txt","w")) == 0) {exit(1);}
    11. fprintf(output,"hello\n");
    12. fclose(output);
    13. }
    To copy to clipboard, switch view to plain text mode 
    tom.h
    Qt Code:
    1. #ifndef TOM_H
    2. #define TOM_H
    3. #include <QtGui>
    4.  
    5. class MyWidget : public QWidget
    6. {
    7. Q_OBJECT
    8. public:
    9. MyWidget (QWidget* parent = 0);
    10. void myFunction();//ADD
    11.  
    12. public slots:
    13. //void myFunction();
    14.  
    15. signals:
    16.  
    17. protected:
    18.  
    19. private:
    20. //QVBoxLayout* mainLayout;
    21. //QHBoxLayout* buttonLayout;
    22. //QPushButton* runButton;
    23. };
    24.  
    25. #endif
    To copy to clipboard, switch view to plain text mode 

    I'd be most thankful for any help!

  2. #2
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Converting GUI to CONSOLE

    1. QWidget is part of QtGui, so it can't be used in console app.
    2. You get error, as there's really no such function as myFunction() in main.cpp.
    3. In .pro file you should have
      Qt Code:
      1. QT -= gui
      2. CONFIG += console
      To copy to clipboard, switch view to plain text mode 
    4. Qt Creator can create sample console project for you
    Oleg Shparber

  3. The following user says thank you to Oleg for this useful post:

    timmu (5th December 2011)

  4. #3
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting GUI to CONSOLE

    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!

  5. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting GUI to CONSOLE

    Quote Originally Posted by timmu View Post
    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

    Quote Originally Posted by timmu View Post
    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

Similar Threads

  1. Converting UIC 2 .h & .Cpp
    By jibolso in forum Newbie
    Replies: 5
    Last Post: 5th September 2009, 13:28
  2. Converting a php program to Qt
    By srohit24 in forum Qt Programming
    Replies: 7
    Last Post: 19th March 2009, 19:33
  3. Converting C++ to Qt4
    By ComaWhite in forum Qt Programming
    Replies: 8
    Last Post: 11th July 2008, 08:33
  4. Converting QT 4 to VC++
    By vvbkumar in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2006, 13:54
  5. Converting my UI to Qt4
    By Honestmath in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2006, 23:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.