Error in template generated by Qt in Visual Studio - c++

So I've been trying to play around with Qt in Visual Studio. However, when I create a GUI application, the automatically generated template already contains a compiler error, namely:
Error (active) E1696 cannot open source file "ui_QtGuiApplication1.h" QtGuiApplication1 E:\visual_studio_projects\gui_test\QtGuiApplication1\QtGuiApplication1.h 4
I couldn't locate the ui_QtGuiApplication1.h header anywhere in the solution.
Removing the include statement results in the Ui class not being found. As far as I can tell, the Ui class is supposed to be located in ui_QtGuiApplication1.h.
QtGuiApplication1.h:
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplication1Class ui;
};
QtGuiApplication1.cpp:
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
Any help is appreciated.
EDIT: The ui_QtGuiApplication1.h was generated automatically by uic after the corresponding .ui file is saved in Qt Designer.

The file #include "ui_QtGuiApplication1.h is generated from your "QtGuiApplication1.ui" Designer file. Seems, that you didn't call the User Interface Compiler Tool (uic).

Related

Cannot open source file "ui_QtGuiApplication.h" in default Qt project

I installed the latest QT version and the QT tool for Visual Studio. When creating a new GUI project in Qt, the default program should create an empty window, but it seems that I'm getting an E1696 error in Visual Studio Community: "cannot open source file "ui_QtGuiApplication4.h".
The error takes place in the default header class created for the project:
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication4.h"
class QtGuiApplication4 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication4(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplication4Class ui;
};
The Ui tag is also not recognized.
I added the additional include directiories for the QT path and I tried switching up between the 64bit and 32bit version of QT, but I'm getting the same error.
Any help would be appreciated!
The ui_<YourClass>.h header file is a file that only gets generated for you when you compile your project. Qt's User Interface Compiler uic will read your .ui file(s) and create the corresponding ui_ headers before the actual C++ compiler is invoked.
So you have to give it at least one compile run to generate the file. Then your IDE should be smart enough to find it.
FYI: Using QtCreator 4.12 you won't even have to compile. The clang backend process will generate the file in a temp folder somewhere probably to give you the proper code inspections (regarding code completion and so on). As I read from your question you're using Visual Studio which doesn't seem to do that.

Qt 5.6 how to use QWebEngineView in my custom widget plugin?

I'm trying to create a Qt custom widget plugin to wrap a QWebEngineView. But I found QWebEngineView seems does not work with Qt Designer.
The demo code is attached as below. After build and place this plugin in Qt plugins folder, then Qt Designer will not launch correctly (No GUI window).
If I remove the line m_web = new QWebEngineView();, then the plugin can be loaded by Qt Designer correctly.
How to solve this issue?
#define WEBVIEWWRAPPER_H
#include <QWidget>
#include <QWebEngineView>
class WebViewWrapper : public QWidget
{
Q_OBJECT
public:
WebViewWrapper(QWidget *parent = 0);
private:
QWebEngineView* m_web;
};
#endif
// webviewwrapper.cpp
#include "webviewwrapper.h"
WebViewWrapper::WebViewWrapper(QWidget *parent) :
QWidget(parent)
{
m_web = new QWebEngineView(); // if I remove this line, the plugin will be loaded correctly
}

Error linking Qt based app

