Program crashes when play video with QWebView - c++

Here is the code
#include <QApplication>
#include <QtWebKitWidgets/QWebView>
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QWebView view;
QCoreApplication::addLibraryPath("./plugins");
view.settings()->setAttribute(QWebSettings::PluginsEnabled, true);
// crash here
view.load(QUrl("http://hon.qq.com/act/20140320video/Aluna.html"));
// OK to show youtube
//view.load(QUrl("http://www.youtube.com/watch?v=KMU0tzLwhbE"));
view.show();
return app.exec();
}
It crashes when play video on hon.qq.com, but works well playing video on Youtube.

Related

How to hide QMainWindow and show splashcreen during startup?

I am trying to hide the MainWindow of my Qt desktop app during startup, and to show a splashscreen. Both only happens after the loading phase, even though I call both splash.show() and window.hide() before the loading phase. I tried to split loading phase and constructor, but result is the same. How can I achieve both before the loading phase ?
Update 1
To display the splash screen, I had to add a call to QApplication::processEvents()
Update 2
The black window was actually not the MainWindow, but a ghost window that popped because scrollArea->setVisible(true) was called in the constructor.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap(QStringLiteral(":/ressources/icons/icon.png"));
QSplashScreen splash(pixmap);
splash.show();
MainWindow window; // this loads for 5-6 seconds
a.processEvents();
window.showLoginPrompt();
splash.finish(&window);
return a.exec();
}
Based on your code and some example I could make it run like you are trying to do.
You only need to call your promptLogin function instead.
#include <QApplication>
#include <QTimer>
#include <QSplashScreen>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap("D:\\Projects\\SplashScreen\\TestSplashScreen\\splash.png"));
splash->show();
MainWindow mainWin;
QTimer::singleShot(2500, splash, SLOT(close()));
QTimer::singleShot(2500, &mainWin, SLOT(show()));
return app.exec();
}

Qt translations loaded after window load

I'm trying to setup translations for a qt app but it seems that the translations files are loaded after the window because the only translated text are the ones in messages box
This is the code:
#include <QApplication>
#include <QTranslator>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QCoreApplication::setOrganizationName("Lebbadi");
QCoreApplication::setOrganizationDomain("lebbadi.fr");
QCoreApplication::setApplicationName("zNavigo");
QTranslator translator;
if(translator.load(QLocale(QLocale::French), "app", "_", ":/translations"))
app.installTranslator(&translator);
else
exit(-1);
MainWindow window;
window.show();
return app.exec();
}
Thanks

Run QtVirtualKeyboard using QQuickWidget

I am trying to make the QtVirtualKeyboard example work with QQuickWidget instead of QQuickView. For QuickView, I use the following main.cpp code, which works fine for me:
#include <QQuickView>
#include <QGuiApplication>
#include <QQmlEngine>
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QGuiApplication app(argc, argv);
QQuickView view(QString("qrc:/%2").arg(MAIN_QML));
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.show();
return app.exec();
}
I run into problems, when changing to QQuickWidgets with the following implementation of main.cpp:
#include <QQuickWidget>
#include <QApplication>
#include <QQmlEngine>
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QApplication app(argc, argv);
QQuickWidget w(QString("qrc:/%2").arg(MAIN_QML));
w.setResizeMode(QQuickWidget::SizeRootObjectToView);
w.show();
return app.exec();
}
When I hit the input fields, the virtual keyboard shows up, but when I start typing at the keyboard, I get the message "input method is not set", which seems to be related to the input method plugin. No chars appear in the input fields.
Any ideas? The QML-code didn't change between the above variants of main.cpp
BTW: I am using Linux, gcc, Qt 5.9.0, EGLFS plugin
Thanks for any suggestions!
Regards,
Patrick
Found the solution for QML looking through inputMethod documentation. Following workaround works for me:
TextArea {
...
onActiveFocusChanged: {
if(activeFocus) {
Qt.inputMethod.update(Qt.ImQueryInput)
}
}
}
Works with other controls as well.
Of course InputPanel should be defined in ApplicationWindow like this:
ApplicationWindow {
...
InputPanel {
id: inputPanel
...
}
}

How I show application when open application again Qt

Now, I have 1 application, but I don't want to open application twice, so I using QShareMemory to detect application when open twice.
And my question is: how I show current application in screen when user open application the second ?
int main(int argc, char *argv[]) {
Application a(argc, argv);
/*Make sure only one instance of application can run on host system at a time*/
QSharedMemory sharedMemory;
sharedMemory.setKey ("Application");
if (!sharedMemory.create(1))
{
qDebug() << "123123Exit already a process running";
return 0;
}
/**/
return a.exec();
}
Thanks.
Here's another approach in pure Qt way:
Use QLocalServer and QLocalSocket to check the existence of application and then use signal-slot mechanism to notify the existing one.
#include "widget.h"
#include <QApplication>
#include <QObject>
#include <QLocalSocket>
#include <QLocalServer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString appKey = "applicationKey";
QLocalSocket *socket = new QLocalSocket();
socket->connectToServer(appKey);
if (socket->isOpen()) {
socket->close();
socket->deleteLater();
return 0;
}
socket->deleteLater();
Widget w;
QLocalServer server;
QObject::connect(&server,
&QLocalServer::newConnection,
[&w] () {
/*Set the window on the top level.*/
w.setWindowFlags(w.windowFlags() |
Qt::WindowStaysOnTopHint);
w.showNormal();
w.setWindowFlags(w.windowFlags() &
~Qt::WindowStaysOnTopHint
);
w.showNormal();
w.activateWindow();
});
server.listen(appKey);
w.show();
return a.exec();
}
But if you're using Qt 5.3 on Windows, there's a bug for QWidget::setWindowFlags and Qt::WindowStaysOnTopHint, see https://bugreports.qt.io/browse/QTBUG-30359.
Just use QSingleApplication class instead of QApplication:
https://github.com/qtproject/qt-solutions/tree/master/qtsingleapplication
int main(int argc, char **argv)
{
QtSingleApplication app(argc, argv);
if (app.isRunning())
return 0;
MyMainWidget mmw;
app.setActivationWindow(&mmw);
mmw.show();
return app.exec();
}
It is part of Qt Solutions: https://github.com/qtproject/qt-solutions

How can i create QML canvas in my C++ code and attach video on that? Such as Java Canvas i can

How to create QML such canvas, and attach XOverlay video object on that canvas?
Java:
final Canvas canvas = new Canvas();
canvas.setPreferredSize(new Dimension(200, 200));
XOverlay.wrap(video).setWindowID(canvas);
C++
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/QmlTest1/main.qml"));
// set the window no border, to do full screen live VIDEO
viewer.setWindowFlags(
Qt::CustomizeWindowHint |
Qt::FramelessWindowHint
);
viewer.showExpanded();
return app.exec();
}
My ref:
http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeexamples.html
http://www.scriptol.com/programming/qml.php
Video playback in QML can be done through Qt Mobility's Video element:
http://doc.qt.nokia.com/qtmobility-1.2/qml-video.html