override operator with qtscript - overriding

I want do a math editor using qtscript.
It will support array calculating in script. Such as array1 + array2 = array3.({1,2,3}+{3,4,5} = {4,6,8});
Maybe I need override operator+,
I consult the example of QByteArray, and I override operator+,but when I execute in Script,it can't be invoke,anyone coule give me some suggestions?
bytearray.h
class ByteArrayClass : public QObject, public QScriptClass
{
public:
QByteArray &operator+(int n);
}
main.cpp
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QScriptEngine eng;
ByteArrayClass *baClass = new ByteArrayClass(&eng);
eng.globalObject().setProperty("ByteArray", baClass->constructor());
eng.evaluate("ba = new ByteArray(4))"
eng.evaluate("ba+2;"); //this will not invoke override operator+.
ByteArrayClass *ba = new ByteArrayClass(&eng);
int n = 3;
*ba + n; //but this can invoke the override operator+
}
If this couldn't be realised,maybe the one way is to replace all the operator to the custom function.

As far as I know operators can not be overloaded in QtScript, because it is not allowed in Javascript in general (e.g. see ECMA Script 4 - Progress and this Article).
Now for your case you have the choice to go with Add, Mult, ... functions or leave for some less constrained scripting language.

Related

QtConcurrent mapped with index

I wondered if there is an option to also hand over the current processed index with QtConcurrent::mapped(someVector, &someFunction)) (also filter, filtered, map,...)
What I want: I want to do something with the elements in someVector based on the current index in it. but since the function someFunction is only taking the type T which is also used for the QVector<T> vector.
What I did: Because I needed this, I created a QVector<std::pair<int, T>> and manually created the index for the elements.
Since this requires more space and is not a nice solution, I thought maybe there could be another solution.
Docs: https://doc.qt.io/qt-5/qtconcurrent-index.html
If your input is a QVector, you can make use of the fact that QVector stores all the elements contiguously. This means that given a reference to an element e in a QVector v, then the index of e can be obtained by:
std::ptrdiff_t idx = &e - &v.at(0);
Below is a complete example using QtConcurrent::mapped:
#include <iterator>
#include <numeric>
#include <type_traits>
#include <utility>
#include <QtCore>
#include <QtConcurrent>
// lambda functions are not directly usable in QtConcurrent::mapped, the
// following is a necessary workaround
// see https://stackoverflow.com/a/49821973
template <class T> struct function_traits :
function_traits<decltype(&T::operator())> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> {
// specialization for pointers to member function
using functor_type = ClassType;
using result_type = ReturnType;
using arg_tuple = std::tuple<Args...>;
static constexpr auto arity = sizeof...(Args);
};
template <class Callable, class... Args>
struct CallableWrapper : Callable, function_traits<Callable> {
CallableWrapper(const Callable &f) : Callable(f) {}
CallableWrapper(Callable &&f) : Callable(std::move(f)) {}
};
template <class F, std::size_t ... Is, class T>
auto wrap_impl(F &&f, std::index_sequence<Is...>, T) {
return CallableWrapper<F, typename T::result_type,
std::tuple_element_t<Is, typename T::arg_tuple>...>(std::forward<F>(f));
}
template <class F> auto wrap(F &&f) {
using traits = function_traits<F>;
return wrap_impl(std::forward<F>(f),
std::make_index_sequence<traits::arity>{}, traits{});
}
int main(int argc, char* argv[]) {
QCoreApplication app(argc, argv);
// a vector of numbers from 0 to 500
QVector<int> seq(500, 0);
std::iota(seq.begin(), seq.end(), 0);
qDebug() << "input: " << seq;
QFuture<int> mapped = QtConcurrent::mapped(seq, wrap([&seq](const int& x) {
// the index of the element in a QVector is the difference between
// the address of the first element in the vector and the address of
// the current element
std::ptrdiff_t idx = std::distance(&seq.at(0), &x);
// we can then use x and idx however we want
return x * idx;
}));
qDebug() << "output: " << mapped.results();
QTimer::singleShot(100, &app, &QCoreApplication::quit);
return app.exec();
}
See this question for a related discussion. Note that the linked question has a cleaner answer that involves the usage of zip and counting iterators from boost (or possibly their C++20 ranges counterparts), but I don't think that this would play well with QtConcurrent::map when map slices the sequence into blocks, and distributes these blocks to multiple threads.

