How to include QML Oscilloscope example - c++

I am trying to use this source to create an oscilloscope view on my application.
Here is the source i am using which is created by QT team
https://code.qt.io/cgit/qt/qtcharts.git/tree/examples/charts/qmloscilloscope?h=6.2
I have a lot of screens but i want to show this oscilloscope on my homepage with some other stuff. I already created ControlPanel.qml, ScopeView.qml and MultiButton.qml. Instead of main.qml in the project, i have created a qml named ChartTest.qml. Also carried the datasource.cpp and datasource.h to my project. When i initialize it with main.cpp below
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTimeEdit>
#include <QString>
#include <QApplication>
#include "myclass.h"
#include <QQmlContext>
#include <stdio.h>
#include <stdlib.h>
#include <QSqlRecord>
#include "backend.h"
#include <QQmlContext>
#include <QQmlComponent>
#include <QQmlProperty>
#include <QCursor>
#include <QtWidgets/QApplication>
#include <QtQml/QQmlContext>
#include <QtQuick/QQuickView>
#include <QtQml/QQmlEngine>
#include <QtCore/QDir>
#include "datasource.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
bool openGLSupported = QQuickWindow::graphicsApi() == QSGRendererInterface::OpenGLRhi;
if (!openGLSupported) {
qWarning() << "OpenGL is not set as the graphics backend, so AbstractSeries.useOpenGL will not work.";
qWarning() << "Set QSG_RHI_BACKEND=opengl environment variable to force the OpenGL backend to be used.";
}
QQuickView viewer;
#ifdef Q_OS_WIN
QString extraImportPath(QStringLiteral("%1/../../../../%2"));
#else
QString extraImportPath(QStringLiteral("%1/../../../%2"));
#endif
viewer.engine()->addImportPath(extraImportPath.arg(QGuiApplication::applicationDirPath(),
QString::fromLatin1("qml")));
QObject::connect(viewer.engine(), &QQmlEngine::quit, &viewer, &QWindow::close);
DataSource dataSource(&viewer);
viewer.rootContext()->setContextProperty("dataSource", &dataSource);
viewer.rootContext()->setContextProperty("openGLSupported", openGLSupported);
viewer.setSource(QUrl("qrc:/deneme/ChartTest.qml"));
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
myclass KeyEmitter;
SerialPortHandler* sb = SerialPortHandler::getInstance();
DatabaseHandler* db = DatabaseHandler::getInstance();
BackEnd backend = BackEnd(sb,db);
engine.rootContext()->setContextProperty("keyEmitter",&KeyEmitter);
engine.rootContext()->setContextProperty("backendHandler", &backend);
QQmlContext* context = engine.rootContext();
context->setContextProperty("keyEmitter", &KeyEmitter);
const QUrl url(u"qrc:/deneme/main.qml"_qs);
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
viewer.show();
return app.exec();
}
My application starts and works functionally, but viewer(which is oscilloscope QQuickView object) starts in another window. I tried to include it into my QApplication but couldn't handle that. I want to use that QQuickView in my QApplication as a content just like a normal chartview on some page.
Sorry about syntax, i have some backend and keyboard stuff in it. What i want to have is get something like this
instead of this
I read articles about it but none of them solved my problem because not relevant. Couldn't find any good practice.
how does qwt oscilloscope example's samplingthread class work in the project?
Push QML ChartView updates from c++
Qwt oscilloscope example combined with qextserialport
any ideas?

Related

QIcon not showing in QToolBar

I'm a beginner in Qt, currently reading this : https://zetcode.com/gui/qt5/menusandtoolbars/
When I declare QActions in a QToolBar, the QPixmap objects (turned into QIcons) are not showing :
No icons, only text
However, the QPixmap images are actually showing when I declare a QMenu without the toolbar.
I am using Qt6 ; working on Fedora ; no warning shown on my compiler.
simple_menu.hpp
#ifndef SIMPLE_MENU_HPP
#define SIMPLE_MENU_HPP
#include <QMainWindow>
#include <QApplication>
class SimpleMenu : public QMainWindow
{
public:
SimpleMenu(QWidget *parent = nullptr);
};
#endif
simple_menu.cpp
#include "simple_menu.hpp"
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QIcon>
#include <QAction>
SimpleMenu::SimpleMenu(QWidget *parent)
: QMainWindow(parent)
{
QPixmap newpix("new.png");
QPixmap openpix("open.png");
QToolBar *toolbar = addToolBar("main toolbar");
toolbar->addAction(QIcon(newpix), "New File");
toolbar->addAction(QIcon(openpix), "Open File");
}
main.cpp
#include "simple_menu.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SimpleMenu window;
window.resize(350, 250);
window.setWindowTitle("Tuto Toolbar");
window.show();
return app.exec();
}
Maybe the system cannot find your pictures. Please try to put the full file path (like :).
QPixmap newpix("c:\mydoc\pictures\new.png");
To check if the pictures are loaded, you can do something a bit different :
QPixmap newpix;
qDebug() << newpix.load("c:\mydoc\pictures\new.png");
the load method returns true or false in case of loading fail.
Anyway, if you want to embed your icons in the final EXE file, check the Qt resource system
It's a convenient system to achieve what you want to do, and to avoid dealing with storage paths on your local drives.
Good luck !

