I am trying follow the instructions published here:
https://www.linux.org/threads/c-tutorial-create-qt-applications-without-qtcreator.18409/
but in a PC running Windows 10, to build a Qt application createdwith the Atom editor. I have this 3 files in my project right now:
qt_main.cpp
#include <QtWidgets>
#include "mainwidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWidget w;
w.show();
return a.exec();
}
mainwidget.h
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
class QPushButton;
class QTextBrowser;
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
private:
QPushButton* button_;
QTextBrowser* textBrowser_;
};
#endif // MAINWIDGET_H
mainwidget.cpp
#include <QtWidgets>
#include "mainwidget.h"
MainWidget::MainWidget(QWidget *parent) : QWidget(parent)
{
button_ = new QPushButton(tr("Push Me!"));
textBrowser_ = new QTextBrowser();
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(button_,0,0);
mainLayout->addWidget(textBrowser_,1,0);
setLayout(mainLayout);
setWindowTitle(tr("Connecting buttons to processes.."));
}
MainWidget::~MainWidget()
{
delete button_;
delete textBrowser_;
}
I execute this commands, in sequence:
qmake -project
add `QT += widgets` to the qt_main.pro file generated
qmake qt_main.pro
make
After that, the EXE file is generated without error, but when I try run it, I got the error:
"The procedure entry point _ZN10QArrayData10deallocateEPS_jj could not be located in the dynamic link library etc."
I try follow the suggested on the comments for the question:
QArrayData error, linking Qt libraries with CMake
and check my PATH. right now, the only directory containing qt DLLs is the one:
C:\Qt\Qt5.14.0\5.14.0\mingw73_64\bin
(this directory was createwd by the Qt offline installer).
Anyone can give a hint of how to solve this issue?
Error when attempting to build code I copy-pasted directly from the QT for beginners wiki page.
error
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Window::Window(class QWidget *)" (??0Window##QEAA#PEAVQWidget###Z) referenced in function main
debug\testempty.exe:-1: error: LNK1120: 1 unresolved externals
testempty.pro
TEMPLATE = app
TARGET = testempty
QT = core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += \
main.cpp \
window.cpp
HEADERS += \
window.h
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
class QPushButton;
class Window : public QWidget
{
public:
explicit Window(QWidget *parent = nullptr);
private:
QPushButton *m_button;
};
#endif // WINDOW_H
window.cpp
#include "window.h"
#include <QPushButton>
Window::Window(QWidget *parent) : QWidget(parent)
{
setFixedSize(100, 50);
m_button = new QPushButton("Hello World", this);
m_button->setGeometry(10, 10, 80, 30);
}
main.cpp
#include <QApplication>
#include "window.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}
As for things I have already tried, I was instructed to build->clean all and try building again, but that made no difference.
Solution From a similar thread.
Right click on project > Clean
Right click on project > Run qmake
Right click on project > Build
Run
Why it works
The reason this worked is because Run qmake updates your Makefile. For some reason qt is not automatically updating paths when you make changes to your project such as adding or removing files. Running qmake forces qt to update the paths for your project which enables it to find the mainwindow.obj file. You probably could just run qmake and your problem would be solved.
I am trying to build a basic Qt Application with the Meson Build System on my Mac (using macOS Sierra), following the tutorial on http://mesonbuild.com/samples.html.
My meson.build file looks like this:
project('qt5 demo', 'cpp',
default_options : ['cpp_std=c++11'])
qt5_dep = dependency('qt5', modules : ['Core', 'Gui', 'Widgets'])
# Import the extension module that knows how
# to invoke Qt tools.
qt5 = import('qt5')
prep = qt5.preprocess(moc_headers : 'mainWindow.h',
ui_files : 'mainWindow.ui')
executable('qt5app',
sources : ['main.cpp', 'mainWindow.cpp', prep],
dependencies : qt5_dep,
cpp_args : '-std=c++11')
I have a small test program consisting of only four files: main.cpp, mainwindow.h, mainwindow.cpp, and mainwindow.ui.
The source code is as follows.
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
The program compiles and executes as expected when I use qmake as a build system using the following qmake-file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtDesigner
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
When I execute
meson build
it works fine, except for the warning:
WARNING: rcc dependencies will not work reliably until this upstream issue is fixed:
https://bugreports.qt.io/browse/QTBUG-45460
It also compiles without errors, when I switch to the build directory and call
ninja
but when I execute the program I get the following error:
dyld: Library not loaded: #rpath/QtCore.framework/Versions/5/QtCore
Referenced from:
/Users/<myname>/code/C++/QtDesignerCode/build/./qt5app
Reason: image not found
Abort trap: 6
It seems like the linker cannot find the libraries? Which is weird, as in Qt Creator (using qmake) the source code compiles fine.
Thanks in advance for any help.
Do the following in the build dir
mesonconf -Dcpp_std=c++11
or, set the default language version in your meson.build file
I'm a newbie in Qt and not that much experienced in C++ either.
I created simple Qt GUI app, but then I had to add the mousepressevent function on a QLabel type object, so I created the class which has the header file with following code:
#ifndef IMAGEACTION_H
#define IMAGEACTION_H
#include <QLabel>
#include <QMouseEvent>
#include <QDebug>
#include <QEvent>
class imageaction : public QLabel
{
Q_OBJECT
public:
explicit imageaction(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *ev);
signals:
void Mouse_Pressed();
public slots:
};
#endif // IMAGEACTION_H
The .cpp file has the following code:
#include "imageaction.h"
imageaction::imageaction(QWidget *parent) :
QLabel(parent)
{
}
void imageaction::mousePressEvent(QMouseEvent *ev)
{
emit Mouse_Pressed();
}
In the mainwindow.cpp file added the line #include "imageaction.h" to include the header file and in the .pro file it's the following lines are also added:
SOURCES += main.cpp\
mainwindow.cpp \
imageaction.cpp
HEADERS += mainwindow.h \
imageaction.h
But the program always gives the following error:
C1083: Cannot open include file:'imageaction.h': No such file or directory.
Could you say where I'm making the mistake? To make this class I followed this video
I think, "C1083: Cannot open include file:'imageaction.h': No such file or directory" error from your ui_*.h file. If that is the case, your issue regading promoting imageaction widget.
This may work
1. while promoting imageaction widget, uncheck "globalinclude".
or
2. Update pro file with "INCLUDEPATH += path where mywidget.h"
Please check for more info Promoting Widget
This question already has answers here:
Qt Linker Error: "undefined reference to vtable" [duplicate]
(9 answers)
Closed 9 years ago.
When I uncomment the Q_OBJECT macro that I need for signal-slot I get a undefined reference to vtable for MyApp error, but without the macro it compiles perfectly but I can't use signals and slots without it. I think I may be doing something stupid wrong, but please try helping because I realy can't find the problem. O and I know my code is untidy and am working on it.
myapp.h:
#ifndef MYAPP_H
#define MYAPP_H
#include <QApplication>
#include <QEvent>
#include <QObject>
#include <QDebug>
class MyApp : public QApplication
{
public:
MyApp( int argc, char** argv );
protected:
bool eventFilter(QObject *object, QEvent *event);
signals:
void focusG();
void focusL();
};
#endif // MYAPP_H
myapp.cpp:
#include "myapp.h"
MyApp::MyApp(int argc, char **argv): QApplication(argc, argv)
{
installEventFilter(this);
}
bool MyApp::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::ApplicationDeactivate)
{
qDebug() << "Focus lost";
//focusL();
}
if (event->type() == QEvent::ApplicationActivate)
{
qDebug() << "Focus gained";
//focusG();
}
return false;
}
main.cpp:
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QObject>
#include <QGraphicsObject>
#include <QTimer>
#include <QVariant>
#include "timecontrol.h"
#include "scorecontrol.h"
#include "Retry.h"
#include <QEvent>
#include "myapp.h"
int main(int argc, char *argv[])
{
//QApplication app(argc, argv);
MyApp app(argc, argv);
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
viewer.setMainQmlFile(QLatin1String("qml/Raker/main.qml"));
viewer.showExpanded();
QObject *rootObject = viewer.rootObject();
QTimer *timmer = new QTimer;
timmer->setInterval(1000);
TimeControl *timcon = new TimeControl;
scorecontrol *scorer = new scorecontrol;
Retry *probeer = new Retry;
QObject::connect(timmer, SIGNAL(timeout()), timcon, SLOT(updateTime()));
QObject::connect(timcon, SIGNAL(setTime(QVariant)), rootObject, SLOT(setTime(QVariant)));
QObject::connect(rootObject, SIGNAL(blockClicked(int, int)), scorer, SLOT(checkRight(int, int)));
QObject::connect(scorer, SIGNAL(setScore(QVariant)), rootObject, SLOT(setScore(QVariant)));
QObject::connect(scorer, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));
QObject::connect(rootObject, SIGNAL(start()), probeer, SLOT(Reetry()));
QObject::connect(probeer, SIGNAL(start()), timmer, SLOT(start()));
QObject::connect(probeer, SIGNAL(stop()), timmer, SLOT(stop()));
QObject::connect(probeer, SIGNAL(start(int)), scorer, SLOT(randomNum(int)));
QObject::connect(probeer, SIGNAL(sReset()), timcon, SLOT(reset()));
QObject::connect(probeer, SIGNAL(tReset()), scorer, SLOT(reset()));
QObject::connect(timcon, SIGNAL(timeOut()), scorer, SLOT(reset()));
QObject::connect(timcon, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));
QObject::connect(timcon, SIGNAL(changeFinal()), scorer, SLOT(changeFinal()));
QObject::connect(scorer, SIGNAL(setFinal(QVariant)), rootObject, SLOT(setFinal(QVariant)));
QObject::connect(&app, SIGNAL(focusL()), probeer, SLOT(focusL()));
QObject::connect(&app, SIGNAL(focusG()), probeer, SLOT(focusG()));
return app.exec();
}
BlockToucher.pro:
# Add more folders to ship with the application, here
folder_01.source = qml/Raker
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01
# Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH =
symbian:TARGET.UID3 = 0x2004b49f
# Smart Installer package's UID
# This UID is from the protected range and therefore the package will
# fail to install if self-signed. By default qmake uses the unprotected
# range value if unprotected UID is defined for the application and
# 0x2002CCCF value if protected UID is given to the application
symbian:DEPLOYMENT.installer_header = 0x2002CCCF
# Allow network access on Symbian
symbian {
#TARGET.CAPABILITY += NetworkServices
vendorinfo = "%{\"Gerhard de Clercq\"}" ":\"Gerhard de Clercq\""
ICON = BlockToucher.svg
}
# If your application uses the Qt Mobility libraries, uncomment the following
# lines and add the respective components to the MOBILITY variable.
# CONFIG += mobility
# MOBILITY +=
# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp \
timecontrol.cpp \
scorecontrol.cpp \
Retry.cpp \
myapp.cpp \
myapplication.cpp
# Please do not modify the following two lines. Required for deployment.
include(qmlapplicationviewer/qmlapplicationviewer.pri)
qtcAddDeployment()
HEADERS += \
timecontrol.h \
scorecontrol.h \
Retry.h \
myapp.h \
myapplication.h
OTHER_FILES += \
qtc_packaging/debian_fremantle/rules \
qtc_packaging/debian_fremantle/README \
qtc_packaging/debian_fremantle/copyright \
qtc_packaging/debian_fremantle/control \
qtc_packaging/debian_fremantle/compat \
qtc_packaging/debian_fremantle/changelog
compat \
qtc_packaging/debian_fremantle/changelog
Q_OBJECT is used by the MOC system to produce the code you need for the signals. My best guess would be that you are not included the MOC generated files in your project (the CPP files which it generates).
How you do this greatly depends on your build system (CMake, QMake, AUtomake, MSVC) but you should refer to the tutorial with QMake first.
That is, the undefined reference to vtable is the error I sometimes get when I forget to update my Cmake files and have a Q_OBJECT.
Also, your ctor for MyApp is wrong, the signature must be:
MyApp( int & argc, char** argv );
The & is important should you ever use command-line parameters.
I had same issue when i decided to add Q_OBJECT into my header file.
First try run qmake manually.
If not resolved, so delete build folder and build project again
If building all of the project is a heavy task, so try following steps:
Add a new temp class with QObject as its base class
Double check your header and source file for any mistake using new temp class
Build your project. It should built successfully
Remove your temp class completely (.h and .cpp file and relative lines on .pro file)
Good luck :)