error: ‘QtMobility’ is not a namespace-name - qt

I am trying to compile: http://wiki.forum.nokia.com/index.php/Fetching_a_map_tile_in_Qt_using_Google_Maps
I am using Qt-Mobility 1.2 with Qt 4.7 on OpenSuse 11.2
The errors I am receiving are:
MainWindow.h:7: error: ‘QtMobility’ is not a namespace-name
MainWindow.h:7: error: expected namespace-name before ‘;’ token
MainWindow.h:10: error: expected class-name before ‘{’ token
In file included from /opt/qtsdk-2010.05/qt/include/QtCore/qcoreapplication.h:47,
from /opt/qtsdk-2010.05/qt/include/QtGui/qapplication.h:45,
from /opt/qtsdk-2010.05/qt/include/QtGui/QApplication:1,
from main.cpp:2:
/opt/qtsdk-2010.05/qt/include/QtCore/qeventloop.h:51: error: expected initializer before ‘QtCoreModule’
make: *** [main.o] Error 1
My .pro file contains:
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
HEADERS += MainWindow.h
SOURCES += main.cpp MainWindow.cpp
QT += network
CONFIG += mobility
MOBILITY = location
The error is reported in the header file, which I have shown as follows:
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QPaintEvent>
#include <QPixmap>
using namespace QtMobility;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow ();
void paintEvent(QPaintEvent* paintEvent);
public slots:
void handleNetworkData(QNetworkReply* reply);
private:
void fetchMap(const QSize& size, qreal latitude, qreal longitude);
private:
QNetworkAccessManager networkAccessManager;
QPixmap mapPixmap;
}
Please guide.

You'll need to include at least one QtMobility header to be able to use that namespace.
Also that using namespace declaration is not the recommended way anymore. Use:
QTM_USE_NAMESPACE
instead (see QtMobility QuickStart).

Related

gcc compiles my Qt class with vtable undefined