I am unable to reference QSettings using Qt

I am running a project that uses Qt. I have installed version 5.10.1 (msvc2017_64). I receive an error that the QSettings is undefined. I cannot find the reference in the libraries installed.
#include "mainwindow.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCoreApplication::setApplicationName("Circuit Editor");
QSettings settings;
MainWindow w;
w.setWindowTitle("Circuit Editor");
w.show();
return a.exec();
}
My two cents:
1) You forgot to include the header:
#include <QSettings>
2) You declared a QSetting object but you are not doing anything with it. Is that what you want to do?
Never use #include <QtModule/QClass> format of includes. They hide project misconfiguration. Use #include <QClass> instead.
If you're unsure of what to include, include the entire module and deal with details later: #include <QtModule>.
Thus:
#include "mainwindow.h"
#if 1
// entire module
#include <QtWidgets>
#else
// needed classes
#include <QApplication>
#include <QSettings>
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCoreApplication::setApplicationName("Circuit Editor");
QSettings settings;
MainWindow w;
w.setWindowTitle("Circuit Editor");
w.show();
return a.exec();
}

How do I use Objective-C variables and methods for a Qt window on macOS?

I have the following code in a Qt project, and I want to set the titlebarAppearsTransparent variable for a window to true in Objective-C. The program compiles correctly, but it crashes when it reaches [&w titlebarAppearsTransparent:YES]; Is what I'm trying to do even possible, and if so how do I fix it?
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
#include <QDir>
#include "globals.h"
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <AppKit/NSWindow.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QApplication::setOrganizationName("Siddha Tiwari");
QApplication::setApplicationName("NarwhalEdit");
MainWindow *w = new MainWindow();
[&w titlebarAppearsTransparent:YES];
setTheme(true);
w->show();
return a.exec();
}
It is possible to accomplish this using the native API, as reported here, obtaining the NSWindow pointer from QWidget::window()::winId().
I would also suggest to wrap the code with conditional compiling directives, so it is ignored when compiling for other platforms.
Here is a snippet (assuming w is the pointer to your QMainWindow):
#ifdef Q_OS_MAC
QCoreApplication::setAttribute( Qt::AA_DontCreateNativeWidgetSiblings );
NSView *nsview = ( __bridge NSView * )reinterpret_cast<void *>( w->window()->winId() );
NSWindow *nswindow = [nsview window];
nswindow.titlebarAppearsTransparent = YES;
#endif

Signal and Slot between QML and c++

my problem is, that I can't get a signal and slot connections between a cpp and a qml file.
First of all I've found some solutions in the web, but it doesn't work.
I'm sure, that the mistake is mine, but I didn't find it.
main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QQmlContext>
#include "Hotfolder.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QScopedPointer<cReadJson> jsonReader(new cReadJson);
QScopedPointer<cHotfolder> hotfolder(new cHotfolder);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.rootContext()->setContextProperty("jsonReader", jsonReader.data());
engine.rootContext()->setContextProperty("hotfolder", hotfolder.data());
QObject *topLevel = engine.rootObjects().at(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
QObject::connect(&cHotfolder, SIGNAL(sigNewOrder()), window, SLOT(
// Here is the mistake, that I can't find the Slot in QML
return app.exec();
}
This is my main.cpp file. In this file I found the Signal from the cpp file, but not the Slot in QML.
main.qml:
function bla()
{
console.log("bla")
}
This is the function in my main.qml file.
So where ist mistake?
Many thanks in advance!
Ben
The solution is:
Just connect to the signal from within QML and not from C++
example:
Component.onCompleted: hotfolder.sigNewOrder.connect(bla)

Can't register C++ type to QML system

I'm trying to add C++ type to QML system.
#include <QtGui/QGuiApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include "qtquick2applicationviewer.h"
#include <QQmlApplicationEngine>
class FooBar: public QObject {
Q_OBJECT
};
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine("qml/RBot/main.qml");
qmlRegisterType<FooBar>("io.secorp", 1, 0, "FooBar");
return app.exec();
}
But when I'm trying compile this(i don't import this in .qml file, only testing) I'm geting errors about debugging.
What's wrong?
Thanks.
You're mixing Qt Quick 1 and 2, which is unsupported. The QDeclarative headers are for Quick 1 and the QQml headers are for Quick 2.
Your includes should be:
#include <QtGui/QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include "qtquick2applicationviewer.h"
#include "foobar.h"
The definition of FooBar should be in its own header, and QObject should be included there for moc to work its magic.
For more details, see this already reported bug in Qt:
QTBUG-32138 - Hello World for QtQuick2 does not compile with #include when QML debugging is on