We're using a really old version of Qt, 1.1, I think. Circa 2000. It's for in-house use only, so there's little concern to upgrade at this time. Program is built on Windows using Visual Studio 2005. I know very little of Qt, other than what I've been researching over the last couple days.
The (very basic) architecture is:
main() creates a QApplication instance.
main() also creates a pointer to a custom subclass of QWidget called Wizard.
Wizard creates a number of Controller objects, which are subclassed from QThread.
I am trying to implement a new class / thread, launched from main(), the purpose of which is to monitor a service and signal an action to be carried out in the Controller objects / threads.
My new class / Thread definition:
#include "qthread.h"
class ServiceMonitor : public QThread
{
Q_OBJECT
public:
ServiceMonitor(int p) : port(p) {}
~ServiceMonitor() {};
private:
void run();
void TerminateProgram();
signals:
void SomethingBadHappened();
private:
int port;
};
And in my cpp file:
void ServiceMonitor::TerminateProgram()
{
...
emit SomethingBadHappened();
...
}
When I compile the app, I receive a linker error:
error LNK2019: unresolved external symbol "protected: void __thiscall ServiceMonitor::SomethingBadHappened(void)"
(?SomethingBadHappened#ServiceMonitor##IAEXXZ) referenced in function "private: void __thiscall ServiceMonitor::TerminateProgram(void)"
(?TerminateProgram#ServiceMonitor##AAEXXZ) ServiceMonitor.obj
I notice that all of our other objects (which have signals) are not derived from Qthread, so I have no examples to follow. The rest (which do use signals) are derived from QWidget or QObject). I notice those use the moc.exe in a custom compile step to generate an output file which is included in the project. I did try to run moc on the header containing the class above and including the output file, where I received:
Error 1 error C2039: 'className' : is not a member of 'QThread'
Error 2 error C3861: 'badSuperclassWarning': identifier not found
Error 3 error C2039: 'staticMetaObject' : is not a member of 'QThread'
Error 4 error C3861: 'activate_signal': identifier not found
What am I doing wrong?
EDIT:
Tried RA's proposal, worked a treat (Once I remembered to include qobject.h). Thanks!
New definition:
#include "qthread.h"
#include "qobject.h"
class ServiceMonitor : public QObject, public QThread
...
For versions of Qt prior to Qt 4.0, the QThread class did not inherit from QObject. As such, if you want to create a class derived from QThread that has signals, you must inherit from both QObject and QThread:
#include "qobject.h"
#include "qthread.h"
class ServiceMonitor : public QObject, public QThread
{
Q_OBJECT
// Rest of class
};
Note that QObject must be listed as the first class derived from.
Also remember to run moc on the class and to compile the generated moc code.
The missing part is the implementation of all the signals, and of the staticMetaObject structure that's declared as part of Q_OBJECT macro. Those are generated by moc. Moc-ing of the headers would normally be handled by a Qt add-in for Visual Studio. Unfortunately, there are no Qt-5 add-ins for VS 2005.
Your options are, in order of decreasing desirability.
Use qmake to generate a VS 2005 project file that invokes moc for you, and includes the necessary files. That would be the best way for you to use Qt.
Manually run moc on all of the header files that contain the Q_OBJECT macro, and add the generated code to your project.
Upgrade to at least VS 2008 (not Express), so that you can use the Qt 5 add-in.
Try Qt 4, which has an add-in for VS 2005.
Since you can have multiple versions of Qt installed at the same time, you can pursue multiple approaches in parallel. For example, #1 and #4.

Why is the Q_OBJECT macro causing issues (Qt)?

I am running QtCreator in OSX Lion, and anytime I create a class that requires the Q_OBJECT macro, I get an error when I try to build my application. The code for that class is below, as is the error I am recieving. Any clue what may be going on?
Note: I have already tried cleaning, running qmake and re-building to no avail.
#ifndef TASKLIST_H
#define TASKLIST_H
#include <QObject>
class TaskList : public QObject
{
Q_OBJECT
public:
explicit TaskList(QObject *parent = 0 );
public slots:
void addTask();
void displayTasks();
};
#endif // TASKLIST_H
And the error:
:-1: error: symbol(s) not found for architecture x86_64
:-1: error: collect2: ld returned 1 exit status
There still seems to be a bug in Qt Creator.
I have a large project with a number of classes all having Q_OBJECT and another number of classes not having Q_OBJECT. It works fine. However, if I add Q_OBJECT to one of the classes which didn't have it, I get this "collect2: ld returned 1 exit status" error when trying to build it.
Checking the build directory, I see that the moc file for this class is missing. Qt just does not create the moc files for it! However, if I remove the header and cpp files from the project and add them again, it works, the moc files are generated and the project is built successfully.
This problem seems to happen only if I have a class which did not have Q_OBJECT and it was built succesfully in the past. A fresh class with Q_OBJECT that was never compiled before adding "Q_OBJECT" always works fine.
So, if this problem happens and you are sure you included everything correctly (and commenting out Q_OBJECT lets the project to be built correctly), do the following:
remove the .h and .cpp files (where you just added the Q_OBJECT) from the project.
add them to the project again
clean project
build it again.
EDIT
In some cases running qmake (Build/Run qmake) followed by a Clean All is enough.
tasklist.h file
#ifndef TASKLIST_H
#define TASKLIST_H
#include <QObject>
class TaskList : public QObject
{
Q_OBJECT
public:
explicit TaskList(QObject *parent = 0 );
public slots:
void addTask(){};
void displayTasks(){};
};
#endif // TASKLIST_H
tasklist.cpp
#include "tasklist.h"
TaskList::TaskList(QObject *parent) :
QObject(parent)
{
}
main.cpp
#include <QtCore/QCoreApplication>
#include "tasklist.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TaskList t;
return a.exec();
}
works fine, but it should be in separate files

