When I create a blank QML application in Qt, the IDE automatically creates the main.cpp like below. What I don't understand is why in the if statement it checks for obj and url? why it doesn't just check for obj alone? Why do we need this checking at first place?
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
As you can read in the qt-docs:
void QQmlApplicationEngine::objectCreated(QObject *object, const QUrl &url)
...object contains a pointer to the loaded object, otherwise the pointer is NULL.
The url to the component the object came from is also provided.
So the signal QQmlApplicationEngine::objectCreated passes obj and objUrl to the lambda. In the lambda obj is checked for nullptr and objUrl is checked if it was created from the correct url.
If one of the two conditions is not met you know the object was not created correctly.
We don't need this whole check. It would be enough to check if obj is not a null pointer, since that would mean that load failed, as states the documentation.
The second check (url == objUrl) is superfluous and will never be true. The only way this fails, is if the url to qml file was provided as a QString, because:
Note: If the path to the component was provided as a QString containing a relative path, the url will contain a fully resolved path to the file.
But in that case it wouldn't be a good idea to kill the application.
Feel free to submit a bug to Qt devs :)
Related
I have a code as simple as this:
int main() {
QUrl url("http://google.com");
if (!QDesktopServices::openUrl(url) )
qDebug() << "Failed to open url";
return 0;
}
Running the code gives "Failed to open url". Tried on Ubuntu with Qt 5.5.1 and on MS Windows with Qt 5.7. No difference.
Local files also do not open:
int main() {
QString file = "/home/user/testfile.pdf";
if (!QDesktopServices::openUrl( QUrl::fromLocalFile(file) ) )
qDebug() << "Failed to open file";
return 0;
}
Again, "Failed to open file". On both Ubuntu and Windows. I can see some discussion in stackoverflow about openUrl, but they are concerned with specific features, such as failing to open urls with spaces, etc. But here it just doesn't work at all, on two independent platforms. What do I miss?
QDesktopServices is part of the Qt GUI module. Therefore, in order to use any function related to QDesktopServices, you will need to instantiate at least a QGuiApplication :
Since the QGuiApplication object does so much initialization, it must
be created before any other objects related to the user interface are
created.
In fact, you can create a QApplication (as #Alex Spataru suggested), since it inherits QGuiApplication. To make your code work, you just need this :
int main(int argc, char *argv[]) {
QApplication app(argc, argv); // just this line
QUrl url("http://google.com");
if ( !QDesktopServices::openUrl(url) )
qDebug() << "Failed to open url";
return 0;
}
QUrl class can be used to open both local or online file. I used QLineEdit to take URL as QString and give it to QUrl. The program can access both local and online file. My point question is: is there any official way to automatically detect if the given url is local or online and add http:// automatically if the url is online?
For example, if user type www.google.com, it should be online and should be added http:// before it being processed. If user type /home/username/somepath it should be offline.
Of course a little if and else thing with string pattern check can be used for this purpose. My question is, if there's officially supported way to do something like this from Qt5.
You can use QUrl:fromUserInput(...) for that purpose.
QString first("qt-project.org");
QString second("ftp.qt-project.org");
QString third("hostname");
QString fourth("/home/user/test.html");
qDebug() << QUrl::fromUserInput(first); // QUrl( "http://qt-project.org" )
qDebug() << QUrl::fromUserInput(second); // QUrl( "ftp://ftp.qt-project.org" )
qDebug() << QUrl::fromUserInput(third); // QUrl( "http://hostname" )
qDebug() << QUrl::fromUserInput(fourth); // QUrl( "file:///home/user/test.html" )
I want to use QFtp for the first time and googled a lot to find out how it should be used. This, among others is a typical example:
#include <QCoreApplication>
#include <QFtp>
#include <QFile>
int main(int argc, char ** argv)
{
QCoreApplication app(argc, argv);
QFile *file = new QFile( "C:\\Devel\\THP\\tmp\\test.txt" );
file->open(QIODevice::ReadWrite);
QFtp *ftp = new QFtp();
ftp->setTransferMode(QFtp::Active);
ftp->connectToHost("ftp.trolltech.com");
ftp->login();
ftp->cd("qt");
ftp->get("INSTALL",file);
ftp->close();
QObject::connect(ftp, SIGNAL(done(bool)), &app, SLOT(quit()));
int ret = app.exec();
delete ftp;
delete file;
return ret;
}
The question:
As far as I understood, the QCoreApplication app is needed to handle the "done" signal, emmited upon finalization of ftp-get. Now, the ftp->get is called before the connect and even before the app handler is running at all (app.exec() is called afterwards).
What happens, if the file transfer has completed already before the "connect" statement? In fact, that will not happen, but I could put an artificial delay of, say 1 minute between ftp->close() and the connect(...). During this time, the ftp get will surely be finished. What would happen?
Note that QFtp is really only meant for legacy Qt applications and it is now suggested that QNetworkAccessManager and QNetworkReply are used instead, as detailed in the Qt documentation.
That being said, with your connect call being positioned after the connection to the FTP site and retrieving of the file, should the file be downloaded first, the 'quit' signal would never be reached. If you make the connection straight after creating the QFtp object, then this won't be an issue: -
QFtp *ftp = new QFtp();
QObject::connect(ftp, SIGNAL(done(bool)), &app, SLOT(quit()));
This guarantees that the 'quit' slot will be called when the QFtp object emits the 'done' signal.
QFtp *ftp = new QFtp();
QObject::connect(ftp, SIGNAL(done(bool)), &app, SLOT(quit()));
ftp->setTransferMode(QFtp::Active);
ftp->connectToHost("ftp.trolltech.com");
ftp->login();
ftp->cd("qt");
ftp->get("INSTALL",file);
ftp->close();
int ret = app.exec();
In reality though, I would expect the connect in your example would complete before the machine had time to negotiate a connection to another server, login and start the download of the file.
I'm developing an application for Symbian S60 phones using the Qt Nokia SDK, which sends requests and receives responses from a webservice in every view i have.
The problem with this, is that it always asks the user to choose a accesspoint.
So what i want is to choose an accesspoint when the application starts, and use that throughout the application.
So i found this example: http://wiki.forum.nokia.com/index.php/How_to_set_default_access_point_using_Qt_Mobility_APIs
but i got following error:
undefined reference to 'QtMobility::QNetworkConfigurationManager::QNetworkConfigurationManager(QObject*)
i'm also getting more of these errors from other classes from QMobillity, like:
undefined reference to 'QtMobility::QNetworkSession::open()
.pro file:
CONFIG += mobility
MOBILITY += bearer
header:
#include <qmobilityglobal.h>
#include <QtNetwork>
#include <QNetworkSession>
#include <QNetworkConfigurationManager>
QTM_USE_NAMESPACE;
cpp file:
QNetworkConfigurationManager manager;
const bool selectIap = (manager.capabilities()& QNetworkConfigurationManager::CanStartAndStopInterfaces);
QNetworkConfiguration defaultIap = manager.defaultConfiguration();
if(!defaultIap.isValid() && (!selectIap && defaultIap.state() != QNetworkConfiguration::Active))
{
qDebug() << "Network access point NOT found";
// let the user know that there is no access point available
msgBox->setText(tr("Error"));
msgBox->setInformativeText(tr("No default access point available"));
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setDefaultButton(QMessageBox::Ok);
msgBox->topLevelWidget();
msgBox->exec();
}
else
{
qDebug() << "Network access point found and chosen";
}
session = new QNetworkSession(defaultIap,this);
session->open();
Anyone got an idea of what could be wrong?
Have you tried adding this to the .PRO file?
CONFIG += network
Is there a way to get the MIME type of a file in Qt?
I am writing an application that needs to find the MIME type of a given file.
Qt 5 has added support for MIME types:
http://doc.qt.io/qt-5/qmimedatabase.html
QString path("/home/my_user/my_file");
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
QMimeDatabase db;
QMimeType type = db.mimeTypeForFile(path);
qDebug() << "Mime type:" << type.name();
#endif
See also: http://doc.qt.io/qt-5/qmimetype.html
#include <QMimeDatabase>
QString mimeType( const QString &filePath ){ return QMimeDatabase().mimeTypeForFile( filePath ).name(); }
You need to use 3rd party libraries for this purpose, there is no mime-type guessing support in Qt itself. On Linux/Unix you could use libmagic.