QLineEdit should accept hexadecimal values ranging from [0 - FFFFF]

I have a requirement where I want my QLineEdit should accept hexadecimal values ranging from [0 - FFFFF]. can someone help me out with this?
I have tried the below code, but it holds good for only 1 char display.
I don't see any code in your post, but what you're looking for is a validator, derived from the QValidator class. Create a sub-class, say HexValidator, and implement the "validate" method. You check the input string for the allowed characters and range and return the appropriate state.
You then assign the validator to the QLineEdit using the QLineEdit::setValidator method. Note that the QLineEdit doesn't take ownership of the validator, so you need to make sure you delete it separately or give it a parent so that it gets cleaned up when the parent is deleted. You can create a single validator and assign it to multiple fields if needed.
I was wrong in my comment about Qt docs having a Hex spin box example... so I found one in my archive instead. It could be more flexible, but OTOH it's very simple and short. I know this isn't a QLineEdit but maybe it'll help anyway. The QValidator used here could be used in a line edit also.
#ifndef _HEXSPINBOX_H_
#define _HEXSPINBOX_H_
#include <QSpinBox>
#include <QRegularExpressionValidator>
// NOTE: Since QSpinBox uses int as the storage type, the effective editing range
// is +/- 0x7FFF FFFF, so it can't handle a full unsigned int.
// QDoubleSpinBox would be a more suitable base class if a wider range is needed.
class HexSpinBox : public QSpinBox
{
Q_OBJECT
public:
HexSpinBox(QWidget *parent = nullptr,
bool showPrefix = false,
const QString &format = QStringLiteral("%x")) :
QSpinBox(parent),
format(format)
{
// Validates hex strings up to 8 chars with or w/out leading "0x" prefix.
// For arbitrary prefix/suffix, the regex could be built dynamically
// in validate(), or override setPrefix()/setSuffix() methods.
const QRegularExpression rx("(?:0[xX])?[0-9A-Fa-f]{1,8}");
validator = new QRegularExpressionValidator(rx, this);
setShowPrefix(showPrefix);
}
public slots:
void setShowPrefix(bool show)
{
if (show)
setPrefix(QStringLiteral("0x"));
else
setPrefix(QString());
}
void setFormat(const QString &text)
{
format = text;
lineEdit()->setText(textFromValue(value()));
}
protected:
QValidator::State validate(QString &text, int &pos) const override
{
return validator->validate(text, pos);
}
int valueFromText(const QString &text) const override
{
return text.toInt(0, 16);
}
QString textFromValue(int value) const override
{
return QString().sprintf(qPrintable(format), value);
}
private:
QRegularExpressionValidator *validator;
QString format;
};
#endif // _HEXSPINBOX_H_
Example:
#include "HexSpinBox.h"
#include <QApplication>
#include <QBoxLayout>
#include <QDialog>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog d;
d.setLayout(new QVBoxLayout);
HexSpinBox* sb = new HexSpinBox(&d);
sb->setMaximum(0xFFFFF);
sb->setValue(0x0A234);
d.layout()->addWidget(sb);
HexSpinBox* sb2 = new HexSpinBox(&d, true, QStringLiteral("%05X"));
sb2->setMaximum(0xFFFFF);
sb2->setValue(0x0A234);
d.layout()->addWidget(sb2);
return d.exec();
}

QT add Item trigger redraw, not freezing

