How to take ScreenShot Qt/QML - qt

QtQuick1 example
I can’t make it work on QtQuick2
screenCapture.hpp
#include <QObject>
class QString;
class QQuickView;
class screenCapture : public QObject
{
Q_OBJECT
public:
explicit screenCapture(QQuickView *parent = 0);
public slots:
void capture(QString const &path) const;
private:
QQuickView *currentView_;
};
screenCapture.cpp
#include <QPixmap>
#include <QQuickView>
#include <QString>
#include "screenCapture.hpp"
screenCapture::screenCapture(QQuickView *currentView) :
QObject(0), currentView_(currentView)
{
}
void screenCapture::capture(QString const &path) const
{
QPixmap::grabWidget(currentView_).save(path);
}
main.cpp
#include <QGuiApplication>
#include <QQuickPaintedItem>
#include <QQuickView>
#include <QQmlContext>
#include "screenCapture.hpp"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<screenCapture>("Image", 1, 0, "ScreenCapture");
qmlRegisterType<saveAbleImage>("Image", 1, 0, "SaveAbleImage");
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QStringLiteral("/Users/Qt/program/experiment_apps_and_libs/funnyCamera/qml/funnyCamera/main.qml"));
view.show();
screenCapture screenClass(&view);
view.rootContext()->setContextProperty("screenObject", &screenClass);
return app.exec();
}
main.qml
import QtQuick 2.0
Rectangle{
id : root
width : 1024
height : 768
MouseArea{
anchors.fill: root
onClicked: {
console.log("save image")
screenObject.capture("Pictures/saveTest.jpg")
}
}
}
error message
QPixmap::grabWidget is deprecated, use QWidget::grab() instead
QMetaObject::invokeMethod: No such method QQuickView::grab(QRect)

I found a solution, pretty simple, change the function “capture” to
void screenCapture::capture(QString const &path) const
{
QImage img = currentView_->grabWindow();
img.save(path);
}

Related

Couldn't render OpenGLES context in Qt

I am trying to use OpenGLES in a desktop platform. I have a class whose name is GraphicsViewer which is derived from QOpenGLWidget. When I set the QSurfaceFormat::renderableType to QSurfaceFormat::OpenGLES, I get the following errors and nothing is drawn on the widget. Can you help me to create an OpenGLES context in a desktop platform? My OS is Windows 10 and my Qt version is 5.12.3.
Errors:
QOpenGLWidget: Failed to create context
QOpenGLWidget: Failed to create context
qt.qpa.backingstore: composeAndFlush: QOpenGLContext creation failed
QOpenGLWidget: Failed to create context
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed
qt.qpa.backingstore: composeAndFlush: makeCurrent() failed
Here is my codes...
main.cpp
#include "QtOpenGLES.h"
#include <QApplication>
#include <QSurfaceFormat>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSurfaceFormat f;
f.setVersion(2, 0);
f.setRenderableType(QSurfaceFormat::OpenGLES);
QSurfaceFormat::setDefaultFormat(f);
QtOpenGLES w;
w.show();
return a.exec();
}
QtOpenGLES.h
#ifndef QTOPENGLES_H
#define QTOPENGLES_H
#include <QMainWindow>
namespace Ui {
class QtOpenGLES;
}
class QtOpenGLES : public QMainWindow
{
Q_OBJECT
public:
explicit QtOpenGLES(QWidget *parent = nullptr);
~QtOpenGLES();
private:
Ui::QtOpenGLES *ui;
};
#endif // QTOPENGLES_H
QtOpenGLES.cpp
#include "QtOpenGLES.h"
#include "ui_QtOpenGLES.h"
QtOpenGLES::QtOpenGLES(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::QtOpenGLES)
{
ui->setupUi(this);
}
QtOpenGLES::~QtOpenGLES()
{
delete ui;
}
GraphicsViewer.h
#ifndef GRAPHICSVIEWER_H
#define GRAPHICSVIEWER_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
class GraphicsViewer : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
GraphicsViewer(QWidget* parent = nullptr);
// QOpenGLWidget interface
protected:
void initializeGL();
void paintGL();
};
GraphicsViewer.cpp
#include "GraphicsViewer.h"
#include <QtOpenGL>
GraphicsViewer::GraphicsViewer(QWidget *parent) :
QOpenGLWidget (parent)
{
}
void GraphicsViewer::initializeGL()
{
initializeOpenGLFunctions();
}
void GraphicsViewer::paintGL()
{
glClearColor(1, 0, 0, 1);
}