I cannot get rid of the error "Undefined reference to vtable" when compiling my Linux desktop application.
I did find the thread
Undefined reference to vtable
I have a
set(CMAKE_AUTOMOC ON)
in my CMakeLists.txt
but I receive
AutoMoc warning
---------------
"SRC:/QtGUI/BASIC/SimulatorWindowBasic.cpp"
includes the moc file "SimulatorWindowBasic.moc", but does not contain a Q_OBJECT, Q_GADGET, Q_GADGET_EXPORT, Q_NAMESPACE or Q_NAMESPACE_EXPORT macro.
despite that my definition contains
a Q_Object.
At the end of build, I receive the feared message
/usr/bin/ld: CMakeFiles/Basic_GUI.dir/Basic_GUI.cpp.o: in function `main':
/Myhome/main/BASIC/Basic_GUI.cpp:6: undefined reference to `SimulatorWindowBasic::SimulatorWindowBasic(int, char**, QWidget*)'
/usr/bin/ld: CMakeFiles/Basic_GUI.dir/Basic_GUI.cpp.o: in function `SimulatorWindowBasic::~SimulatorWindowBasic()':
What do I wrong?
Below I show my relevant sources.
(in my previous install using Qt5 and Ubuntu 20.04
I did not experience this issue. The present install uses Ubuntu 22.04 and Qt 6.4.2)
#ifndef SimulatorWindowBasic_H
#define SimulatorWindowBasic_H
#include <QMainWindow>
namespace Ui {
class SimulatorWindowBasic;
}
class SimulatorWindowBasic : public QMainWindow
{
Q_OBJECT
public:
explicit SimulatorWindowBasic(int argc, char **argv, QWidget *parent = 0);
virtual ~SimulatorWindowBasic();
protected:
Ui::SimulatorWindowBasic *ui;
};
#endif // SimulatorWindowBasic_H
and
#include "SimulatorWindowBasic.moc"
#include "SimulatorWindowBasic.h"
#include "uiSimulatorWindowBasic.h"
SimulatorWindowBasic::SimulatorWindowBasic(int argc, char **argv, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SimulatorWindowBasic)
{
ui->setupUi(this); // This creates the basic splitters
}
SimulatorWindowBasic::~SimulatorWindowBasic
{
}
You have to include "moc_SimulatorWindowBasic.cpp" because you want to have moc run on a header (and not on your source) as described in the cmake documentation

Qt class MyButton when use Q_PRIVATE_SLOT, the .h file compile error: undefined MyButtonPrivate

https://www.dvratil.cz/2019/11/q-private-slot-with-new-connect-syntax/
#include <QPushButton>
#include <memory>
class MyButtonPrivate;
class MyButton : public QPushButton {
Q_OBJECT
public:
explicit MyButton(QWidget* parent);
~MyButton() noexcept override;
private:
std::unique_ptr<MyButtonPrivate> const d_ptr;
Q_DECLARE_PRIVATE(MyButton);
Q_PRIVATE_SLOT(d_func(), void onClicked(bool));
};
this is the .h file, compile error: undefined MyButtonPrivate.
truely, the moc_MyButton.cpp(auto generated by compiler) doesnot include the MyButtonPrivate.
then what's wrong, and how to solve?
I'm not sure why it's an error, but according to https://forum.qt.io/topic/61295/solved-what-is-the-usage-of-q_private_slot/3, with Qt > 5 and C++11 it is a useless macro.
the moc file generated:
#include "../../MyButton.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
...
case 0: _t->d_func()->onClicked((*reinterpret_cast< bool(*)>(_a[1]))); break;
...
notice: _t->d_func()->onClicked invoke the private class's func(but no include)
so absolutely result in compile error. the question is moc file doesnt include MyButton_p.h, and manually add the include:
#include "../../MyButton.h"
#include "../../MyButton_p.h"
...
then its ok(but the moc file shouldnt modify manually)

Export QObject-based class to DLL

I'm composing a class which derives from QObject, and I want to export this class into a DLL file so other applications can use it. But I got some mysterious problem here:
The code is shown below:
mydll.h:
#ifndef MYDLL_H
#define MYDLL_H
#include "mydll_global.h"
#include <QObject>
#include <QDebug>
class MYDLLSHARED_EXPORT MyDll : public QObject
{
Q_OBJECT
public:
explicit MyDll(QObject * parent = 0);
void test() const;
};
#endif // MYDLL_H
mydll_global.h:
#ifndef MYDLL_GLOBAL_H
#define MYDLL_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MYDLL_LIBRARY)
# define MYDLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define MYDLLSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // MYDLL_GLOBAL_H
mydll.cpp:
#include "mydll.h"
MyDll::MyDll(QObject * parent) :
QObject(parent)
{
}
void MyDll::test() const {
qDebug() << "Hello from dll!";
}
and the dll is used in another application. The dll is compiled successfully. I've add LIBS += "myDll.dll" in the .pro file of the application using this dll, and I've copied myDll.dll to the working directory of the application.
The compiler reports:
C4273: "MyDll::qt_static_metacall" : inconsistent dll linkage.
C2491: "MyDll::staticMetaObject": definition of dllimport static data member not allowed
What's the problem here?
Your code for mydll_global.h checks whether MYDLL_LIBRARY is defined, but none of the code you have posted defines MYDLL_LIBRARY. Is this declared in a file that you have not shared on the question? If not, you need to add a #define MYDLL_LIBRARY in your build project, or your PCH.

QT the glBegin not declared?

i'm following a tutorial to write a small piece of code of opengl in qt.here's the link
http://www.youtube.com/watch?v=1nzHSkY4K18
but in the 6:13 when I bluid the code it show a couple of error that
..\testopgl\glwidget.cpp: In member function 'virtual void GLWidget::paintGL()':
..\testopgl\glwidget.cpp:17:20: error: 'glColor3f' was not declared in this scope
..\testopgl\glwidget.cpp:19:25: error: 'glBegin' was not declared in this scope
..\testopgl\glwidget.cpp:20:31: error: 'glVertex3f' was not declared in this scope
..\testopgl\glwidget.cpp:23:11: error: 'glEnd' was not declared in this scope
..\testopgl\glwidget.cpp: At global scope:
what I really don't understand is when I only put the glClear(GL_COLOR_BUFFER_BIT) it builds alright,but occurs error even i just put the glColor3f().Does the GLWidget not support the glColor*() or glBegin() command?
here's my code.
testopgl.pro
#-------------------------------------------------
#
# Project created by QtCreator 2013-03-28T09:48:44
#
#-------------------------------------------------
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testopgl
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
glwidget.cpp
HEADERS += mainwindow.h \
glwidget.h
FORMS += mainwindow.ui
glwidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void initializeGL();
void paintGL();
void resizeGL(int w,int h);
};
#endif // GLWIDGET_H
glwidget.cpp
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}
void GLWidget::initializeGL(){
glClearColor(1,1,0,1);
}
void GLWidget::paintGL(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0);
glVertex3f(0.5,-0.5,0);
glVertex3f(0.0,0.5,0);
glEnd();
}
void GLWidget::resizeGL(int w,int h){
}
The functions you mention are simply not present in any modern version of GL, so the tutorial you are following sounds like it is quite out of date.
So probably the version of GL exposed through your build of QT does not have these functions. It may be possible to reconfigure/rebuild QT to use an older version of GL, but I would instead recommend getting to know and use the modern programmable interface.

Simple QT console TCP application. What am I doing wrong?

#include <QtCore/QCoreApplication>
#include <QTCore>
#include <QtNetwork>
#include <QDebug>
#define CONNECT(sndr, sig, rcvr, slt) connect(sndr, SIGNAL(sig), rcvr, SLOT(slt))
class mynet : QObject
{
Q_OBJECT
public:
mynet()
{}
void start()
{
CONNECT(tcpServer, newConnection(), this, acceptConnection());
CONNECT(tcpClient, connected(), this, startTransfer());
CONNECT(tcpClient, bytesWritten(qint64), this, updateClientProgress(qint64));
CONNECT(tcpClient, error(QAbstractSocket::SocketError), this, displayError(QAbstractSocket::SocketError));
// start server listening
tcpServer->listen();
while(!tcpServer->isListening());
// make client connection
tcpClient->connectToHost(QHostAddress::LocalHost, tcpServer->serverPort());
}
public slots:
void acceptConnection()
{
tcpServerConnection = tcpServer->nextPendingConnection();
CONNECT(tcpServerConnection, readyRead(), this, updateServerProgress());
CONNECT(tcpServerConnection, error(QAbstractSocket::SocketError), this, displayError(QAbstractSocket));
tcpServer->close();
}
void startTransfer()
{
bytesToWrite = TotalBytes - (int)tcpClient->write(QByteArray(PayloadSize, '#'));
}
void updateServerProgress()
{
bytesReceived += (int)tcpServerConnection->bytesAvailable();
tcpServerConnection->readAll();
if (bytesReceived == TotalBytes)
{
qDebug() << "done";
tcpServerConnection->close();
}
}
void updateClientProgress(qint64 numBytes)
{
// callen when the TCP client has written some bytes
bytesWritten += (int)numBytes;
// only write more if not finished and when the Qt write buffer is below a certain size.
if (bytesToWrite > 0 && tcpClient->bytesToWrite() <= 4*PayloadSize)
bytesToWrite -= (int)tcpClient->write(QByteArray(qMin(bytesToWrite, PayloadSize), '#'));
}
void displayError(QAbstractSocket::SocketError socketError)
{
if (socketError == QTcpSocket::RemoteHostClosedError)
return;
qDebug() << tcpClient->errorString();
tcpClient->close();
tcpServer->close();
}
private:
QTcpServer* tcpServer;
QTcpSocket* tcpClient;
QTcpSocket* tcpServerConnection;
int bytesToWrite;
int bytesWritten;
int bytesReceived;
int TotalBytes;
int PayloadSize;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
mynet m1;
m1.start();
return a.exec();
}
I get an
Undefined symbols for architecture x86_64:
"vtable for mynet", referenced from:
mynet::mynet() in main.o
mynet::~mynet()in main.o.
Please advise what I am doing wrong. Can I not inline the method definitions in the class for some reason in Qt?
You need to add your class to the .pro file
HEADERS += mynet.h
SOURCES += mynet.cpp
so the meta-object compiler can scan them and work out they need moc'ing and generate the relevant stubs.
Assuming that your source file is named foo.cpp, you have to put the following line at the very end:
#include "foo.moc"
This line tells qmake and the VS Qt add-in that the file should be run via moc, and that the generated moc file should be named foo.moc.
You also have problems in the #include lines for Qt headers. I've found that the following work:
#include <QtCore/QCoreApplication>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
Make sure to add network to your .pro file. This will create the correct linking to the network library functions.
QT += core network
Two things:
1) You should publicly derive from QObject.
2) Are you moc'ing this file and then compiling and linking the output? If you include the Q_OBJECT macro and don't moc, you will get an error like that.

Resources