I am having problems using the QObject. In my code I attempt to add a object to Javascript in Qt5.
#include <QtGui>
#include <QtWebKit>
#include <QApplication>
#include <QWebView>
#include <QWebFrame>
#include <QWebPage>
#include <iostream>
#include <string>
using namespace std;
class nativeObject : public QObject {
Q_OBJECT
public:
string version;
};
int main(int argc, char** argv) {
nativeObject test;
test.version = "BETA";
QApplication app(argc, argv);
QWebView view;
QWebFrame *frame = view.page()->mainFrame();
frame->addToJavaScriptWindowObject("someNameForMyObject", &test);
view.setUrl(QUrl("http://google.com"));
view.show();
return app.exec();
}
Running the code gives the following errors:
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall nativeObject::metaObject(void)const " (?metaObject#nativeObject##UBEPBUQMetaObject##XZ)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall nativeObject::qt_metacast(char const *)" (?qt_metacast#nativeObject##UAEPAXPBD#Z)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall nativeObject::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#nativeObject##UAEHW4Call#QMetaObject##HPAPAX#Z)
release\webkit.exe:-1: error: LNK1120: 3 unresolved externals
I cannot find any "good" and relevant documentation as I am very new in programming in qt and C++. Have I declared the QObject incorrectly or is there something else I am doing wrong?
Try this:
frame->addToJavaScriptWindowObject("someNameForMyObject", &test);
It requires QObject* but you set QObject.
void QWebFrame::addToJavaScriptWindowObject(const QString & name, QObject * object, ValueOwnership own = QtOwnership)
& used here to get address of object because it is not a pointer, you can also create nativeObject as pointer:
nativeObject *test = new nativeObject;
In this case
frame->addToJavaScriptWindowObject("someNameForMyObject", test);
will be valid because test is already pointer. Note that for pointers we use -> instead of .
Related
I tried to override the mouseMoveEvent method by subclassing ChartView and I get a linking error which has something to do with the constructor of ChartView class.
class ChartView : public QChartView
{
Q_OBJECT
public:
ChartView(QChart* chart, QWidget* parent = 0);
protected:
void mouseMoveEvent(QMouseEvent* event) override;
};
ChartView::ChartView(QChart* chart, QWidget* parent)
: QChartView(chart, parent)
{
this->setMouseTracking(true);
}
void ChartView::mouseMoveEvent(QMouseEvent* event)
{
qDebug() << event->pos();
}
The error:
1>mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl ChartView::metaObject(void)const " (?metaObject#ChartView##UEBAPEBUQMetaObject##XZ)
1>mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl ChartView::qt_metacast(char const *)" (?qt_metacast#ChartView##UEAAPEAXPEBD#Z)
1>mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl ChartView::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#ChartView##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
When I remove the constructor of ChartView, the problem is gone, but I dont know why, because I also dont understand the error.
What am I doing wrong and how can I fix this problem?
Before building your application, you need to clean qmake and run it again. Then, you can rebuild the solution. Problem is fixed.
I'm trying to implement a simple server client application in QT. I'm using Visual Studio 2017.
Here's the Header File to my client class.
#pragma once
#ifndef Client_H
#define Client_H
#include <QTcpSocket>
class Client : public QObject
{ Q_OBJECT
public:
explicit Client (QObject *parent = 0);
void Connect();
signals:
public slots:
private:
QTcpSocket *socket;
};
#endif
and here's the code to Client.cpp
#include "Client.h"
Client::Client(QObject *parent) :
QObject(parent)
{
}
void Client::Connect()
{
socket = new QTcpSocket(this);
socket->connectToHost("192.168.10.10", 8016);
if (socket->waitForConnected(3000))
{
printf("COnnected");
}
else
{
printf("Not Connected");
}
}
as you can see it's pretty simple. In my Main.cpp i create an instance of the client class and then call the function Connect():
Client Test;
Test.Connect();
When building my project i get the following errors
1>Client.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Client::metaObject(void)const " (?metaObject#Client##UEBAPEBUQMetaObject##XZ)
1>Client.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl Client::qt_metacast(char const *)" (?qt_metacast#Client##UEAAPEAXPEBD#Z)
1>Client.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl Client::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#Client##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
I know that this Error means that the functions are declared but the definition is missing. I just don't know where to find these definitions and which file i have to link to get rid of these errors.
Thank you
These files are automatically generated by the moc tool. Did you set it up to run whenever your header file changed? Did you run it manually? If not, consider installing the Qt Visual Studio tools that solve this for you.
I'm having strange linkage problems with the following very simple application, which has a class inheriting both QObject and an interface class.
#include <QApplication>
#include <memory>
#include <iostream>
#include <qobject>
class IFoo {
public:
virtual ~IFoo() {}
virtual void foo()=0;
};
class Foo: public QObject, public IFoo
{
Q_OBJECT
public:
explicit Foo(QObject *parent=0): QObject(parent) { std::cout << "foo ctor" << std::endl; }
void foo() override { std::cout << "foo::foo" << std::endl; }
};
std::unique_ptr<IFoo> createFoo() { return std::unique_ptr<IFoo>(new Foo()); }
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto foo = createFoo();
return a.exec();
}
This causes the following errors:
LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Foo::metaObject(void)const " (?metaObject#Foo##UEBAPEBUQMetaObject##XZ)
LNK2001: unresolved external symbol "public: virtual void * __cdecl Foo::qt_metacast(char const *)" (?qt_metacast#Foo##UEAAPEAXPEBD#Z)
LNK2001: unresolved external symbol "public: virtual int __cdecl Foo::qt_metacall(enum QMetaObject::Call,int,void * *)" qt_metacall#Foo##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
LNK1120: 3 unresolved externals
I have obviously tried run qmake, clean and rebuild without effect. If I make the class Foo a plain C++ class by removing all Qt references, it works correctly.
What could be wrong?
As in Errors while compiling a test program using Qt I am following the book 'C++ GUI Programming with Qt 4' by Jasmin Blanchette and Mark Summerfield too. I don't have that mistake, and anyway I can't build it. It had been able to build until I started to write the constructor.
I use Qt 4.8 and MSVC 2010 via Qt add-in.
[EDIT]
Can I avoid using Qmake in Qt visual studio add-in?
[/EDIT]
My code:
finddialog.h
#ifndef FindDialog_H
#define FindDialog_H
#include <qdialog.h>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog
{
Q_OBJECT
public:
FindDialog(QWidget *parent=0);
signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrev(const QString &str, Qt::CaseSensitivity cs);
private slots:
void findClicked();
void enableFindButton(const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
#endif
finddialog.cpp
#include <QtGui>
#include <finddialog.h>
FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
label = new QLabel(tr("Find &what: "));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Serch backward"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new QPushButton(tr("CLose"));
connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableFindButton(const QString &)));
connect(findButton, SIGNAL(findClicked()), this, SLOT(clicked()));
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
}
void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
if(backwardCheckBox->isChecked())
emit findPrev(text, cs);
else emit findNext(text, cs);
}
void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
main.cpp
#include <qapplication.h>
#include <finddialog.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
}
And the output:
1>------ Build started: Project: ex1, Configuration: Release Win32 ------
1>Build started 26.03.2013 23:36:17.
1>InitializeBuildStatus:
1> Touching "Release\ex1.unsuccessfulbuild".
1>ClCompile:
1> finddialog.cpp
1> main.cpp
1> Generating Code...
1>finddialog.obj : error LNK2019: unresolved external symbol "public: static struct QMetaObject const FindDialog::staticMetaObject" (?staticMetaObject#FindDialog##2UQMetaObject##B) referenced in function "public: static class QString __cdecl FindDialog::tr(char const *,char const *)" (?tr#FindDialog##SA?AVQString##PBD0#Z)
1>finddialog.obj : error LNK2019: unresolved external symbol "protected: void __thiscall FindDialog::findNext(class QString const &,enum Qt::CaseSensitivity)" (?findNext#FindDialog##IAEXABVQString##W4CaseSensitivity#Qt###Z) referenced in function "private: void __thiscall FindDialog::findClicked(void)" (?findClicked#FindDialog##AAEXXZ)
1>finddialog.obj : error LNK2019: unresolved external symbol "protected: void __thiscall FindDialog::findPrev(class QString const &,enum Qt::CaseSensitivity)" (?findPrev#FindDialog##IAEXABVQString##W4CaseSensitivity#Qt###Z) referenced in function "private: void __thiscall FindDialog::findClicked(void)" (?findClicked#FindDialog##AAEXXZ)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall FindDialog::metaObject(void)const " (?metaObject#FindDialog##UBEPBUQMetaObject##XZ)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall FindDialog::qt_metacast(char const *)" (?qt_metacast#FindDialog##UAEPAXPBD#Z)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall FindDialog::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#FindDialog##UAEHW4Call#QMetaObject##HPAPAX#Z)
1>C:\Users\Family\Documents\Visual Studio 2010\Projects\ex1\Win32\Release\\ex1.exe : fatal error LNK1120: 6 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.21
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Thank you.
Code's been run before. It worked without any make-file. Just simple compilation in MSVC2010:
#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
window->setWindowTitle("Enter Your Age");
QSpinBox *spinBox = new QSpinBox;
QSlider *slider = new QSlider(Qt::Horizontal);
spinBox->setRange(0, 130);
slider->setRange(0, 130);
QObject::connect(spinBox, SIGNAL(valueChanged(int)),
slider, SLOT(setValue(int)));
QObject::connect(slider, SIGNAL(valueChanged(int)),
spinBox, SLOT(setValue(int)));
spinBox->setValue(35);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(spinBox);
layout->addWidget(slider);
window->setLayout(layout);
window->show();
return app.exec();
}
Maybe it is quite a beginner's question. But if I had such one, maybe it would be helpful for someone else.
Errors were there because of my using Q_OBJECT, so my app cannot work without moc. Second example worked coorectly cause there was no the macros and moc wasn't needed necessarily.
And Can I avoid writing Qmake and moc? Yes I can. Qt Visual Studio add-in makes all required files automatically. If there are some problems like
1>finddialog.obj : error LNK2019: unresolved external symbol "public: static struct QMetaObject const FindDialog::staticMetaObject" (?staticMetaObject#FindDialog##2UQMetaObject##B) referenced in function "public: static class QString __cdecl FindDialog::tr(char const *,char const *)" (?tr#FindDialog##SA?AVQString##PBD0#Z)
1>finddialog.obj : error LNK2019: unresolved external symbol "protected: void __thiscall FindDialog::findNext(class QString const &,enum Qt::CaseSensitivity)" (?findNext#FindDialog##IAEXABVQString##W4CaseSensitivity#Qt###Z) referenced in function "private: void __thiscall FindDialog::findClicked(void)" (?findClicked#FindDialog##AAEXXZ)
1>finddialog.obj : error LNK2019: unresolved external symbol "protected: void __thiscall FindDialog::findPrev(class QString const &,enum Qt::CaseSensitivity)" (?findPrev#FindDialog##IAEXABVQString##W4CaseSensitivity#Qt###Z) referenced in function "private: void __thiscall FindDialog::findClicked(void)" (?findClicked#FindDialog##AAEXXZ)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall FindDialog::metaObject(void)const " (?metaObject#FindDialog##UBEPBUQMetaObject##XZ)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall FindDialog::qt_metacast(char const *)" (?qt_metacast#FindDialog##UAEPAXPBD#Z)
1>finddialog.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall FindDialog::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#FindDialog##UAEHW4Call#QMetaObject##HPAPAX#Z)
Then reload the header file, where Q_OBJECT class was defined (just exclude the file, and add it again). New moc.cpp files will be created. Then rebuild the project.
I couldn't find any help for the Qt add-in. If someone had it, could you give the link, please.
I'm creating a QLabel subclass which adds the DoubleClickEvent to it. I have created the following, but I'm getting some strange linker errors, maybe someone can point out what I've done wrong?
//Header
#ifndef IMAGE_LABEL_H
#define IMAGE_LABEL_H
#include <QLabel>
#include <QMouseEvent>
class image_label : public QLabel
{
Q_OBJECT
public:
image_label(QWidget* parent = 0);
~image_label();
signals:
void doubleClicked();
protected:
void mouseDoubleClickEvent(QMouseEvent * e);
};
#endif
//CPP
#include "image_label.h"
#include <QMouseEvent>
image_label::image_label(QWidget* parent) : QLabel(parent)
{
}
image_label::~image_label()
{
}
void image_label::mouseDoubleClickEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
{
emit doubleClicked();
QLabel::mouseDoubleClickEvent(e);
}
}
I get the following linker errors when I compile:
image_label.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall image_label::metaObject(void)const " (?metaObject#image_label##UBEPBUQMetaObject##XZ)
image_label.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall image_label::qt_metacast(char const *)" (?qt_metacast#image_label##UAEPAXPBD#Z)
image_label.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall image_label::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#image_label##UAEHW4Call#QMetaObject##HPAPAX#Z)
image_label.obj : error LNK2019: unresolved external symbol "protected: void __thiscall image_label::doubleClicked(void)" (?doubleClicked#image_label##IAEXXZ) referenced in function "protected: virtual void __thiscall image_label::mouseDoubleClickEvent(class QMouseEvent *)" (?mouseDoubleClickEvent#image_label##MAEXPAVQMouseEvent###Z)
Can anybody help why I get these errors?
You must run the MOC preprocessor on the file image_label.h. This generates a file moc_image_label.cppthat you must include in the build. The error message indicates that you have not done this. (The symbols image_label::metaObject etc. that are mentioned in the error message are defined in moc_image_label.cpp.)