How to update qml text from two different cpp?

I have an qml project.
There is a text(named id:cnt) on StackPage.qml and I need to update this text from firstclass.cpp and secondclass.cpp.
Q_PROPERTY defines are on firstclass.h and setCntText function is on firstclass.cpp.
I update the text from firstclass.cpp by setCntText(i) and try to update from secondclass.cpp by calling setCntText(0).
I can set the m_cntText variable from secondclass but couldnt update qml text(named id:cnt).
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "firstclass.h"
#include "secondclass.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
firstClass frmFirstClass;
engine.rootContext()->setContextProperty("frmFirstClass",&frmFirstClass);
secondClass frmSecondClass;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
firstclass.cpp
#include "firstclass.h"
firstClass::firstClass(QObject *parent) : QObject(parent)
{
QTimer *timer1 = new QTimer();
connect(timer1, &QTimer::timeout, [=]() {setter1();});
timer1->start(2000);
}
int firstClass::cntText() const
{
return m_cntText;
}
void firstClass::setCntText(int cntText)
{
if (m_cntText == cntText)
return;
m_cntText = cntText;
emit cntTextChanged(m_cntText);
}
void firstClass::setter1()
{
static int i = 0;
i++;
qDebug() <<"counter1 : " << i;
setCntText(i);
}
secondclass.cpp
#include "secondclass.h"
firstClass frmFirstClass;
secondClass::secondClass(QObject *parent) : QObject(parent)
{
QTimer *timer = new QTimer();
timer->setSingleShot(true);
timer->start(1000);
connect(timer, &QTimer::timeout, [=]() {
QTimer *timer2 = new QTimer(this);
connect(timer2,SIGNAL(timeout()),this,SLOT(setter2()));
timer2->start(2000);
timer->deleteLater();
} );
}
void secondClass::setter2()
{
frmFirstClass.setCntText(0);
qDebug() << "Checking m_cntText = " << frmFirstClass.m_cntText;
}
firstclass.h
#ifndef FIRSTCLASS_H
#define FIRSTCLASS_H
#include <QObject>
#include <QTimer>
#include <QDebug>
class firstClass : public QObject
{
Q_OBJECT
Q_PROPERTY(int cntText READ cntText WRITE setCntText NOTIFY cntTextChanged)
public:
explicit firstClass(QObject *parent = nullptr);
int cntText() const;
int m_cntText;
signals:
void cntTextChanged(int cntText);
public slots:
void setCntText(int cntText);
private slots:
void setter1();
};
#endif // FIRSTCLASS_H
secondclass.h
#ifndef SECONDCLASS_H
#define SECONDCLASS_H
#include <QObject>
#include <QTimer>
#include <QDebug>
#include "firstclass.h"
extern firstClass frmFirstClass;
class secondClass : public QObject
{
Q_OBJECT
private:
public:
explicit secondClass(QObject *parent = nullptr);
signals:
public slots:
private slots:
void setter2();
};
#endif // SECONDCLASS_H
main.qml
import QtQuick 2.10
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle{
anchors.fill: parent
color: "black"
StackView{
anchors.fill: parent
initialItem : stackPage
}
Component{
id:stackPage
StackPage{}
}
}
}
StackPage.qml
import QtQuick 2.0
Item {
Rectangle{
anchors.centerIn: parent
width: 200
height: 200
color: "orange"
Text {
id: cnt
text: frmFirstClass.cntText
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font.pointSize: 40
anchors.fill: parent
}
}
}
Your extern usage is incomplete, so you are effectively creating two instances of firstClass. As extern is a bit misleading at poitns, I suggest you to give a pointer to the "one-and-only" firstClass when creating secondClass (in main). This also gives the code a better representation of intended hierarchy.
Update the following files:
secondClass.h
#include "firstClass.h"
class secondClass : public QObject
{
Q_OBJECT
private:
public:
explicit secondClass(firstClass *theFirstClass, QObject *parent = nullptr);
signals:
public slots:
private slots:
void setter2();
private:
firstClass *myFirstClass_ = nullptr;
};
secondClass.cpp
#include "secondclass.h"
secondClass::secondClass(firstClass *theFirstClass, QObject *parent)
: QObject(parent)
, myFirstClass(theFirstClass)
{
QTimer *timer = new QTimer();
timer->setSingleShot(true);
timer->start(1000);
connect(timer, &QTimer::timeout, [=]() {
QTimer *timer2 = new QTimer(this);
connect(timer2,SIGNAL(timeout()),this,SLOT(setter2()));
timer2->start(2000);
timer->deleteLater();
} );
}
void secondClass::setter2()
{
myFirstClass->setCntText(0);
qDebug() << "Checking m_cntText = " << myFirstClass->m_cntText;
}
main.cpp
int main(int argc, char *argv[])
{
...
firstClass frmFirstClass;
engine.rootContext()->setContextProperty("frmFirstClass", &frmFirstClass);
secondClass frmSecondClass(&frmFirstClass);
...
}
Disclaimer: I did not try to compile this

