I can not figure out how to organize the dynamic creation of the c++ objects in QJSEngine
I try to do so :
main.cpp
#include <QCoreApplication>
#include <QQmlEngine>
#include <QObject>
#include <QQmlComponent>
#include <QtQml>
#include <QQmlEngine>
#include "js_object.h"
Q_DECLARE_METATYPE(Js_Object*)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QJSEngine myEngine;
qmlRegisterType<Js_Object>("Js_Object", 1, 0, "Js_Object");
QJSValue v=myEngine.evaluate("import Js_Object 1.0;\n var t= new Js_Object() ;");
qDebug()<<v.toString();
return a.exec();
}
JsObject.h
#ifndef JS_OBJECT_H
#define JS_OBJECT_H
#include <QObject>
class Js_Object : public QObject
{
Q_OBJECT
public:
explicit Js_Object(QObject *parent = 0) : QObject(parent) {
qDebug()<<"Js_Object::Js_Object(QObject *parent) : QObject(parent)";
}
};
#endif // JS_OBJECT_H
this example compile, bun console print :
"SyntaxError: Syntax error"
What I do wrong ?
QScriptEngine allow to do this, but in QT5.5 QScriptEngine is deprecated,
try to work with QJSEngine
Related
I'm trying to add an objects (that inherits from QWidget) as a child to another QWidget as shown below, it works perfectly with another normal QWidget instance but not with my custom class, any idea why ?
fenetre.h
#ifndef FENETRE_H
#define FENETRE_H
#include <QWidget>
#include <QMouseEvent>
class Fenetre : public QWidget
{
Q_OBJECT
public:
Fenetre();
};
#endif // FENETRE_H
fenetre.cpp
#include "fenetre.h"
Fenetre::Fenetre() : QWidget()
{
}
main.cpp
#include <iostream>
#include <QApplication>
#include <QWidget>
#include "fenetre.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QWidget window;
window.setFixedSize(800,600);
//This appears
QWidget rec1;
rec1.setParent(&window);
rec1.setFixedSize(100,100);
rec1.move(400,200);
rec1.setStyleSheet("background-color: red");
//This one not
Fenetre rec2;
rec2.setParent(&window);
rec2.setFixedSize(100,100);
rec2.move(200,200);
rec2.setStyleSheet("background-color: green");
window.show();
return app.exec();
}
PS: I did research on the platform, but the majority of the answers speak of the use of a layout. Thank you !
you miss the parent:
//header .h
class Fenetre : public QWidget
{
Q_OBJECT
public:
Fenetre(QWidget *parent = 0);
};
//source .cpp
Fenetre::Fenetre(QWidget *parent) : QWidget(parent)
{
}
I'm running QT 5.10.1 on Windows, the app is only made in QML. I'm trying to use the new retranslate() to change language during runtime. The current code is working fine with texts that use the getEmptyString() appended to it. But the rest of the text within qsTr() does not. TranslationHandler.cpp is empty and I haven't cleaned up the includes.
So I'm able to set a language from the QML using the context property. Also I checked so the engine is the same instance. Any ideas why the retranslate function is not working?
Thanks for the help!
main.cpp :
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTranslator>
#include <QtGui>
#include <QQmlContext>
#include <QDebug>
#include <QQmlEngine>
#include "translationhandler.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
TranslationHandler transHndl(&engine);
engine.rootContext()->setContextProperty("translateHandler", &transHndl);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
TranslationHandler.h :
#ifndef TRANSLATIONHANDLER_H
#define TRANSLATIONHANDLER_H
#include <QTranslator>
#include <QString>
#include <QGuiApplication>
#include <QObject>
#include <QDebug>
#include <QQmlEngine>
class TranslationHandler : public QObject
{
Q_OBJECT
Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)
public:
explicit TranslationHandler(QQmlEngine *engine)
{
m_translator1 = new QTranslator(this);
m_currentLanguage = "en";
m_engine = engine;
}
QString getEmptyString()
{
return "";
}
Q_INVOKABLE QString getCurrentLanguage()
{
return m_currentLanguage;
}
Q_INVOKABLE void selectLanguage(QString language)
{
if(language == QString("jp"))
{
m_currentLanguage = language;
m_translator1->load(":/translation/qml_jp.qm");
qGuiApp->installTranslator(m_translator1);
m_engine->retranslate();
}
if(language == QString("en"))
{
m_currentLanguage = language;
qGuiApp->removeTranslator(m_translator1);
m_engine->retranslate();
}
emit languageChanged();
}
signals:
void languageChanged();
private:
QTranslator *m_translator1;
QString m_currentLanguage;
QQmlEngine *m_engine;
public slots:
};
#endif // TRANSLATIONHANDLER_H
This was confirmed as a bug and will be fixed in version 5.12. If you want to compile it yourself please check the bug report
https://bugreports.qt.io/browse/QTBUG-68350
I created a class, whose base class was QObject, Now if I try to add a QTextBrowser in my code, I get the error as no matching function for call to QTextBrowser. I tried to compile the code by adding QWidget class but still I am unable to resolve the error. How can I resolve this.
MainWindow code
#include <QApplication>
#include "window.h"
#include "bbbserver.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
bbbServer server;
Window window;
window.setStyleSheet("background-color: rgb(226, 226, 226);");
window.showFullScreen();
return app.exec();
}
Here is the code for window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
class QPushButton;
class QTextBrowser;
class QProcess;
class QFile;
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
QTextBrowser *statusWindow;
private slots:
};
#endif // WINDOW_H
Here is the code for window.cpp
#include "window.h"
#include <QPushButton>
#include <QProcess>
#include <QTextBrowser>
#include <QDebug>
Window::Window(QWidget *parent) : QWidget(parent)
{
// Create and position the buttons on the main window
/*************** text browser *********************/
statusWindow = new QTextBrowser(this);
statusWindow->setMinimumSize(QSize(0,0));
statusWindow->setMaximumSize(QSize(10000,10000));
statusWindow->setGeometry(175, 50, 440, 420);
statusWindow->setStyleSheet("background-color: rgb(236, 236, 236);");
}
Here is the code for bbbserver.h file
#ifndef BBBSERVER_H
#define BBBSERVER_H
#include <QObject>
#include <QWidget>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
class bbbServer : public QObject
{
Q_OBJECT
public:
explicit bbbServer(QObject *parent = 0);
signals:
public slots:
void newConnection();
private:
QTcpServer *server;
};
#endif // BBBSERVER_H
this is bbbserver.cpp file
#include "bbbserver.h"
bbbServer::bbbServer(QObject *parent):
QObject(parent)
{
/*************************** SERVER *********************************/
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
if(!server->listen(QHostAddress::QHostAddress("192.168.0.1"), 5000))
{
qDebug() << "SERVER NOT STARTED";
}
else
{
qDebug() << "SERVER STARTED";
}
}
void bbbServer::newConnection()
{
QTcpSocket *socket= server->nextPendingConnection();
socket->write("Connection from 192.168.0.1 BBB\n");
socket->flush();
socket->waitForBytesWritten(30000);
socket->waitForReadyRead(30000);
qDebug() << socket->readAll();
`HERE I WANT TO ACCESS THE STATUS WINDOW(textbrowser statusWindow)`
}
And this is the full error, which is same as in screenshot.
bbbserver.cpp:8: error: no matching function for call to 'QTextBrowser::QTextBrowser(bbbServer* const)'
The parent parameter passed to the QTextBrowser constructor needs to be of type QWidget * whereas you're passing a QObject *. If you really need the bbbServer to be the parent object of the QTextBrowser then simply do...
infoSpace = new QTextBrowser;
infoSpace->QObject::setParent(this);
Note: The QObject:: qualifier is required because QWidget::setParent(QWidget *) effectively hides its base's QObject::setParent(QObject *) member meaning the latter won't take part in the normal lookup process.
I have 3 classes.
class with a mainwindow which comes from the designer(ui-file)
class wich will manage database stuff like inserts
controller class. I want to extend the whole thing to networkcommunication later.
My problem:
I want to connect a simple QPushButton ui->addbutton from the window class with a slot addEntry from the databaseclass, but I get this error :
ERROR: no matching function for call to
'generalControler::connect(QPushButton*, const char*, dbmanager*&,
const char*)'
mydb, SLOT(addEntry()));
//no difference with &mydb or *mydb
MainWindow(0x13f57988, name = "MainWindow") QPushButton(0x13f5f3e0, name = "addButton")
MainWindow(0x13f57988, name = "MainWindow") 0x13f5f3e0//<--?????
//<=Here u see the adresses printed with Qdebug(). top: mainwindowclass. bot: generalcontrolerclass
//why there is missing information after returning the adress of a 'ui->addButton' to another class? Is this maybe the problem?
main.cpp
#include <QApplication>
#include "generalcontroler.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
generalControler controler;
return a.exec();
}
generalcontroler.h
#ifndef GENERALCONTROLER_H
#define GENERALCONTROLER_H
#include <QApplication>
#include "mainwindow.h"
#include "dbmanager.h"
class generalControler : public QObject
{
Q_OBJECT
public:
generalControler();
};
#endif // GENERALCONTROLER_H
generalcontroler.cpp
#include "generalcontroler.h"
#include <QDebug>
generalControler::generalControler(){
MainWindow* window = new MainWindow;
window->show();
dbmanager* mydb= new dbmanager("path_to_my_database.db", window);
mydb->addEntry();
qDebug()<<window->getThis()<<window->getcloseButton();
connect(window->getaddButton(), SIGNAL(clicked()),
mydb, SLOT(addEntry()));
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton* getaddButton();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
}
QPushButton* MainWindow::getaddButton()
{
return ui->addButton;
}
dbmanager.h
#ifndef DBMANAGER_H
#define DBMANAGER_H
#include <QSqlDatabase>
#include <QDebug>
#include "mainwindow.h"
class dbmanager: public QObject{
Q_OBJECT
public:
dbmanager(const QString& path);
public slots:
void addEntry();
private:
QSqlDatabase mydatabase;
};
#endif // DBMANAGER_H
dbmanager.cpp
#include "dbmanager.h"
dbmanager::dbmanager(const QString& path)
{
mydatabase = QSqlDatabase::addDatabase("QSQLITE");
mydatabase.setDatabaseName(path);
if (!mydatabase.open())
{
qDebug() << "Error: connection fail";
}
else
{
qDebug() << "Database: connection ok";
}
}
void dbmanager::addEntry()
{
qDebug()<<"addEntry success";
}
I was searching for 6 hours but I never saw such an example with 2 classes, a controler and an ui-file. Could anyone help me?
The connect looks good to me. Try if #include <QPushButton> in generalcontroler.cpp helps. If the compiler knows about QPushButton only by forward-declaration, it doesn't know that it's a QObject and thus the connect() signatures (with QObject* in it) don't match.
Not sure this issue about Qt or C++ in general, I'm just a newbie for both of these!
I got a simple Qt app, with a MainWindow and Hello class like below:
hello.h
#ifndef HELLO_H
#define HELLO_H
#include <QWidget>
#include "mainwindow.h"
class Hello : public QWidget
{
Q_OBJECT
public:
explicit Hello(MainWindow *parent = 0);
signals:
public slots:
};
#endif // HELLO_H
heloo.cpp
#include "hello.h"
Hello::Hello(MainWindow *parent) :
QWidget(parent)
{
//nothing here yet
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include "hello.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Hello* hi;
};
#endif // MAINWINDOW_H
mainwindows.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
hi = new Hello(this);
}
MainWindow::~MainWindow()
{
}
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
and here is the error when I build my project:
from ../untitled1/main.cpp:2: ../untitled1/hello.h:11: error: expected
‘)’ before ‘*’ token
and the line cause the error is:
explicit Hello(MainWindow *parent = 0);
Can you help me to resolve the issue!
Thanks you!
You have circular inclusion of header files in "hello.h" and "maindwindow.h". There is no need to include these files in the header file as you are just using a pointer. A simple forward declaration such as class MainWindow; in "hello.h" is sufficient.