Qt cannot include ui header file - qt

I am try to build a simple qt project. For the project, the source file main.cpp is created using a general c++ editor, and the .ui file is created using qt design form, and they both are placed in the same directory. Then a .pro file is created, which reference both the main.cpp and .ui file.
The .pro file is here:
TEMPLATE = app
TARGET = gotocell
SOURCES += main.cpp
FORMS += gotocelldialog.ui
QT += widgets
And the main.cpp is:
#include <QApplication>
#include <QDialog>
#include "ui_gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
dialog->show();
return app.exec();
}
This follows the example code: gotocell1
But this line of codes
#include "ui_gotocelldialog.h"
causes the error: no such file or directory.
I am using QtCreator4.5. It is said that QtCreator will automatically generate the header files for the created ui how to create header file. What is going wrong here?

The generated source file is created and stored within your build folder. It does not get added to your project tree view as part of QtCreator. You can find your build folder directory by looking under Tools->Options->Build&Run.
I usually add the generated files to my own project tree view as I usually like to inspect it, but since is auto-generated from the ui file, it is not committed to version control.

Related

How to install Box2D-qml with mingw32

My environment:
Qt 5.3.1 + Mingw32
Windows 7
I tired these:
Extract box2d-qml in C:\Qt\Qt5.3.1\5.3\mingw482_32\imports and rename it as Box2D
Open cmd and cd into Box2D's directory .
Input qmake in cmd, no output
Input mingw32-make, no error output.
Input mingw32-make install, no error output. and Box2D.2.0 was generated under C:\Qt\Qt5.3.1\5.3\mingw482_32\qml directory.
Then i create empty quick project and add import Box2D 2.0 into qml.
Moving the mouse cursor on import Box2D 2.0,QtCreator pop up a message:
But when i run it got error:
QQmlApplicationEngine failed to load component
qrc:///main.qml:3 plugin cannot be loaded for module "Box2D": ?v???O?C?? 'C:/Qt/Qt5.3.1/5.3/mingw482_32/qml/Box2D.2.0/Box2D.dll' ????? Qt ?????????????C?u???????g?p?????????B (?f?o?b?N???????[?X?????C?u?????????g?p???邱??????????)
The main Cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
You have 2 options to use the plugin:
Installing into $QTPATH/qml (or $QTPATH/imports) folder as system-wide QML plugin. In this case you just have to do next steps:
download the source from Github and open it with QtCreator (don't put it into system folder, put the source into some user folder)
Go to Projects tab / Run / Add deploy step
add install to Make arguments
go to Edit tab, right click on the project and select Run qmake
now build the project (don't forget to set Release profile)
right click on the project and select Deploy;
The plugin dll (include all you need, Box2D code etc) will be installed into qt folder.
.
Compiling the plugin as part of your project. In this case you just have to do next steps:
download the source from Github and put the plugin source folder into your project folder (myproject/qml-box2d/, for example)
add the line into your .pro file: include(qml-box2d/box2d_lib.pri)
add this code into your main.cpp file:
#include <box2dplugin.h> // <-- this line
int main(int argc, char *argv[])
{
...
Box2DPlugin plugin; // and these
plugin.registerTypes("Box2D"); // 2 lines
...
}
Rerun qmake and recomplile the project. Now you can use Box2D items in your QML files.

Why doesn't qmake add "QT += widgets" to *.pro files automatically?

I am trying to build a simple "Hello World" example in QT5.
I am compiling the code using the below steps
qmake-qt5 -project
qmake-qt5
make
I am getting the below error
main.cpp:1:24: fatal error: QApplication: No such file or directory
#include <QApplication>
^
compilation terminated.
When I read QT forums, its mentioned to add "QT += widgets" to the *.pro file. After doing this the code is compiled.
Question: Why do I need to add "QT += widgets" to the *.pro file manually ? Why doesn't qmake do it automatically ?
Note: I am using Ubuntu
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();
}
The QT variable in the .pro file is used to specify the modules which are used in the project.
And as the qmake manual mentions:
By default, QT contains both core and gui, ensuring that standard GUI applications can be built without further configuration.
The Qt Widgets module is not linked by default and has thus to be specified in the .pro file with QT += widgets.

Qt 5.3.1: Why do I need to set include path and libs for widgets

I'm on Windows 8.1. I want to build Qt apps from the command line like the Unix trooper I usually am. Well, actually, I want things to be a little more automated! After wondering why qmake would not generate the proper Makefiles and hand-editing them, I finally realized I could just add on to the Qt variable in the Qt .pro file. (Been since about Qt 3-something that I've Qt'ed, so finally the light-bulb went on :)
This is the simplest of applications to help refresh the dead brain cells.
#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();
}
The .pro file:
######################################################################
# Automatically generated by qmake (3.0) Sat Aug 23 16:38:04 2014
######################################################################
TEMPLATE = app
TARGET = HelloQt
INCLUDEPATH += .
#why do I need these
INCLUDEPATH += "C:\Qt\Qt5.3.1\5.3\msvc2013_64\include\qtwidgets"
LIBS += "C:\Qt\Qt5.3.1\5.3\msvc2013_64\lib\Qt5widgets.lib"
#why does the following line do nothing
qmake: Qt += widgets
CONFIG -= X86
# Input
SOURCES += hello.cpp
Why do I need to specify the widgets INCLUDEPATH and LIBS to get a successful build?
As far as I can tell, Qt += widgets is not a valid addition to the Qt variable.
Why not? If I don't specify the Qt5Widgets include and libs, and I run nmake, I get errors related to not finding the include for QApplication and/or linker errors.
With those two lines in the .pro file everything works fine. Has anybody run into this sort of thing, am I missing something?
The line should look more like QT += core gui widgets. This will set the INCLUDEPATH and LIBS.

