Ho creato un'applicazione tenendo automatizzato screenshot ogni volta che la fonte di un loader sta cambiando! Sto cercando di collegare QML segnale (screenshot()) a un CPP slot (take_screenshot()). Tuttavia, take_screenshot() la funzione non è mai stato chiamato, anche se l'origine del caricatore è di essere cambiato e ho ricevuto il log della console, come pure! Qui è il mio cpp.h
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(std::shared_ptr<QQmlApplicationEngine> engin)
: m_engine(engin)
{
QObject* root = m_engine->rootObjects()[0];
Q_ASSERT(root != nullptr);
QObject::connect(root, SIGNAL(screenshot()),
this, SLOT(take_screenshot()));
}
public slots:
int take_screenshot()
{
QString currentDateTime = QDateTime::currentDateTime().toString( "MMMM d, yyyy, HH:mm:ss");
QScreen *screen;
QPixmap pixmap;
screen = QGuiApplication::primaryScreen();
pixmap = screen->grabWindow(0);
QString screenshotPath = QDir::currentPath();
QString screenShotFileName = QString("%1/%2.png").arg(screenshotPath).arg(currentDateTime);
if (!screenShotFileName.isEmpty())
{
pixmap.save(screenShotFileName);
return 1;
}
else
return 0;
}
private:
std::shared_ptr<QQmlApplicationEngine> m_engine;
};
E qui è il mio main.qml
Window {
id: mainWindow
visible: true
width: 640
height: 480
title: qsTr("Hello World")
signal screenshot()
Loader {
id: pageLoader
objectName: "pageLoader"
anchors.fill: parent
onSourceChanged: {
console.log("onSourceChanged_source :", pageLoader.source)
mainWindow.screenshot()
}
Component.onCompleted:{
console.log("onCompleted!", pageLoader.source)
mainWindow.screenshot()
}
}
}
Tutte le idee in merito al problema, può essere utile! Grazie in anticipo!
mainWindow.screenshot()
. Penso che è destinato a cambiareUtils.screenshot()
, giusto?