accessing a ui.label from main.cpp in QT

i am trying to take a feedback data which is published from arduino and subscribed to my GUI created in QT.
Now i have main.cpp and mainwindow.cpp.
main.cpp
#include <QtGui>
#include <ros/ros.h>
#include <QApplication>
#include "../include/abc/main_window.hpp"
#include "std_msgs/String.h"
#include <std_msgs/UInt16.h>
#include <QMainWindow>
#include <std_msgs/Float32.h>
void chatterCallback(const std_msgs::UInt16 &fb_msg){
ROS_INFO("Feedback: [%f]", fb_msg.data);
ui.label_6->setText(QString("%1").arg(fb_msg.data));
}
int main(int argc, char **argv) {
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("feedback",1000, chatterCallback);
ros::spinOnce();
QApplication app(argc, argv);
abc::MainWindow w(argc,argv);
w.show();
w.setWindowTitle("GUI for Controlling Servo Motor");
app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
int result = app.exec();
return result;}
mainwindow.cpp
namespace abc {
using namespace Qt;
QSerialPort *serial;
MainWindow::MainWindow(int argc, char** argv, QWidget *parent)
: QMainWindow(parent)
, qnode(argc,argv)
{
ui.setupUi(this);
}
MainWindow::~MainWindow() {}
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
ui.label_5->setText(QString("%1").arg(value));
msg.data = ui.label_5->text().toUInt();
ROS_INFO("%d", msg.data);
chatter_pub.publish(msg);
ros::spinOnce();
}
main_window.hpp
#ifndef abc_MAIN_WINDOW_H
#define abc_MAIN_WINDOW_H
#include <QtGui/QMainWindow>
#include "ui_main_window.h"
#include "qnode.hpp"
#include <QtSerialPort/QSerialPort>
#include <ros/ros.h>
#include "std_msgs/UInt16.h"
namespace abc {
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(int argc, char** argv, QWidget *parent = 0);
~MainWindow();
public Q_SLOTS:
private:
Ui::MainWindowDesign ui;
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise <std_msgs::UInt16> ("chatter", 1000);
QSerialPort *arduino;
QNode qnode;
};
} // namespace abc
#endif // abc_MAIN_WINDOW_H
Now when i run this code, it shows me that UI is not declared in main.cpp.
I wanted to display the data from feedback to label_6 (TextBox). The data is only available in main.cpp, any suggestions are highly appreciable.
Thanks in advance.
You will have to pass the fb_msg.data as an argument to the MainWindow constructor. Then you can set the UI element in the MainWindow thread.
Alternatively once you create the MainWindow instance in your main, you can emit a signal which you catch and process in the MainWindow thread.
You cannot modify the UI elements from a different thread.

QQmlListProperty : Cannot assign to non-existent property "lon" lon: "3"