How to create Qt GUI application from dll called by win32 console application?

(I am not native English so Sorry if I make any grammar mistake)
I am new to Qt, started 2 days ago, and had only few experiences of WINAPI and MFC.
I am thinking of...
First, creating Qt GUI application from its dll
Second, my static library file(*.lib) will call this dll file using QLibrary.
Lastly, my console application will have its lib and its header file, and dll to create Qt GUI Application.
and my console application does not have .pro file, just created on visual studio and has only .vcproj and .sln file.
Here is the source code:
http://cfile239.uf.daum.net/attach/037B654151C4FF8D2D0EB7
I copied dll, lib and its header file into its win32 console application.
and when I compile console, I get this error message.
fatal error C1083: Cannot open include file: 'QApplication': No such file or directory
I know it is absolutely right. (cuz my lib uses QLibrary and its lib and header file is included in win32 console application..)
well, actually I do not want to have .pro file including QApplication class into my console application to fix this problem.
Is there any possible way to fix it while avoiding having .pro file?
or should I create .pro and set it to have QT library?
Thank you for reading it. :D
You should indicate your compiler Qt's path.
If you use vs,choose tools->option->project and solutions,VC++ Directories,set your Qt header files path,lib path,bin path, and set Qt's bin path to environment variable PATH.
In your static library project, try this:
create.h
#pragma once
void createQt(int argc, char* argv[]);
create.cpp
#include<QtGui/QWidget>
#include<QtGui/QApplication>
#include "create.h"
#pragma comment(lib,"qtguid4.lib")
void createQt(int argc, char* argv[])
{
QApplication app(argc,argv);
QWidget w;
w.show();
app.exec();
};
in you console program:
.cpp file:
#include "stdafx.h"
#include "create.h"
#pragma comment(lib,"CreaeQt.lib")
int main(int argc, char* argv[])
{
createQt(argc,argv);
return 0;
}

QTranslator doesn't work

#include<QApplication>
#include<QTranslator>
#include<QObject>
#include<QTextCodec>
#include<QWidget>
int main(int argc, char* argv[])
{
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
QApplication app(argc, argv);
QTranslator translator;
translator.load("app_zh_CN.qm");
app.installTranslator(&translator);
QWidget widget;
widget.setWindowTitle(QObject::tr("Hello World!"));
widget.show();
return app.exec();
}
SOURCES += \
main.cpp
TRANSLATIONS += app_zh_CN.ts
The Gui interface is "Hello World!" also.. But in my file.qm is be translate to "你好!"(chinese)...
where is the preblem ? who can help me..
Your example works for me if I put the .qm file in the "correct" spot. (See below.) Make sure you are doing all the steps:
Run lupdate to create the .ts file.
Do your translation in Linguist and save the .ts file.
Run lrelease to compile the .ts file to a .qm file.
Copy the .qm file to the correct location.
My guess is that #4 is going bad. The documentation for QTranslator::load states:
If directory is not specified, the directory of the application's
executable is used (i.e., as applicationDirPath()).
However, I had to put the .qm file in the folder above the executable to get it to work as is. Unless I'm misunderstanding the docs, this is a Qt bug, but one that is simple to workaround. If I explicitly gave the directory as app.applicationDirPath, it worked in the executable folder. You could also specify a separate directory. For example:
translator.load("app_zh_CN.qm"); works with:
[MyApp]
app_zh_CN.qm
[debug]
MyApp.exe
translator.load("app_zh_CN.qm", app.applicationDirPath()); works with:
[MyApp]
[debug]
app_zh_CN.qm
MyApp.exe

Resources