I have already posted this in the OSG mailing list, but the mailing list seems to be a bit slow.
Anyway, I'm trying to modify the osgViewerQt example by adding a new class of my
own that will contain the viewer. The design is:
wrapper.h: Defines class Wrapper. It inherits from
QMainWindow and has a
QDockWidget where the ViewerWidget will be attached.
viewer.h: Defines ViewerWidget class. It's the class from the
example, with a few mods by me.
prueba_qt.cpp: Main function and where a QApplication is created. A Wrapper object is
created here.
The project compiles, but when I execute it, I get an error:
QWidget: Must construct a QApplication before a QPaintDevice*
If I remove the Q_OBJECT line, the signal and the slot from
wrapper.h and compile the files from the terminal using
g++ -IE:/osg-3.0.1/install/include -LE:/osg-3.0.1/install/bin -IC:/Qt64/4.8/include -LC:/Qt64/4.8/bin -losgViewer -lOpenThreads -losgDB -losg -losgGA -losgQt -lQtCore4 -lQtGui4 prueba_qt.cpp
I can execute the app.
Can you please tell me what can I do to make this work? I've struggling all
morning but couldn't find the solution.
Thanks for your time!
PS: SO is Windows 7 64 bits # MingW compiler # Qt 4.8 # OSG 3.0.1
PS2: Here're the files I used in this project, including the pro file from qmake:
wrapper.h
#ifndef Wrapper_hpp
#define Wrapper_hpp
#include "viewer.h"
#include <QtGui/QMainWindow>
#include <QtGui/QDockWidget>
class Wrapper: public QMainWindow {
Q_OBJECT
private:
ViewerWidget* view;
QDockWidget* dock;
public:
Wrapper(void) {
view = new ViewerWidget();
dock = new QDockWidget;
dock->setWidget( view );
dock->setGeometry( 100, 100, 800, 600 );
dock->setAllowedAreas(Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, dock);
dock->show();
}
void Do(void) { view->Do(); }
void Add(void) { view->Add(); }
virtual ~Wrapper(void) {}
public slots:
void MySlot(void) {}
signals:
void MySignal(void);
};
#endif
wrapper.cpp
(This exists only because I read in the Qt forum that moc can only parse cpp files and thus one is needed for the signal/slot mechanism.)
#include "wrapper.h"
Wrapper::Wrapper(void) {
view = new ViewerWidget();
// view->setGeometry( 100, 100, 800, 600 );
dock = new QDockWidget;
dock->setWidget( view );
dock->setGeometry( 100, 100, 800, 600 );
dock->setAllowedAreas(Qt::RightDockWidgetArea);
addDockWidget(Qt::RightDockWidgetArea, dock);
dock->show();
}
prueba_qt.cpp
#include <QtGui/QApplication>
#include <iostream>
#include "wrapper.h"
int main( int argc, char** argv ) {
osg::ArgumentParser arguments(&argc, argv);
QApplication app(argc, argv);
Wrapper wrap;
wrap.resize(800,600);
wrap.setWindowTitle("Cow");
wrap.showNormal();
wrap.Do();
return app.exec();
}
prueba_qt.pro
######################################################################
# Automatically generated by qmake (2.01a) mar 12. mar 13:45:28 2013
######################################################################
QT += core gui
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += . E:/osg-3.0.1/install/include
LIBS += -LE:/osg-3.0.1/install/bin -losg -lOpenThreads -losgDB -losgGA -losgQt -losgViewer
# Input
HEADERS += viewer.h wrapper.h
SOURCES += prueba_qt.cpp wrapper.cpp
viewer.h: This is quite big, so I uploaded it to pastebin
EDIT #1
I have set OSG_NOTIFY_LEVEL to DEBUG_INFO and got this humongous output. The line with the error is:
FindFileInPath() : trying C:\cygwin\bin\osgPlugQWidget: Must construct a QApplication before a QPaintDevice
EDIT #2
The signal and slot were missing in the code. I have just added them to wrapper.h along with the call to Q_OBJECT.
After a little nap, I revised again the libraries used in the project. The problem was that some of those libraries were in debug mode and some in release mode. When using signals and slots, moc went crazy.
After building OSG debug libraries, I tried again my little example and worked.
So it's done!
Related
I have a painful problem, that I wasted a lot of time for. I need help, because I don't have any idea already.
Here it is. I'm writing qt quick application using qml in Qt Creator on Windows 8.1. Created my own C++ class with name "one". Registrated it via:
qmlRegisterType<One>("OneClass", 1, 0, "One");
In qml file I imported it:
import OneClass 1.0
By now everything worked very well. But then I decided to create shared library, where I put my "One" C++ class. Created separated project as New project->Library->C++ Library. Built library with name "mainlib", everything was fine. Connected this library to my application by adding strings in .pro file:
DEPENDPATH += ../lib/mainlib
INCLUDEPATH += ../lib/mainlib
LIBS += -L../lib/build-mainlib-Desktop_Qt_5_7_0_MinGW_32bit-Release/release -lmainlib
Run project and that's moment when I've got this problem:
QQmlApplicationEngine failed to load component
qrc:/main.qml:18 Cannot assign object to list property "data"
ASSERT: "!isEmpty()" in file C:\Program >Files\Qt\5.7\mingw53_32\include/QtCore/qlist.h, line 341
Piece of my "one.h" file:
#ifndef ONE_H
#define ONE_H
#include "mainlib_global.h"
#include <QObject>
class MAINLIBSHARED_EXPORT One : public QObject
{
Q_OBJECT
Q_PROPERTY(QString port_number READ get_port_number WRITE set_port_number)
Q_PROPERTY(QString port_speed READ get_port_speed WRITE set_port_speed)
Q_PROPERTY(QString x READ get_x WRITE set_x)
Q_PROPERTY(QString y READ get_y WRITE set_y)
public:
One();
~One();
QString get_port_number(void);
...
signals:
void portOpened(QString str);
...
private:
QString port_number;
QString x;
QString y;
};
#endif // ONE_H
"mainlib_global.h":
#ifndef MAINLIB_GLOBAL_H
#define MAINLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(MAINLIB_LIBRARY)
# define MAINLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define MAINLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // MAINLIB_GLOBAL_H
"mainlib.pro":
QT -= gui
TARGET = mainlib
TEMPLATE = lib
DEFINES += MAINLIB_LIBRARY
SOURCES += one.cpp
HEADERS += one.h\
mainlib_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
Piece of my "main.qml" file, where I declared my object of One class:
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.0
import OneClass 1.0
import QtQuick.Dialogs 1.1
Window {
id: mainWindow
visible: true
width: 1000
height: 720
minimumWidth: 930
color: "lightgrey";
property bool connected: false
// Object declaration
One {id: objOne}
...
}
Piece of my "main.cpp" file:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include "one.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QLocale::setDefault(QLocale::c());
qmlRegisterType<One>("OneClass", 1, 0, "One");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
...
return app.exec();
}
My application's .pro file:
TEMPLATE = app
DEPENDPATH += ../lib/mainlib
INCLUDEPATH += ../lib/mainlib
LIBS += -L../lib/build-mainlib-Desktop_Qt_5_7_0_MinGW_32bit-Release/release -lmainlib
QT += qml quick serialport
CONFIG += c++11
win32: RC_ICONS += icon.ico
SOURCES += main.cpp \
#one.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
#one.h
Here is the thing if uncomment "one.h" and "one.cpp" to compile them alongside with application, this problem don't appear and everything works fine. But when comment them to work with library, I get this "Cannot assign object to list property "data"" problem.
I've tried to connect library via right click on project->Add Library, but result is the same. I've read documentation about this "data" property, tried to explicitly assign object declaration to "data", but got the same. Tried:
resources: [
One {id: objOne}
]
And got "cannot assign object to list property "resources"". I'm just exhausted with solving this problem. I described you almost every my step, because I think that maybe I do something in wrong way? I'm begging for help...
A possible solution is to declare a new property and bind it to a new 'One' component:
Window {
property One objOne: One { }
}
BUT as you will see this may cause other (threading) problems. Don't do it!
I strongly advise you to use the Qt built-in plugin mechanism. It is designed to do exactly what you want: importing external dynamic QML libraries.
Check the documentation: http://doc.qt.io/qt-5/qtqml-modules-cppplugins.html
Good evening,
system: mac OSX 10.10.1
Qt 5.3.2 (via homebrew)
qwt 6.1.1 (via homebrew)
Qt Creator 3.2.1
I have used this tutorial http://codingexodus.blogspot.de/2013/01/getting-started-with-qwt.html and this one [http://de.wikibooks.org/wiki/Qt_für_C%2B%2B-Anfänger:_Qwt_nutzen2 as a starting point.
Here qwt is used in main.cpp:
#include <qwt_plot_curve.h>
#include <qwt_plot.h>
#include <qapplication.h>
int main(int argc, char **argv)
{
QApplication a(argc, argv);
double x[101];
double y[101];
for ( int i = 0; i < 101; i++ ) {
x[i] = i / 10.0;
y[i] = sin(x[i]);
}
QwtPlot plot;
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setRawSamples(x, y, 101);
curve->attach(&plot);
plot.show();
return a.exec();
}
When I tried (via copy and paste for test purposes if I have the library included correctly) the codes from both sites I kept receiving
QWidget: Must construct a QApplication before a QPaintDevice
Ok, so I should create a Application before I create a Widget.
However, to be on the safe side, I decided to test puting the qwt part into the mainwindow .cpp file. Still, no success.
Below the application output. Seems like there is some conflict between qwt and qt... (does not appear, when I do not use qwt).
Starte /Users/axel/Programmierung/Qt/Qwt2/build-Qwt2-Desktop-Debug/Qwt2.app/Contents/MacOS/Qwt2...
objc[6955]: Class QCocoaPageLayoutDelegate is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/lib/QtPrintSupport.framework/Versions/5/QtPrintSupport. One of the two will be used. Which one is undefined.
objc[6955]: Class QCocoaPrintPanelDelegate is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/lib/QtPrintSupport.framework/Versions/5/QtPrintSupport. One of the two will be used. Which one is undefined.
objc[6955]: Class QCocoaApplicationDelegate is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/plugins/platforms/libqcocoa.dylib. One of the two will be used. Which one is undefined.
objc[6955]: Class QNSApplication is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/plugins/platforms/libqcocoa.dylib. One of the two will be used. Which one is undefined.
objc[6955]: Class QCocoaMenuLoader is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/plugins/platforms/libqcocoa.dylib. One of the two will be used. Which one is undefined.
objc[6955]: Class QNSOpenSavePanelDelegate is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/plugins/platforms/libqcocoa.dylib. One of the two will be used. Which one is undefined.
objc[6955]: Class QNSImageView is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/plugins/platforms/libqcocoa.dylib. One of the two will be used. Which one is undefined.
objc[6955]: Class QNSStatusItem is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/plugins/platforms/libqcocoa.dylib. One of the two will be used. Which one is undefined.
objc[6955]: Class QNSMenu is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/plugins/platforms/libqcocoa.dylib. One of the two will be used. Which one is undefined.
QWidget: Must construct a QApplication before a QPaintDevice
Das Programm ist abgestürzt.
/Users/axel/Programmierung/Qt/Qwt2/build-Qwt2-Desktop-Debug/Qwt2.app/Contents/MacOS/Qwt2 ist abgestürzt
qwt2.pro
#-------------------------------------------------
#
# Project created by QtCreator 2014-12-06T23:33:01
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Qwt2
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../../usr/local/Cellar/qwt/6.1.1/lib/release/ -lqwt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../../usr/local/Cellar/qwt/6.1.1/lib/debug/ -lqwt
else:mac: LIBS += -F$$PWD/../../../../../../usr/local/Cellar/qwt/6.1.1/lib/ -framework qwt
else:unix: LIBS += -L$$PWD/../../../../../../usr/local/Cellar/qwt/6.1.1/lib/ -lqwt
INCLUDEPATH += $$PWD/../../../../../../usr/local/Cellar/qwt/6.1.1/lib/qwt.framework/Versions/6/Headers
DEPENDPATH += $$PWD/../../../../../../usr/local/Cellar/qwt/6.1.1/lib/qwt.framework/Versions/6/Headers
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>
#include <qwt_plot_curve.h>
#include <qwt_plot.h>
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);
double x[101];
double y[101];
for ( int i = 0; i < 101; i++ ) {
x[i] = i / 10.0;
y[i] = sin(x[i]);
}
QwtPlot plot;
// QwtPlotCurve *curve = new QwtPlotCurve();
// curve->setRawSamples(x, y, 101);
// curve->attach(&plot);
// plot.show();
}
MainWindow::~MainWindow()
{
delete ui;
}
(note that the error regarding QApplication and QPaintDevice is replicated as soon as I remove the comment before QwtPlot plot;.
Additional info from 07.12.2014
Please note that the error messages hint at a different Qt version:
objc[8714]: Class QNSMenu is implemented in both /usr/local/lib/QtGui.framework/Versions/4/QtGui and /usr/local/Cellar/qt5/5.3.2/plugins/platforms/libqcocoa.dylib. One of the two will be used. Which one is undefined.
I did not install Qt4 (knowingly)
Hypothesis:
Homebrew installed Qt4.
So i checked via brew info qwt.
Answer: ==> Dependencies
Required: qt ✔
So next step: brew info qt
Answer:
qt: stable 4.8.6 (bottled), HEAD
http://qt-project.org/
/usr/local/Cellar/qt/4.8.6 (2790 files, 122M) *
Built from source
Ok, so homebrew installs Qt4 for qwt.
I vaguely remember that in 5.1 or 5.2 Qt moved some QGui or QApplication stuff to another header... So if qwt now chooses a header from Qt5 (remember: the error message said this is undefined behaviour) instead of Qt4, could this explain why the error occurs?
Ok, by now I opened a new project with the same source code but using qt 4.8.6.
No error messages.
Also no plot, but this seems to be another problem.
Qt project suddenly stopped building. So as new just created empty projects based on QDialog or examples. Cleaning, rebuilding not helping.
Log of key errors:
/Users/dmitrytolstov/Workspace/Qt521/5.2.1/clang_64/lib/QtWidgets.framework/Versions/5/Headers/qdialog.h:117:
error: unknown type name 'QDialog'
Q_DISABLE_COPY(QDialog)
/Users/dmitrytolstov/Workspace/Qt521/5.2.1/clang_64/lib/QtWidgets.framework/Versions/5/Headers/qdialog.h:117:
error: C++ requires a type specifier for all declarations
Q_DISABLE_COPY(QDialog)
/Users/dmitrytolstov/Workspace/Qt521/5.2.1/clang_64/lib/QtWidgets.framework/Versions/5/Headers/qdialog.h:117:
error: unknown type name 'QDialog'
/Users/dmitrytolstov/Workspace/CC++/QtStuff/NewDiaproj/dialog.h:10:
error: unknown class name 'QDialog'; did you mean 'Dialog'?
class Dialog : public QDialog
/Users/dmitrytolstov/Workspace/CC++/QtStuff/NewDiaproj/dialog.h:10:
error: base class has incomplete type
class Dialog : public QDialog
/Users/dmitrytolstov/Workspace/CC++/QtStuff/NewDiaproj/main.cpp:8:
error: no member named 'show' in 'Dialog'
w.show();
7 errors generated.
make: *** [main.o] Error 1
18:46:36: Process «/usr/bin/make» exit with code 2.
Seems like something happened with qdialog.h or something. By the way project on QMainWindow works fine. I didn't do anything. Tried to reopen QtCreator, reboot computer.
I use Mac OS X and Qt 5.2.1
Any example provided by QtCreator or empty project based on QDialog. For example:
dialog.cpp:
#include "dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent)
{
}
Dialog::~Dialog()
{
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
};
#endif // DIALOG_H
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
NewDiaproj.pro
#-------------------------------------------------
#
# Project created by QtCreator 2014-04-20T19:31:45
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = NewDiaproj
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
Based on the fact that your files work fine for me on Archlinux with Qt 5.2, I think your QDialog file in the Qt installation got corrupted by some accidental or "vis major" action.
Reinstall it cleanly and then it should just work.
Hi i have made a gui in qt4 designer and want to add custom slots with custom class.
It project compiles nicely without errors but my custom function wont work what am i doing wrong? I will show u the header file qt4 designer made for me and ill show u my custom file as well as the main.cpp.. first the main.cpp
I have revised my code, here is what i have now i have added a file called sweetest.cpp and edited the sweetest.h here are my new file and the error i recieve..
First main.cpp
#include "ui_sweetguiform.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *widget = new QWidget;
Ui::SweetGuiForm ui;
ui.setupUi(widget);
widget->show();
return app.exec();
}
now my custom header file sweetest.cpp
#include "sweetest.h"
// trying to include the .moc file wouldnt work at all.
now the sweettest.h file with my code
#include "ui_sweetguiform.h"
class SweetGuiForm : public QWidget
{
Q_OBJECT
public:
SweetGuiForm( ): ui( new Ui::SweetGuiForm )
{
ui->setupUi( this );
connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
}
public slots:
void on_buttonBox_accepted()
{
ui.textEdit->setText(QString::number(23));
}
protected:
Ui::SweetGuiForm* ui;
};
Here is the compile error i recieve.. I am really stuck
In file included from sweetest.cpp:1:
sweetest.h: In member function ‘void SweetGuiForm::on_buttonBox_accepted()’:
sweetest.h:16: error: request for member ‘textEdit’ in ‘((SweetGuiForm*)this)->SweetGuiForm::ui’, which is of non-class type ‘Ui::SweetGuiForm*’
make: *** [sweetest.o] Error 1
I think im getting closer
The way that signals and slots work is that you must connect a signal to a slot. In your code, the simplest way to do that is in the constructor for the SweetGuiForm. You need to add:
SweetGuiForm() : ui(new Ui::SweetGuiForm) {
ui->setupUi(this);
connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
}
When the buttonBox emits its accepted signal all slots connected to it will be called.
update 1
On further inspection of your code, you are also missing the Qt macros that are used by the Qt MOC (meta-object compiler) system (http://qt-project.org/doc/qt-4.8/moc.html):
class SweetGuiForm : public QWidget
{
Q_OBJECT
public:
...
};
You also have to push the code through the MOC tool. It will generate a source file that needs to be included in your source. As I recall, you must include that in a cpp file; inclusion in a header is problematic. The following should be sufficient:
sweetguiform.cpp:
#include "suiteguiform.h"
#include "sweetguiform.moc"
update 2
On further further reflection, I had forgotten about the automatic signal/slot connection feature when you name your slots using special names (such as on_buttonBox_accepted). There is a blog post on just that here: http://qtway.blogspot.com/2010/08/automatic-connections-using-qt-signals.html. I've not used it myself, so I can't comment on its ability to work when using a ui member variable, though I suspect that it does not work in that arrangement. Regardless, you still need the Q_OBJECT macro and MOC.
Ok guys i figured it out and thought ide share what i found.
First the documentation is excellent in qt4 use it.
I found you can use qt4 designer to generate the hearder files, first i complied it with out custom slots and generated the ui_sweetgui2.h, then i could open the sweetgui2.h file generated by the first compile i did delete what qt4 put in there and put my custom slots in at that stage. did my head in for hours.... days.
so here is the simplest app on earth but its got me started so here are the files and code that worked for me and the documentation basically got me to click on to whats going on.
main.cpp
Strait from the documentation just changed the class name "SweetGuiForm".
#include <QApplication>
#include "sweetgui2.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SweetGuiForm sweetgui;
sweetgui.show();
return app.exec();
}
next sweetgui2.cpp
My first attempt at c++.. ugly but works. But again i found everything about getting the text from the textEdit and type casting it to a int in the calculator example and searching for toPlainText() in the qt4 assistant. notice below im including the file that i will define the new slots that ill show further on in my explanation. hope im making sense.
#include <QtGui>
#include "sweetgui2.h"
SweetGuiForm::SweetGuiForm(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
void SweetGuiForm::on_buttonBox_accepted()
{
QString stringamount = ui.textEdit->toPlainText();
int digitamount = stringamount.toInt();
ui.textEdit->setText(QString::number(25 + digitamount));
}
next sweetgui2.h the one we included above My custom header file with my custom slot.... simple as i said from the calculator example and twisted a lil.. you will get it this is not what it looks like when you generate it from designer on the first compile this is after i have deleted nearly all what was there and opened the calculator example and followed in the tutorial wich shows you how to make your first custom slot .
#ifndef SWEETGUI2_H
#define SWEETGUI2_H
#include "ui_sweetgui2.h"
class SweetGuiForm : public QWidget
{
Q_OBJECT
public:
SweetGuiForm(QWidget *parent = 0);
private slots:
void on_buttonBox_accepted();
private:
Ui::SweetGuiForm ui;
};
#endif // SWEETGUI2_H
Again Straight from the documentation. I used the calculator example to get the basic flow.
next ui_sweetgui2.h
I include this file because i was trying to add my slots to the sweetgui2.h that was generated by qt4 desinger. doesnt work guys ..so i compiled first with sweetgui2.h file you generate with the designer, i go to forms menu then view code that is where u can save header files. then of course save the ui file.
and compile then you end up with the ui_sweetgui2.h file wich looked like the sweetgui2.h generated by the designer
#ifndef UI_SWEETGUI2_H
#define UI_SWEETGUI2_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialogButtonBox>
#include <QtGui/QHeaderView>
#include <QtGui/QTextEdit>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_SweetGuiForm
{
public:
QDialogButtonBox *buttonBox;
QTextEdit *textEdit;
void setupUi(QWidget *SweetGuiForm)
{
if (SweetGuiForm->objectName().isEmpty())
SweetGuiForm->setObjectName(QString::fromUtf8("SweetGuiForm"));
SweetGuiForm->resize(486, 238);
buttonBox = new QDialogButtonBox(SweetGuiForm);
buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
buttonBox->setGeometry(QRect(150, 200, 181, 26));
buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
textEdit = new QTextEdit(SweetGuiForm);
textEdit->setObjectName(QString::fromUtf8("textEdit"));
textEdit->setGeometry(QRect(150, 50, 221, 91));
retranslateUi(SweetGuiForm);
QObject::connect(buttonBox, SIGNAL(rejected()), SweetGuiForm, SLOT(close()));
QMetaObject::connectSlotsByName(SweetGuiForm);
} // setupUi
void retranslateUi(QWidget *SweetGuiForm)
{
SweetGuiForm->setWindowTitle(QApplication::translate("SweetGuiForm", "SweetGuiBack", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class SweetGuiForm: public Ui_SweetGuiForm {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_SWEETGUI2_H
Then i recompiled again with my custom slots and shazam! now i can begin to learn some c++.
thanks for all the hints guys, between you all and the documentation i got there.
hope this helps. The main thing to look at is the order things are included i mean my sweetgui2.cpp file
includes the sweetgui2.h file. wich grabs all my custom stuff.
My sweetgui2.h file
includes the ui_sweetgui2.h wich has all the stuff the designer made when i did the first compile. Main.cpp calls my SweetGuiForm class .
As you all can see my first couple days with c++ but this is a good starting point. it made me put the basic flow together in my mind. qt4 assistant look at it.. its well explained and the examples seem very good. ho ho ho merry xmas. hope my explanation helps.
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 :)