i'm using QT for the first time and got some problems with refreshing the GUI while adding elements.
The Code looks like:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PObj obj;
MainWindow mw;
qRegisterMetaType<std::string>();
QObject::connect(&obj, SIGNAL(setText(std::string const&)),
&mw, SLOT(appendText(std::string const&)));
QFuture<void> f1 = QtConcurrent::run(&obj, &PObj::process);
mw.show();
f1.waitForFinished();
return a.exec();
}
With the PObj::process definition:
void PObj::process()
{
for(; ;)
{
sleep(1);
//do work and set text
std::string text = "bla";
emit setText( text );
}
}
And the MainWindow::appendText slot:
void MainWindow::appendText(std::string const& str )
{
ui->listWidget->addItem(QString::fromStdString(str));
}
I've tried placing qApp->processEvents() ,QCoreApplication::processEvents(); ... running wit future in the ThreadPool.
I thought running them with Concurrent::run is enough ?
UPDATE:
The question is, why the GUI isnt refreshed every second a new item is added ?
The f1.waitForFinished(); calls blocks until f1 is finished, as the name implies. This will never happen because you have the infinite loop. So your code will never get to main loop. You can't block the main thread like that! In general, avoid any WaitForXxxx() methods, especially the GUI thread.
Also, you have no way of stopping the process(); anyway, so waiting for it to finish doesn't make any sense... You might want to add a way to tell it to stop (such as atomic variable) but anyway, to fix your problem, simply remove the f1.waitForFinished(); line.
To terminate the task nicely, try adding QAtomicInt flag (not volatile boolean, it won't do), and then change the code like this:
Add member variable to PObj (should make it private and add setter):
QAtomicInt termianteFlag;
Change main like this:
int main(int argc, char *argv[])
{
///snip
QFuture<void> f1 = QtConcurrent::run(&obj, &PObj::process);
mw.show();
int ret = a.exec();
f1.terminateFlag = 1; // change this to setter method
f1.waitForFinished(); // this is not ideal, will wait for up to a second before exit
}
and
void PObj::process()
{
while(!terminateFlag)
{
sleep(1);
//do work and set text
std::string text = "bla";
emit setText( text );
}
}

QTimer::singleShot and QMetaMethod::invoke

In some Qt examples, I see they use
QTimer::singleShot(0, this , SLOT(funcA())), why not to call the slot funcA directly? also the same question for using QMetaMethod::invoke to call function with parameters.
The following lines are all functionally equivalent:
QTimer::singleShot(0, object, &Class::funcA); // Qt 5
QTimer::singleShot(0, object, SLOT(funcA())); // Qt 4
QMetaObject::invokeMethod(object, "funcA", Qt::QueuedConnection);
As is now apparent, the intent is to execute the call within the event loop. The queued call results in the posting of an QMetaCallEvent to the object. This event is handled by QObject::event and results in the call of the desired method. Thus, the following are exactly equivalent, even if the latter is a private implementation detail - letting me skip the details of instantiating the event:
QMetaObject::invokeMethod(object, "funcA", Qt::QueuedConnection);
QCoreApplication::postEvent(object, new QMetaCallEvent{...});
This comes handy in various situations. For example:
To execute some code after all hitherto posted events have been handled.
To execute only after the event loop has started.
To call an invokable method that's not accessible due to C++ access modifiers. The invokable methods are: signals, slot, and methods declared Q_INVOKABLE.
The direct call is unsafe (read: an error!) when a QObject resides in another thread, unless you're explicitly calling a method documented as thread-safe.
The queued call is a necessity if you wish to ensure that an event loop quits immediately: a direct quit() call is a no-op if the loop is not running yet.
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
app.quit(); // this is a no-op since the event loop isn't running yet
return app.exec(); // will not quit as desired
}
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
return app.exec(); // will return immediately
}
Ideally, you'd use postToThread from this answer, it offers the lowest-cost way of calling methods in other threads:
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
postToThread([]{ qApp->quit(); });
}
An alternative way of doing it is using a QObject as a signal source:
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
{
QObject src;
src.connect(&src, &QObject::destroyed, &app, &QCoreApplication::quit,
Qt::QueuedConnection);
}
return app.exec(); // will return immediately
}
Yet another way would be to use a custom event and act in its destructor:
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
struct QuitEvent : QEvent {
QuitEvent() : QEvent(QEvent::None) {}
~QuitEvent() { qApp->quit(); }
};
QCoreApplication::postEvent(&app, new QuitEvent);
return app.exec(); // will return immediately
}
Every system has a event loop where events are processed. Say like
application::processEvents()
{
// process event list..
}
Now the place where you write QTimer::singleShot(0, this, SLOT(doSomething())); might be inside some processing event.
When this loop is done, processEvents will be called again and in that the doSomething() will be executed.
So this is like calling doSomething in the next event loop, rather than calling it immediately.
Hope you get the idea.
These methods can also be used to invoke protected and private members of a class (if they are defined as slots) from a scope that would otherwise require public access.

Qt Script function wrapping

I am going through the documentation of Qt Scripting and came up to it is totally confusing and full if mis guiding text. Could some please explain in simple English how to wrap a function and access it in script code after wrapping. I have included my example below.
Wrapper function. This is a simple wrapper which I need to return the string that is passed as the parameter. following is the code.
#include <QApplication>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptContext>
#include <QDebug>
QScriptValue returnProperty(QScriptContext *context , QScriptEngine *engine)
{
qDebug() << "running returnValues Function "<< context->argument(0).toString();
return context->thisObject().property("returnValue");
}
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QScriptEngine engine;
//Evaluating a simple expression
qDebug() << engine.evaluate("1+2").toNumber();
QScriptValue func = engine.globalObject();
func.setProperty("foo",engine.newFunction(returnProperty),QScriptValue::PropertyGetter);
engine.evaluate("var v= foo('name') ; print( 'This prints values from addValues function :',v) ;");
}
And the output is as follows
3
Running returnValues Function "undefined"
If I am understanding this correctly this is what I should do and if I call engine.newObject() as it is mentioned in the doc function does not even get called.
what I do not get here is that in what is the property I am assigning in func.setproperty line and what can I do with the property foo once I set it. How can I set a value in the function.
I appreciate if someone explain what I am doing wrong here.
You are already on the right track. QScriptEngine::newFunction() brings the function into the engine. Now, you need a way to access this function from the script. A "function" is just a property of the global object and you can add a new property with setProperty(). The code
QScriptValue globalObject = engine.globalObject();
QScriptValue func = engine.newFunction(returnProperty);
globalObject.setProperty("foo", func);
produces the output
3
running returnValues Function "name"
This prints values from addValues function : name
The flags QScriptValue::PropertyGetter and QScriptValue::PropertySetter are only needed, when you want to create a property, which has to call a function upon access. It is similar to the properties of QObject. Consider this example:
class MyObject : public QObject
{
Q_PROPERTY(QString name READ getName WRITE setName)
};
MyObject* obj = new MyObject;
When you do a obj->setProperty("name", "Sam"); you call MyObject::setName("Sam") in the background and obj->getProperty("name") is a wrapper for MyObject::getName(). A small example:
QScriptValue getName(QScriptContext* ctx, QScriptEngine* eng)
{
// Return the value of the internal '_name_' property.
qDebug() << "Getter 'getName' called";
return ctx->thisObject().property("_name_");
}
QScriptValue setName(QScriptContext* ctx, QScriptEngine* eng)
{
// Do some processing and store the name in an internal '_name_' property.
qDebug() << "Setter 'setName' called";
ctx->thisObject().setProperty("_name_",
ctx->argument(0).toString().toUpper());
return QScriptValue::UndefinedValue;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QScriptEngine engine;
QScriptValue globalObject = engine.globalObject();
// Create a new object.
QScriptValue obj = engine.newObject();
// Bring the functions into the engine.
QScriptValue getNameFunc = engine.newFunction(getName);
QScriptValue setNameFunc = engine.newFunction(setName);
// Create a 'name' property, which calls the getter and setter from above.
obj.setProperty("name", getNameFunc, QScriptValue::PropertyGetter);
obj.setProperty("name", setNameFunc, QScriptValue::PropertySetter);
// Make the new object known as 'person'.
globalObject.setProperty("person", obj);
// Test our construct.
engine.evaluate("print('Set the name to fitzgerald');");
engine.evaluate("person.name = 'fitzgerald';");
engine.evaluate("print('And the name is... ' + person.name)");
}
Finally the output:
Set the name to fitzgerald
Setter 'setName' called
Getter 'getName' called
And the name is... FITZGERALD

Resources