C++ Qt Multiple Definitions

I'm trying to create a simple GUI application (so far) in Qt with C++ using the MinGW compiler. However, the compiler is informing me that I have a multiple definition of 'WiimoteScouter::WiimoteScouter(QWidget*)' on line 4 of wiimotescouter.cpp. I am using a check to make sure the header isn't included multiple times, but apparently it's not working, and I'm not sure why.
Here's the header file:
#ifndef WIIMOTESCOUTER_H
#define WIIMOTESCOUTER_H
#include <QWidget>
class QLabel;
class QLineEdit;
class QTextEdit;
class WiimoteScouter : public QWidget
{
Q_OBJECT
public:
WiimoteScouter(QWidget *parent = 0);
private:
QLineEdit *eventLine;
};
#endif // WIIMOTESCOUTER_H
And here's the cpp file:
#include <QtGui>
#include "wiimotescouter.h"
WiimoteScouter::WiimoteScouter(QWidget *parent) :
QWidget(parent)
{
QLabel *eventLabel = new QLabel(tr("Event:"));
eventLine = new QLineEdit;
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(eventLabel, 0, 0);
mainLayout->addWidget(eventLine, 0, 1);
setLayout(mainLayout);
setWindowTitle(tr("Wiimote Alliance Scouter"));
}
Lastly, the main.cpp:
#include <QtGui>
#include "wiimotescouter.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
WiimoteScouter wiimoteScouter;
wiimoteScouter.show();
return app.exec();
}
I've seen this happen before when a source file got duplicated in the project (.pro or .pri) file. Check all of the "SOURCES =" and "SOURCES +=" lines in your project file and make sure the cpp file is not in there more than once.
I don't use MinGW but this sounds like a linker error rather than a compiler error. If this is the case then you should check that the .CPP file is not added to the project twice. I also noticed that the extension is "php", this is very unusual as it should be "cpp".
Answer just for reference:
I was including
#include myclass.cpp
instead of
#include myclass.h
This can also happen if you have two .ui files with the same name in different folders. Their corresponding headers are built in the same directory, resulting in one being overwritten. At least that was my problem.
I got this error message when I had my slot declarations listed listed under the signals heading in the header file, rather than the slots one. Another thing for anyone experiencing this error message to check for.
Cut and paste solved the problem and a need to check next time I create Slots manually.
For me it was due to Qt's compilation model in Windows using MinGW.
My code compiled perfectly fine for Linux, but for Windows the linker errors were happening for following files:
Message.cpp
Util.cpp
At first, in the .pro file, I couldn't find any similar file names. Then observing keenly I figured out that, the external google protobuf library, which I was compiling along, had some library files inside its folder named as:
message.cc
util.cc
The cases and the extensions were different, but somehow it created mess in the Qt compilation. I just added an underscore to those library files and the things worked fine.
In my case this was caused by having the function declared globally in a header file.