I wish to see how QQmlListProperty is used. I tried the following but I am not sure if this is the correct way to do it. I got an error shown in the title.
aa.h
#ifndef IMO
#define IMO
#include <QQmlListProperty>
#include "DummyClass.h"
class SubClass: public QObject
{
Q_OBJECT
Q_PROPERTY (QQmlListProperty <DummyClass> functionWhat READ functionWhat)
public:
SubClass (QObject *parent = 0);
QQmlListProperty <DummyClass> functionWhat()
{
return QQmlListProperty <DummyClass> (this, dummyList);
}
private:
QList <DummyClass*> dummyList;
};
#endif
aa.cpp
#include "aa.h"
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
SubClass :: SubClass (QObject *parent) : QObject (parent)
{
qDebug ("In Constructor.");
}
void appendDummies (QQmlListProperty<DummyClass> *property, DummyClass *dc)
{
qDebug ("Do nothing. Can't add to a directory using this method.");
}
DummyClass.h
#ifndef IM
#define IM
#include <QObject>
class DummyClass : public QObject
{
private:
Q_OBJECT
Q_PROPERTY (bool functionWhat READ functionWhat WRITE functionSetWhat)
public:
DummyClass (QObject *parent = 0) {}
float lat; float lon;
bool functionWhat ()
{
qDebug ("In functionWhat");
}
public slots:
void functionSetWhat (bool arg)
{
qDebug ("In functionWhatSlot");
}
signals:
void functionWhatChanged (bool arg);
};
#endif
main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "/home/anisha/qmllistproperties/DummyClass.h"
#include <QtQml>
#include "/home/anisha/qmllistproperties/aa.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
const char* ocuui = "OCUUI"; // #uri OCUUI
qmlRegisterType <DummyClass> (ocuui, 1, 0, "DummyClass");
qmlRegisterType <SubClass> (ocuui, 1, 0, "SubClass");
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/untitled/main.qml"));
viewer.showExpanded();
return app.exec();
}
main.qml
import QtQuick 2.0
import OCUUI 1.0
Rectangle {
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
SubClass
{
functionWhat:
[
DummyClass
{
lat: "2"
lon: "3"
}
]
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}
You seem to be missing the property declarations for lat and lon properties in your DummyClass:
Q_PROPERTY (int lat READ getLat WRITE setLat)
Q_PROPERTY (int lon READ getLon WRITE setLon)
You should study this example.

failure to initialize multiple windows using qt

I've been struggling with the problem of initializing of multiple forms in Qt under Windows (the version is 5.0.1 but I don't reckon this to matter much). Long story short, my application contains of 2 forms the one of which is supposed to be called on clicking the button on the another one.
The code runs as follows:
1) main.cpp:
#include "main.h"
#include <QApplication>
FmNewWord fmNewWord;
MainWindow w;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
w.show();
return a.exec();
}
2) main.h:
#ifndef MAIN_H
#define MAIN_H
#include "mainwindow.h"
#include "fmnewword.h"
extern FmNewWord fmNewWord;
extern MainWindow w;
#endif // MAIN_H
3) mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "ui_mainwindow.h"
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
signals:
void sgnl_new_word();
public:
explicit MainWindow(QWidget *parent = 0);
protected:
void changeEvent(QEvent *e);
private slots:
void on_commandLinkButton_clicked();
void on_miExit_triggered();
void on_miNewWord_triggered();
};
#endif // MAINWINDOW_H
4) mainwindow.cpp:
#include "main.h"
#include <Windows.h>
extern FmNewWord fmNewWord;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_commandLinkButton_clicked()
{
MessageBox(0, 0, 0, 0);
}
void MainWindow::on_miExit_triggered()
{
this->close();
}
void MainWindow::on_miNewWord_triggered()
{
fmNewWord.show();
}
5) fmnewword.cpp:
#include "fmnewword.h"
FmNewWord::FmNewWord(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this); /// here the app crashes with parent == NULL
}
void FmNewWord::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi(this);
break;
default:
break;
}
}
6) fmnewword.h:
#ifndef FMNEWWORD_H
#define FMNEWWORD_H
#include "ui_fmnewword.h"
class FmNewWord : public QMainWindow, private Ui::FmNewWord
{
Q_OBJECT
public:
explicit FmNewWord(QWidget *parent = 0);
protected:
void changeEvent(QEvent *e);
};
#endif // FMNEWWORD_H
The code keeps crashing during inititialization in fmnewword.cpp on the line shown. Could anybody point out the reason? Thanks.
First of all, QApplication must be created before any widgets.
Your code should look llike this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
And that's how the second form must be created:
class FmNewWord;
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
...
private slots:
void on_miNewWord_triggered();
private:
FmNewWord *fmNewWord;
};
void MainWindow::on_miNewWord_triggered()
{
fmNewWord = new FmNewWord;
fmNewWord->show();
}

Resources