I have this code
class cCanvas : public QQuickPaintedItem
..
..
void cCanvas::paint(QPainter *ppainter)
{
ppainter->setPen(QPen(colorValue()));
ppainter->drawLine(0, 0, rand() % 255 ,100);
}
QML
MultiPointTouchArea {
onPressed {
canvas.update();
}
}
It works, but when drawing each new line, the canvas is cleared, previous line is deleted. But I want the lines to stay.
QML Canvas have requestPaint();
How to call this method for QQuickPaintedItem?
How to correctly create the ability to draw with the mouse on QQuickPaintedItem?
(Drawing on the QML canvas itself is not suitable because of the speed of work, since drawing occurs using calculations)
Qt does not keep a cache of what was painted, so you only see what was painted last. A possible solution is to save the line instructions in a QPainterPath:
#ifndef CCANVAS_H
#define CCANVAS_H
#include <QPainterPath>
#include <QQuickPaintedItem>
class CCanvas : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
QML_ELEMENT
public:
CCanvas(QQuickItem *parent = nullptr);
Q_INVOKABLE void requestPaint();
void paint(QPainter *painter);
const QColor &color() const;
void setColor(const QColor &newColor);
signals:
void colorChanged();
private:
QPainterPath m_path;
QColor m_color;
};
#endif // CCANVAS_H
#include "ccanvas.h"
#include <QPainter>
CCanvas::CCanvas(QQuickItem *parent):QQuickPaintedItem(parent)
{
}
void CCanvas::requestPaint()
{
QPointF start(0, 0);
QPointF end(rand() % 255, 100);
m_path.moveTo(start);
m_path.lineTo(end);
update();
}
void CCanvas::paint(QPainter *painter)
{
painter->setPen(QPen(m_color));
painter->drawPath(m_path);
}
const QColor &CCanvas::color() const
{
return m_color;
}
void CCanvas::setColor(const QColor &newColor)
{
if (m_color == newColor)
return;
m_color = newColor;
emit colorChanged();
update();
}
import QtQuick 2.12
import QtQuick.Window 2.12
import Canvas 1.0
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
CCanvas{
id: canvas
anchors.fill: parent
color: "red"
}
Timer {
interval: 500; running: true; repeat: true
onTriggered: canvas.requestPaint()
}
}
QT += quick
CONFIG += c++11
SOURCES += \
ccanvas.cpp \
main.cpp
HEADERS += \
ccanvas.h
RESOURCES += qml.qrc
CONFIG += qmltypes
QML_IMPORT_NAME = Canvas
QML_IMPORT_MAJOR_VERSION = 1
A similar solution can be done with a QImage.
Related
Here's my main.qml:
import QtQuick 2.0
import A 1.0
Item {
width: 1280
height: 720
OpenGlVideoQtQuick {
}
}
Here's a part of OpenGlVideoQtQuick:
class OpenGlVideoQtQuick : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(qreal t READ t WRITE setT NOTIFY tChanged)
public:
OpenGlVideoQtQuick();
qreal t() const { return m_t; }
void setT(qreal t);
Q_INVOKABLE void initRtspMedia(const QString &uri);
}
How do I call initRtspMedia() from QML right at the creation of the OpenGlVideoQtQuick QML object? I've only seen how can buttons and other things call C++ code, but not how to call it immediately.
For this case you can use Component.onCompleted:
import QtQuick 2.0
import A 1.0
Item {
width: 1280
height: 720
OpenGlVideoQtQuick {
id: opengl_video
Component.onCompleted: opengl_video.initRtspMedia("some uri")
}
}
Or from C++ you can do it with the help of QQmlParserStatus:
class OpenGlVideoQtQuick : public QQuickItem, public QQmlParserStatus
{
Q_OBJECT
Q_PROPERTY(qreal t READ t WRITE setT NOTIFY tChanged)
Q_INTERFACES(QQmlParserStatus)
public:
OpenGlVideoQtQuick();
qreal t() const { return m_t; }
void setT(qreal t);
Q_INVOKABLE void initRtspMedia(const QString &uri);
void classBegin() {}
void componentComplete() {
initRtspMedia("some uri");
}
}
I came across QFontMetrics?
http://doc.qt.io/qt-5/qfontmetrics.html
This gives the height and width of the present font.
I need to run my application with full screen mode on on different monitors for which I am using Scale class. http://doc.qt.io/qt-5/qml-qtquick-scale.html
That returns the height and width of the current screen.
Is there a way to use QFontMetrics or anything else to change the font size according to the monitor size?
ApplicationWindow
{
id: head
visible: true
width: Screen.width
height: Screen.height
title: "Novus Pilot"
property var id: 0;
Draw_on_qimage
{
id: draw_on_qimage
anchors.fill: parent
parent: image
scaleX: head.width / 640
scaleY: head.height / 480
}
}
Draw_on_qimage is a cpp class.
The easiest way is to set QFont as Q_PROPERTY of your item so you can set it from QML:
#ifndef DRAWITEM_H
#define DRAWITEM_H
#include <QPainter>
#include <QQuickPaintedItem>
class DrawItem : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
public:
DrawItem(QQuickItem *parent = Q_NULLPTR):QQuickPaintedItem(parent){}
void paint(QPainter *painter){
painter->setFont(mFont);
painter->drawText(boundingRect(), "Hello");
}
QFont font() const{
return mFont;
}
void setFont(const QFont &font){
if(mFont == font)
return;
mFont = font;
emit fontChanged();
update();
}
signals:
void fontChanged();
private:
QFont mFont;
};
#endif // DRAWITEM_H
To set its size we use the pointSize property of QFont:
DrawItem
{
id: draw_on_qimage
anchors.fill: parent
font.pointSize: some_function(head.width, head.height)
transform: Scale {
xScale: head.width / 640
yScale: head.height / 480
}
}
Where some_function is the function that establishes the relationship between the font size and the size of the window.
I tried to add to a ListView in QML of N Items a way to add and delete a new Item at a given index.
I did the following example, but the problem is that when I move some Items, when I try to insert a new one, the position might be incorrect and I have no clue why. When I check my DataList in my cpp model, positions are correct, however, new or deleted items won't be inserted/deleted at the right position.
It seems that the error occurs when I insert a new Item, then I move it , and then I try to delete this Item or insert an Item next to this New Item.
Here is a simple example (you can run it if you need). I called my Items Data : Blocks
#include "mainwindow.h"
#include <QApplication>
#include <QtQml>
#include <QQuickView>
#include <QQuickWidget>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
main.cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "model.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
void addItem(int index);
~MainWindow();
private slots:
private:
QList<QObject*> dataList;
Ui::MainWindow *ui;
BlockModel model;
int cpt = 0;
};
#endif // MAINWINDOW_H
mainwindow.h
#include <QtQml>
#include <QQuickView>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QQuickWidget"
#include <QStringList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
int nbItems = 5;
for(; cpt < nbItems; cpt ++) {
Block a = Block(QString("Item ")+QString::number(cpt));
model.addBlock(a);
}
ui->setupUi(this);
QQuickWidget *view = new QQuickWidget;
QQmlContext *ctxt = view->rootContext();
ctxt->setContextProperty("myModel", &model);
view->setSource(QUrl::fromLocalFile("main.qml"));
view->setGeometry(0, 200, 600, 400);
view->setResizeMode(QQuickWidget::SizeRootObjectToView);
ui->dockWidget_3->setWidget(view);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.cpp
#include <QAbstractListModel>
#include <QStringList>
#include <qqmlcontext.h>
#include <QDebug>
#include <QStringList>
//![0]
class Block
{
public:
Block(){
}
Block(const QString &name);
QString nameBlock() const;
void setName(QString n) {
m_name = n;
}
private:
QString m_name;
};
class BlockModel : public QAbstractListModel
{
Q_OBJECT
public:
Block* getBlock(QString name);
Q_INVOKABLE void moveBlock(int from,int to);
Q_INVOKABLE void insertBlock(int index);
Q_INVOKABLE void deleteBlock(int index);
enum BlockRoles {
nameRole = Qt::UserRole + 1,
};
BlockModel(QObject *parent = 0);
void setContext(QQmlContext *ctx) {
m_ctx = ctx;
}
void setName(const QString &name);
void addBlock(const Block &Block);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;
private:
QList<Block> m_blocks;
QQmlContext* m_ctx;
int cpt = 0;
};
mode.h
#include "model.h"
#include "qDebug"
Block::Block(const QString &name)
: m_name(name)
{
}
QString Block::nameBlock() const
{
return m_name;
}
BlockModel::BlockModel(QObject *parent)
: QAbstractListModel(parent)
{
}
void BlockModel::addBlock(const Block &Block)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_blocks << Block;
endInsertRows();
}
int BlockModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return m_blocks.count();
}
void BlockModel::moveBlock(int from, int to) {
m_blocks.move(from,to);
}
void BlockModel::insertBlock(int index) {
Block b =(Block(QString("New Item ")+QString::number(cpt)));
beginInsertRows(QModelIndex(),index+1,index+1);
m_blocks.insert(index+1,b);
endInsertRows();
cpt++;
}
void BlockModel::deleteBlock(int index) {
beginRemoveRows(QModelIndex(),index,index);
m_blocks.removeAt(index);
endRemoveRows();
}
QVariant BlockModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= m_blocks.count())
return QVariant();
const Block &Block = m_blocks[index.row()];
if (role == nameRole)
return Block.nameBlock();
return QVariant();
}
//![0]
QHash<int, QByteArray> BlockModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[nameRole] = "nameBlock";
return roles;
}
model.cpp
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.2
import QtQml.Models 2.2
import QtQuick.Controls.Styles 1.4
Rectangle {
id : rootRectangle
visible: true
ScrollView {
anchors.fill:parent
ListView{
id: root
width: parent.width; height: parent.height
property int visualIndex: -1
displaced: Transition {
NumberAnimation { properties: "y"; easing.type: Easing.OutQuad }
}
model: DelegateModel {
id: visualModel
model: myModel
delegate: Component {
MouseArea {
id: delegateRoot
property int visualIndex: DelegateModel.itemsIndex
cursorShape: Qt.PointingHandCursor
width: root.width; height: 100
drag.target: icon
drag.axis: Drag.YAxis
Behavior on height {
PropertyAnimation { duration: 100 }
}
Rectangle {
anchors.top: delegateRoot.top
anchors.left: delegateRoot.left
id: icon
objectName: nameBlock
width: root.width-5; height: 100
color: "skyblue"
radius: 3
Text {
objectName: "rect"
id: title
anchors.fill: parent
anchors.margins: 10
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
text: nameBlock
}
Drag.active: delegateRoot.drag.active
Drag.source: delegateRoot
Drag.hotSpot.x: 36
Drag.hotSpot.y: 36
Button {
id : buttonAdd
text: "Add Block"
anchors{
right: parent.right
top: parent.top
bottom: parent.bottom
margins: 30
}
onClicked: {
myModel.insertBlock(visualIndex)
}
}
Button {
id : buttonDelete
text: "Delete Block"
anchors{
right: buttonAdd.left
top: parent.top
bottom: parent.bottom
margins: 30
}
onClicked: {
myModel.deleteBlock(visualIndex)
}
}
states: [
State {
when: icon.Drag.active
ParentChange {
target: icon
parent: root
}
AnchorChanges {
target: icon;
anchors.horizontalCenter: undefined;
anchors.verticalCenter: undefined
}
}
]
transitions: Transition {
// Make the state changes smooth
ParallelAnimation {
ColorAnimation { property: "color"; duration: 500 }
NumberAnimation { duration: 300; properties: "detailsOpacity,x,contentY,height,width,font.pixelSize,font.bold,visible" }
}
}
}
DropArea {
anchors { fill: parent; margins: 15 }
onEntered: {
visualModel.items.move(drag.source.visualIndex, delegateRoot.visualIndex)
myModel.moveBlock(drag.source.visualIndex,delegateRoot.visualInde)
}
}
}
}
}
}
}
}
main.qml
Do you have any idea of what I am doing wrong ?
Thanks a lot and have a good day !
There are two bugs when moving items. In DropArea.onEntered, if you print out both drag.source.visualIndex and delegateRoot.visualIndex before and after visualModel.items.move, you'll see that values are modified after moving. That means you are moving wrong rows when calling myModel.moveBlock. To fix the problem, save the value before moving items:
DropArea {
anchors { fill: parent; margins: 15 }
onEntered: {
var from = drag.source.visualIndex;
var to = delegateRoot.visualIndex;
myModel.moveBlock(from, to);
}
}
When moving items in C++ model, QAbstractItemModel::beginMoveRows should be called just like insert/remove items. Otherwise the QML DelegateModel cannot correctly display your model. Remember that when implementing BlockModel::moveBlock, the destination row for the model is different from the one for your source list m_blocks. See the last example in QAbstractItemModel::beginMoveRows documentation for detail.
void BlockModel::moveBlock(int from, int to) {
if (from == to)
return;
auto modelFrom = from;
auto modelTo = to + (from < to ? 1 : 0);
beginMoveRows(QModelIndex(), modelFrom, modelFrom, QModelIndex(), modelTo);
m_blocks.move(from,to);
endMoveRows();
}
I got the runtime error below when using my custom popup implementation with Qt 5.7 (Qt 5.7 is necessary because there is introduced Popup and it needs to be compiled with
-developer-build flag so private symbols (QQuickPopup class) are exported).
You can try replace "CPopup" with "Popup" in MyItem.qml and you will see it works, so the problem needs to be somewhere with the inheritance from QQuickPopup in my CustomPopup class.
ERROR: ASSERT: "_currentList.object" in file qml/qqmlobjectcreator.cpp, line 945
https://github.com/qt/qtquickcontrols2/blob/5.7/src/quicktemplates2/qquickpopup_p.h
https://github.com/qt/qtquickcontrols2/blob/5.7/src/quicktemplates2/qquickpopup.cpp
https://github.com/qt/qtdeclarative/blob/dev/src/qml/qml/qqmlobjectcreator_p.h
https://github.com/qt/qtdeclarative/blob/dev/src/qml/qml/qqmlobjectcreator.cpp
PROJECT: popuptest
popuptest.pro
TEMPLATE = app
QT += gui qml quick quickcontrols2
QT_PRIVATE += quick-private core-private gui-private qml-private quickcontrols2-private quicktemplates2-private
CONFIG += c++11
SOURCES += main.cpp \
custompopup.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
custompopup.h
custompopup.h
#ifndef CUSTOMPOPUP_H
#define CUSTOMPOPUP_H
#include <private/qquickpopup_p.h>
class CustomPopup : public QQuickPopup
{
Q_OBJECT
//Q_PROPERTY(QQmlListProperty<QObject> contentData READ contentData FINAL)
//Q_CLASSINFO("DefaultProperty", "contentData")
public:
CustomPopup(QObject *parent = nullptr);
protected:
bool overlayEvent(QQuickItem *item, QEvent *event) override;
private:
/*Q_DISABLE_COPY(CustomPopup)
Q_DECLARE_PRIVATE(QQuickPopup)*/
};
QML_DECLARE_TYPE(CustomPopup)
#endif // CUSTOMPOPUP_H
custompopup.cpp
#include "custompopup.h"
CustomPopup::CustomPopup(QObject *parent) : QQuickPopup(parent)
{
}
bool CustomPopup::overlayEvent(QQuickItem *item, QEvent *event)
{
/*QQuickPopup implementation
Q_D(QQuickPopup);
switch (event->type()) {
case QEvent::KeyPress:
case QEvent::KeyRelease:
case QEvent::MouseMove:
case QEvent::Wheel:
if (d->modal)
event->accept();
return d->modal;
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
if (d->modal)
event->accept();
d->tryClose(item, static_cast<QMouseEvent *>(event));
return d->modal;
default:
return false;
}
*/
// Q_D(QQuickPopup);
return QQuickPopup::overlayEvent(item, event);
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "custompopup.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<CustomPopup>("Custom", 1, 0, "CPopup");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.7
import QtQuick.Controls 1.5
ApplicationWindow {
visible: true
width: 600
height: 480
MyItem {
anchors.fill: parent
}
}
MyItem.qml
import QtQuick 2.0
import QtQuick.Controls 2.0
import Custom 1.0
Item {
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
onClicked: {
menu.x = mouse.x
menu.y = mouse.y
menu.open()
}
}
CPopup {
id: menu
Rectangle {
width: 100
height: 100
color: "red"
}
}
}
I have a problem with QQuickView instantiating components from C++... Here is my code:
Class definition (vviewerqml.h):
class VViewerQml : public QObject
{
Q_OBJECT
public:
explicit VViewerQml(QSettings &systemSettings, QObject *parent = 0);
~VViewerQml();
protected slots:
void onViewStatusChanged(QQuickView::Status status);
protected:
QString _qmlFolder;
QQuickView _view;
};
Class implementation (vviewerqml.cpp):
#include "vviewerqml.h"
VViewerQml::VViewerQml(QSettings &systemSettings, QObject *parent) :
QObject(parent)
{
// Initialize viewer reading from settings file
_qmlFolder = "/my/path/to/qml/files";
// Initialize the source
connect(&_view, SIGNAL(statusChanged(QQuickView::Status)),
this, SLOT(onViewStatusChanged(QQuickView::Status)));
_view.setSource(QUrl::fromLocalFile(QDir(_qmlFolder).filePath("Main.qml")));
// Show the viewer
_view.show();
}
VViewerQml::~VViewerQml()
{
// Close the viewer
_view.close();
}
void VViewerQml::onViewStatusChanged(QQuickView::Status status)
{
if(status == QQuickView::Ready)
{
QQmlComponent *c =
new QQmlComponent(_view.engine(),
QDir(_qmlFolder).filePath("TextLegacy.qml"));
QQuickItem *i = qobject_cast<QQuickItem*>(c->create());
QQmlEngine::setObjectOwnership(i, QQmlEngine::CppOwnership);
i->setParent(_view.rootObject());
i->setVisible(true);
}
}
Here is my Main.qml:
import QtQuick 2.0
Rectangle {
width: 1024
height: 768
color: "#000000"
Text {
x: 0
y: 0
color: "#ffffff"
text: "Main page"
}
}
And here is my TextLegacy.qml:
import QtQuick 2.0
Item {
Rectangle {
x: 0
y: 0
width: 100
height: 50
color: "#ff0000"
}
}
My code works fine until the load of Main.qml: the QML viewer opens on the screen and I can read the text "Main page" (white on black) on my screen... But unluckily I am not able to load the TextLegacy.qml... if I put a breakpoint in onViewStatusChanged function, the execution reaches that point... no visible errors are shown in the debug console... but I am not able to see on the screen the red rectangle provided by TextLegacy.qml...
What am I missing? Can somebody give some help?
Ok, I found the solution on my own: I have confused setParent with setParentItem... The correct code is:
void VViewerQml::onViewStatusChanged(QQuickView::Status status)
{
if(status == QQuickView::Ready)
{
QQmlComponent *c = new QQmlComponent(_view.engine(),
QUrl::fromLocalFile(QDir(_qmlFolder).filePath("TextLegacy.qml")));
QQuickItem *i = qobject_cast<QQuickItem*>(c->create());
QQmlEngine::setObjectOwnership(i, QQmlEngine::CppOwnership);
i->setParent(this);
i->setVisible(true);
i->setParentItem(_view.rootObject());
}
}
Actually i->setParent(this); defines the parent of i as a QObject (for deleting purposes, for example) while i->setParentItem(_view.rootObject()); actually adds the object to the scene, as a child of the scene root object.