1

I used QT Desginer to create a finalversion_with_buttons.ui file, later i converted it to finalversion_with_buttons.h file using the command

uic -o finalversion_with_buttons.h finalversion_with_buttons.ui

in command prompt.

I got to know that we cannot have a .cpp file and .h file contains everything we need, now how do i execute/run this .h file ?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • If you are a beginner then I recommend using Qt Creator that will link all the .ui and do the other tasks for you to take care of the other tasks. – eyllanesc Feb 20 '20 at 23:44
  • okay, so if i create a new project in QTcreator and drag & drop all my widgets to finish my UI, will the corresponding related code change in .h and .cpp files of the project or they will stay the same just like when the project was created ??? – Mr_Workalot Feb 20 '20 at 23:46
  • Qt creator will generate the ui_X.h file in the build folder that will be included in the X.cpp: `#include "ui_X.h"` so any modification of the .ui only modifies the ui_X.h but not the X.h or the X.cpp – eyllanesc Feb 20 '20 at 23:48
  • If you are starting with Qt you should not be interested in how the .ui is converted to code since it will not be of benefit. Maybe when you want to delve into concepts it is necessary but if you are a beginner in Qt and as you also point out in C++ for you it will be a waste of time. – eyllanesc Feb 20 '20 at 23:51
  • These files are meant to be generated at build time by QMake (or CMake or whateve build system you use). They are meant to created manually or put into version control. When added to the .pro file like `FORMS += finalversion_with_buttons.ui`, it will create the .h and .cpp in the build folder when building the project. – Frank Osterfeld Feb 21 '20 at 14:10

2 Answers2

0

Please check Qt Creator documentation e.g. "Creating a Qt Widget Based Application". It will give you some overview how to setup qmake/CMake project based on UI form files (aka Qt Widgets). The UI files itself may not be used standalone. It is only UI description.

Ľubomír Carik
  • 387
  • 5
  • 11
0

It is always of benefit to create a ".pro" or ".cmake" file that contails all the stuff for the compilation of the project that has several benefits, even for small programms. I highly suggest reading through this sites, that helped me a lot in creating/compiling projects: https://www.cprogramming.com/tutorial/makefiles.html https://www.cprogramming.com/tutorial/makefiles_continued.html

This is what an automatic generated .pro file of the qt creator contains and I guess self explainatory:



    QT       += core gui multimedia multimediawidgets

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    CONFIG += c++11

    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS

    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

    SOURCES += \
        main.cpp \
        mainwindow.cpp

    HEADERS += \
        mainwindow.h

    FORMS += \
        mainwindow.ui

    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target

    RESOURCES += \
        mainwindow.qrc

If you are working with the qt designer part of the qt creator you got this xml .ui file and the easiest way to compile it would be just clicking on Build->Run or ctrl+R. If your .pro file looks like the above example with the right file names you should be good to go.

Actually it is possible to have just a c++ file and no header files - thats invented to make the projects more modular with classes - thats what c++ is all about compared with c. To quote Bjarne "Classes use to hide the ugly stuff" ..so you can read the programm and understand it without even knowing what the class files contain and do with your code with a proper reference, and you don't should have to care. And thats what qt does - hiding the ugly stuff you would have to do all by yourself in its classes so you can just call QPushButton and it works. (and many more benefits but to keep it simple, qt is just c++ classes)

This is an example for a class in the code without a header file:



    [//example from here https://www.cprogramming.com/tutorial/lesson12.html][1]
    #include <iostream>

    using namespace std;

    class Computer // Standard way of defining the class
    {
    public:
      // This means that all of the functions below this(and any variables)
      //  are accessible to the rest of the program.
      //  NOTE: That is a colon, NOT a semicolon...
      Computer();
      // Constructor
      ~Computer();
      // Destructor
      void setspeed ( int p );
      int readspeed();
    protected:
      // This means that all the variables under this, until a new type of
      //  restriction is placed, will only be accessible to other functions in the
      //  class.  NOTE: That is a colon, NOT a semicolon...
      int processorspeed;
    };
    // Do Not forget the trailing semi-colon

    Computer::Computer()
    {
      //Constructors can accept arguments, but this one does not
      processorspeed = 0;
    }

    Computer::~Computer()
    {
      //Destructors do not accept arguments
    }

    void Computer::setspeed ( int p )
    {
      // To define a function outside put the name of the class
      //  after the return type and then two colons, and then the name
      //  of the function.
      processorspeed = p;
    }
    int Computer::readspeed()  
    {
      // The two colons simply tell the compiler that the function is part
      //  of the class
      return processorspeed;
    }

    int main()
    {
      Computer compute;  
      // To create an 'instance' of the class, simply treat it like you would
      //  a structure.  (An instance is simply when you create an actual object
      //  from the class, as opposed to having the definition of the class)
      compute.setspeed ( 100 ); 
      // To call functions in the class, you put the name of the instance,
      //  a period, and then the function name.
      cout<< compute.readspeed();
      // See above note.
    }

And a compiler doesn't see something else after the linker is done than that. so

"I got to know that we cannot have a .cpp file and .h file contains everything we need"

is not right because you can as seen in the example above. Just its not how c++ (or c with classes as it was called in the early days) should be used.

But to answer your question:

"how do i execute/run this .h file ?"

  1. like said before, just use the qt creator and click on Run or crtl + R (it is free for opensource and edu)
  2. create a project file like exampled before and use qmake SampleProject.pro in the command line. This will create a file by the name of “Makefile” in the project directory. (like described here https://vitux.com/compiling-your-first-qt-program-in-ubuntu/ than issue the command make in the same directory (also described here)
  3. Create a make file like described in link 1 and 2.
  4. Everything else is beyond the scope of this question like fiddling out the semantic for using gcc or g++

That being said, you can create QPushButtons and all the stuff with the qt creator or you can create push buttons just in the code without using the .ui xml - file like described here: https://www.bogotobogo.com/Qt/Qt5_LayoutNotUsingDesigner.php

But what all of the guys here highly suggest is: Get yourself a goot qt/c++ book or tutorial and learn the foundations about classes and qt and you gonna get to be a really good programmer in no time. I also hope deeply this post is able to clarify a lot of qt programming/compiling for you and you start to have fun and will create really nice applications :) Cheers

Ingo Mi
  • 999
  • 12
  • 26