Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with creating first project and the book "C++-gui-programming-with-qt-4-2ndedition"
QtWS25 Last Chance

Problem with creating first project and the book "C++-gui-programming-with-qt-4-2ndedition"

Scheduled Pinned Locked Moved General and Desktop
17 Posts 5 Posters 5.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kurdman
    wrote on last edited by
    #1

    Hi all,

    I installed "qt-opensource-windows-x86-mingw482_opengl-5.3.0" on my Windows 7 machine. For learning Qt I chose that book (on the title) and started with it.
    In getting started, page 10 of that book, the below code is written:

    @
    #include <QApplication>
    #include <QLabel>

    int main (int argc, char argv[])
    {
    QApplication app (argc, argv);
    QLabel
    label = new QLabel ("Hello Qt!");
    label ->show();
    return app.exec();
    }@

    But I don't know how to make that window for writing codes. How to make that project, that is, from what menus, step-by-step.
    I also read the Appendixes A & B of the book but those just confuse me more.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      That book is old, it was written before Qt Creator existed.

      You can start by running File -> New file or project... -> Applications -> Qt Widget Application.

      It will create some basic project structure for you, with main.cpp already in place. Alternatively, you can go for "Empty Qt project" and manually add .pro file code and main.cpp file.

      (Z(:^

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kurdman
        wrote on last edited by
        #3

        I'm really confused.
        I made an Empty Qtproject and wrote this code:

        @#include <QApplication>
        #include <QLabel>

        int main(int argc,char *argv[])
        {
        QApplication app(argc, argv);
        QLabel *label = new QLabel("Hello Qt!");
        label->show();
        return app.exec();
        }@

        But I get 4 issues!
        They are as follows.What is the problem please?

        C:\Qt\Qt5.3.0\Tools\QtCreator\bin\untitled\untitled.pro:4: error: Extra characters after test expression.
        C:\Qt\Qt5.3.0\Tools\QtCreator\bin\untitled\untitled.pro:6: error: Extra characters after test expression.
        C:\Qt\Qt5.3.0\Tools\QtCreator\bin\untitled\untitled.pro:7: error: Assignment needs exactly one word on the left hand side.
        C:\Qt\Qt5.3.0\Tools\QtCreator\bin\untitled\untitled.pro:9: error: Extra characters after test expression.

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hi,
          The errors are in your project file!! (untitled.pro) with the lines 4, 6, 7, 9
          Show that piece of code and we might solve the problem.
          Also, creating a project in your Qt folder is IMHO not a smart thing to do.
          When using a QLabel you should have generated a new->QWidget project like sierdzio said ;-)

          Greetz, Jeroen

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Put C++ code (the whole snippet above) into main.cpp file.

            In .pro file, type this:
            @
            QT += core gui widgets

            SOURCES += main.cpp
            @

            Should work.

            (Z(:^

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kurdman
              wrote on last edited by
              #6

              I made a Qt Widget Application like what "sierdzio" has said, named "test.pro".
              These are the things which are written on it be default:

              @#-------------------------------------------------

              Project created by QtCreator 2014-06-11T11:37:42

              #-------------------------------------------------

              QT += core gui

              greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

              TARGET = test
              TEMPLATE = app

              SOURCES += main.cpp
              mainwindow.cpp

              HEADERS += mainwindow.h

              FORMS += mainwindow.ui
              @

              Now where to put that simple snip code please?

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                Hi,
                When you generate a "empty" widget project, you get a project file (test.pro) a main file (main.cpp) and your first class declaration (MainWindow.cpp) if you selected to add a mainwindow widget to the project.
                Your main function in main.cpp should look like this:
                @
                int main(int argc,char *argv[])
                {
                QApplication app(argc, argv);
                QLabel *label = new QLabel("Hello Qt!");
                label->show();
                return app.exec();
                }
                @
                This is to generate a QApplication which you need 1 off in every program (or a QCoreApplication etc if no widgets are used etc). The QApplication handles the Event loop and a hole lot of other stuff.
                Your MAinWindow class is not used for now, but that will come later.

                Greetz, Jeroen

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Kurdman
                  wrote on last edited by
                  #8

                  Completely mixed up!

                  Please read the thread from beginning. My question is very simple. I gave the first example of the book and said how to make a project and put that snip code into that and run it.
                  I should first solve this problem and then be familiar with other parts of Qt.
                  This is the window of test.pro which I created it:
                  http://tinypic.com/view.php?pic=5ltrgj&s=8#.U5hiG3KSzis

                  1 Reply Last reply
                  0
                  • JeroentjehomeJ Offline
                    JeroentjehomeJ Offline
                    Jeroentjehome
                    wrote on last edited by
                    #9

                    No, I'm not mixed up!
                    My explanation is very basic and noob proof.

                    Don't change the pro file! Leave it alone, this comes later. The pro file is used by QMake (you will learn later) how to compile.

                    Then open the source folder (you know the little triangle in front of it). Then double click the main.cpp file (this will open it in creator). Then place your code there in the main function and delete the MainWindow creation for now.
                    You should get a window with only a button into it. It can't get any more simple then that!

                    Greetz, Jeroen

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      Kurdman
                      wrote on last edited by
                      #10

                      After much attempt I could to run the code. It worked. Please look at my code window screen in below link. Is everything fine to you?
                      http://tinypic.com/r/1265n3k/8

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        clochydd
                        wrote on last edited by
                        #11

                        That looks fine and you should no get that window with one button.
                        And you now have a way to work through the book using the Creator.

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          Kurdman
                          wrote on last edited by
                          #12

                          OK mate. Thank you very much for assigning your time for helping me.

                          I try to read that book carefully but it doesn't seem to be clear for beginners (for example now I should try to solve this: From a command prompt, change the directory to "test" and type qmake -project).

                          Thanks also to all of other guys that helped me.

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            clochydd
                            wrote on last edited by
                            #13

                            If you want to know how to build projects without the IDE, working from the command prompt would help.
                            But it is not necessary as you may work completely from the Creator and the Qt Creator runs qmake and make for you.

                            I think it is now helpful, if you try one of the examples supplied with Creator and then step back to the book and merge the code from the book to the IDE.

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              Kurdman
                              wrote on last edited by
                              #14

                              I'm sorry, I don't know at all how to try one of those examples. And I wrote that qmake and command prompt because they exist in the book and I just quoted them from the book.
                              Since I read that not clear book I have to follow what it says:(.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andreyc
                                wrote on last edited by
                                #15

                                I started to learn Qt from "this":http://qt-project.org/doc/qt-5/tutorials-addressbook.html tutorial.
                                Try it.

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  clochydd
                                  wrote on last edited by
                                  #16

                                  That is a good tutorial and you may find the Qt Examples here:
                                  Qt Creator -> Welcome -> Examples
                                  Happy coding :)

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    Kurdman
                                    wrote on last edited by
                                    #17

                                    Thank you all very much. I try to read that tut. You are awesome guys* ;)*

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved