QML WebEngine how enable scroll animation - qt

The QWebEngineSettings class has the ScrollAnimatorEnabled attribute, but WebEngineSettings (QML) does not have this property. How to enable smooth scrolling in WebEngineView?

can run the program with the --enable-smooth-scrolling argument or like this:
int main(int argc, char *argv[])
{
std::vector<char*> argv2(argc + 1);
argv2.reserve(argc + 1);
for (int i = 0; i < argc; ++i)
argv2.emplace_back(argv[i]);
argv2.emplace_back(const_cast<char*>("--enable-smooth-scrolling"));
argc = int(argv2.size());
argv = argv2.data();
QApplication a(argc, argv);
...

Related

Why isn't Qt gradually changing the opacity of a window in a loop?

I'm trying to change the opacity of the window of my program in Qt and using C++11. In other words, I want to open the main Windows slowly by adding some instructions to main.cpp. You see those instructons below:
MainWindow a;
a.show();
for(int Counter = 0; Counter <= 100; Counter++)
{
a.SetWindowOpacity(Counter);
for(Wait = 0; Wait < 100; Wait++);
}
But when I build and run my project, I don't see any changes and it normally runs. This is whole source code, main.cpp.
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle("Add a new student");
w.show();
for(int Counter = 0; Counter <= 100; Counter++)
{
w.setWindowOpacity(Counter);
for(int Wait = 0; Wait < 100; Wait++);
}
return a.exec();
}
So, why don't Qt change the opacity of the window of my program?
The other answer indicates the cause of the error:
wrong range of values.
instant execution that will not make the effect visible, etc.
They also propose to use QTimer but I think a more elegant solution is to use QPropertyAnimation:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle("Add a new student");
w.show();
QPropertyAnimation *animation = new QPropertyAnimation;
animation->setPropertyName("windowOpacity");
animation->setTargetObject(&w);
animation->setStartValue(0.0);
animation->setEndValue(1.0);
animation->setDuration(1000);
animation->start(QAbstractAnimation::DeleteWhenStopped);
return a.exec();
}
There are number of issues with your code. First, setWindowOpacity can take values from 0.0 to 1.0, also it does not take int but qreal, so your loop has to change and look like this:
for(qreal Counter = 0.0; qreal <= 1.0; Counter+=0.1)
{
w.setWindowOpacity(Counter);
}
Second, the loop is very fast, so even if it did change the opacity you wouldn't notice it, so if you want to see it visually changing opacity, then you need to add a sleep in between, so your loop will look like this:
for(qreal Counter = 0.0; Counter <= 1.0; Counter+=0.1)
{
w.setWindowOpacity(Counter);
QThread::msleep(100);
}
This will mean that it will change the opacity, wait 100 milliseconds and then change it again.
Third, even if you do all of this, you still won't see anything, since the opacity will change and only after that the event loop will run. And the event loop will run after the a.exec() call.
So you have to put your snippet of code somewhere else and after the window is shown. This means you have to call your opacity changing code with delayed execution. The fastest way to do it is with QTimer, so your code will look like this:
#include <QApplication>
#include <QThread>
#include <QTimer>
#include "mainwindow.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.setWindowOpacity(0.0);
w.show();
QTimer::singleShot(0, [&] {
for (qreal i = 0.0; i <= 1.0; i += 0.01) {
w.setWindowOpacity(i);
//The processEvents() call will make sure your app doesn't freeze in the process
QApplication::processEvents(QEventLoop::AllEvents, 100);
QThread::msleep(100);
}
});
return a.exec();
}

Qt application crash when trying to delete Qimage Format_RGB888

I get a "HEAP CORRUPTION DETECTED: After normal block ... CRT detected that the application wrote to memory after end of heap buffer." when variable test gets destroyed.
If I change the image size, the crash does not occur, I'm thinking it has something to do with memory alignment, but I cannot figure out what.
I am using the official Qt builds for MSVC 2017 64bit
#include <QCoreApplication>
#include <QImage>
QImage foo(int width, int height)
{
QImage retVal(width, height, QImage::Format_RGB888);
for (int i = 0; i < height; ++i) // read each line of the image
{
QRgb *lineBuf = reinterpret_cast<QRgb *>(retVal.scanLine(i));
for (int j = 0; j < width; ++j)
{
lineBuf[j] = qRgb(0,0,0);
}
}
return retVal;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
{
QImage test = foo(5,5);
}
return a.exec();
}
As GM suggested in the comment above, the problem is that QRgb is a 32bit construct.
I've changed the code replacing QRgb with a custom RGB struct
struct RGB {
uint8_t r;
uint8_t g;
uint8_t b;
};
QImage foo(int width, int height)
{
QImage retVal(width, height, QImage::Format_RGB888);
for (int i = 0; i < height; ++i) // read each line of the image
{
RGB *lineBuf = reinterpret_cast<RGB *>(retVal.scanLine(i));
for (int j = 0; j < width; ++j)
{
RGB tmp;
//.... do stuff with tmp
lineBuf[j] = tmp;
}
}
return retVal;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
{
QImage test = foo(5,5);
}
return a.exec();
}

Qt: Remove the icon on the top left

i want to remove the icon on the top left of a qt application. here is my main.cpp code
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
Receive receive;
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
// setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
QObject::connect(window,SIGNAL(sendUrltoC(QUrl)),&receive,SLOT(getText(QUrl)));
return app.exec();
}
if i uncomment the commented part it gives me an error. what should i do?

View doesn't display data from the model

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
After making connection, I tried this code , but it doesn't display any data
QSqlTableModel *model=new QSqlTableModel;
model->setTable("tamp");
model->select();
QTableView *view = new QTableView;
view->setModel(model);
view->show();
db.close();
return a.exec()
}

QX11EmbedWidget and QX11EmbedContainer

Can one place an arbitrary program (firefox, openoffice, etc...) in a QX11EmbedContainer? The fllowing seems, to work
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QX11EmbedContainer container;
container.show();
QProcess * process = new QProcess(&container);
QString executable("xterm");
QStringList arguments;
arguments << "-into";
arguments << QString::number(container.winId());
process->start(executable, arguments);
int status = app.exec();
process->close();
return status;
}
but the next snippet launches a new window, not what I want
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QX11EmbedContainer container;
container.show();
QProcess * process = new QProcess(&container);
QString executable("konsole");
process->start(executable);
int status = app.exec();
process->close();
return status;
}
The first example work because xterm is able to reparent its top level widget (an X11 window). You tell it to do so with the argument -into <WinId>.
I don't know if Konsole can do that, i don't use it and the man page doesn't seem to talk about this.
But that doesn't mean it is not doable, the X Window system is very flexible and anyone can reparent another window (that's how windows managers add decorations to windows).
Take a look at man 3 XReparentWindow ;-)

Resources