Skip to content

QML and Qt Quick

Looking for The Bling Thing(tm)? Post here!
20.1k Topics 77.6k Posts
  • Best approach for viewing C++ logging in QML

    Unsolved
    2
    0 Votes
    2 Posts
    2k Views
    W
    I did it with second approach, but it's not a string buffer but a log emitter, which just store latest log and alerts to QML by signal. The message handler LogManager::RedirectToStdout and LogManager::RedirectToFile change the log emitter when called. /// LogManager.hpp #ifndef LOGMANAGER_HPP #define LOGMANAGER_HPP #include <QObject> #include <QString> #include <QDateTime> #include <QFile> #include <QMutex> class LogEmitter : public QObject { friend class LogManager; Q_OBJECT Q_PROPERTY(QString time MEMBER m_Time NOTIFY changed) Q_PROPERTY(QString type MEMBER m_Type NOTIFY changed) Q_PROPERTY(QString message MEMBER m_Message NOTIFY changed) public: explicit LogEmitter(QObject *parent = nullptr) {} void Set(const QString &Time, const QString &Type, const QString &Message) { m_Time = Time; m_Type = Type; m_Message = Message; m_Full = m_Time + " " + m_Type + ": " + m_Message; emit changed(); } QString GetString() { return m_Full; } signals: void changed(); private: QString m_Time; QString m_Type; QString m_Message; QString m_Full; }; class LogManager : public QObject { Q_OBJECT public: static LogManager &Initialize(); LogEmitter *GetEmitter(); private: static void RedirectToStdout(QtMsgType type, const QMessageLogContext &context, const QString &msg); static void RedirectToFile(QtMsgType type, const QMessageLogContext &context, const QString &msg); private: explicit LogManager(QObject *parent = nullptr); private: static LogEmitter *m_LogEmitter; }; #endif // LOGMANAGER_HPP #include "LogManager.hpp" LogEmitter *LogManager::m_LogEmitter = new LogEmitter; namespace LogManagerVars { QDateTime DateTime; QFile File; }; LogManager &LogManager::Initialize() { LogManager *SingletonObject = new LogManager; if (qEnvironmentVariableIsEmpty(qgetenv("QTDIR"))) // If Qt creator then not write to file { qInstallMessageHandler(RedirectToStdout); } else { LogManagerVars::DateTime = QDateTime::currentDateTime(); LogManagerVars::File.setFileName(LogManagerVars::DateTime.toString("yyyy.MM.dd") + ".log"); qInstallMessageHandler(RedirectToFile); } return *SingletonObject; } LogEmitter *LogManager::GetEmitter() { return m_LogEmitter; } void LogManager::RedirectToStdout(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QByteArray localMsg = msg.toLocal8Bit(); QString TypeStr; switch (type) { case QtDebugMsg: fprintf(stderr, "Debug: %s \n", localMsg.constData()); TypeStr = "DEBUG"; break; case QtInfoMsg: fprintf(stderr, "Info: %s \n", localMsg.constData()); TypeStr = "INFO"; break; case QtWarningMsg: fprintf(stderr, "Warning: %s (%s:%u)\n", localMsg.constData(), context.file, context.line); TypeStr = "WARNING"; break; case QtCriticalMsg: fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); TypeStr = "ERROR"; break; case QtFatalMsg: fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); TypeStr = "ERROR"; abort(); } m_LogEmitter->Set(QDateTime::currentDateTime().toString("[hh:mm:ss]"), TypeStr, msg); } void LogManager::RedirectToFile(QtMsgType type, const QMessageLogContext &context, const QString &msg) { if (type != QtInfoMsg && type != QtCriticalMsg) return; static QMutex Mutex; QMutexLocker lock(&Mutex); if (!LogManagerVars::File.isOpen()) if (!LogManagerVars::File.open(QIODevice::Append | QIODevice::Text)) return; m_LogEmitter->Set(QDateTime::currentDateTime().toString("[hh:mm:ss]"), (type == QtInfoMsg ? "INFO" : "ERROR"), msg); QString Output = m_LogEmitter->GetString() + "\n"; LogManagerVars::File.write(Output.toUtf8()); LogManagerVars::File.close(); } LogManager::LogManager(QObject *parent) { } /// main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "LogManager.hpp" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/src/qml/MainWindow.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); LogManager *logger = &LogManager::Initialize(); LogEmitter *log = logger->GetEmitter(); engine.rootContext()->setContextProperty("log", log); engine.load(url); return app.exec(); } /// QML Popup { visible: false modal: true focus: false closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent padding: 10 ListModel { id: listModel function push(newElement) { if (listModel.count >= 50) listModel.remove(0); listModel.append(newElement); } } Component { id: delegate TextInput { selectByMouse: true text: timeStr + " " + typeStr + ": " + messageStr color: typeStr == "INFO" ? "black" : (typeStr == "ERROR" ? "red" : "blue") } } ListView { id:lst height: parent.height*0.8 width: parent.width/2 anchors.verticalCenter: parent.verticalCenter model: listModel delegate: delegate } Connections { target: log onChanged: { listModel.push({ timeStr: log.time, typeStr: log.type, messageStr: log.message }) } } }
  • how in Qt Quick draw line ???

    Unsolved 3d canvas 2d graphics quick
    2
    0 Votes
    2 Posts
    510 Views
    jsulmJ
    @timob256 I entered "Qt Quick draw line" in Google and got this: https://doc.qt.io/qt-5/qml-qtquick-pathline.html
  • How to capture Hide key event in virtual Keypad

    Solved
    2
    0 Votes
    2 Posts
    325 Views
    P
    @Praveen-Illa This was solved in another forum. Please refer to below link for the solution https://stackoverflow.com/questions/69814505/how-to-capture-hide-key-event-in-qt-virtualkeyboard
  • Missing .dll Error Running QML/PySide/QtQuick stock application.

    Unsolved
    1
    0 Votes
    1 Posts
    316 Views
    No one has replied
  • Qt Design Studio and Qt Creator Emulation Layer Fails Mac M1

    Unsolved design studio emulator qml
    1
    0 Votes
    1 Posts
    314 Views
    No one has replied
  • How to save files from and to usb ?

    Solved
    12
    0 Votes
    12 Posts
    2k Views
    JoeCFDJ
    @DiegOne said in How to save files from and to usb ?: QFileDialog::getExistingDirectory the usb path is here /media/username/ There may be more to be done: progress bar(hard to impossible to do on linux if file size is too big) stop in the middle of copying QFile::copy(srcPath, dstPath) is not optimized remind the user: do not remove usb in the middle of copy since usb can be damaged. it is better to add a button to let users remove usb safely.
  • Choose and change user settings in QML

    Unsolved
    1
    0 Votes
    1 Posts
    169 Views
    No one has replied
  • To access QList<QPointF> property value in cpp side using qmetaProperty

    Unsolved
    3
    0 Votes
    3 Posts
    528 Views
    GrecKoG
    Are you sure it's emply and not just not displayed in qDebug? what about qDebug() << meta_property.read(obj).value<QList<QPointF>>();?
  • Customize qtChart

    Solved
    2
    0 Votes
    2 Posts
    316 Views
    S
    Ok I found it : [image: 9a97c36e-9a72-4f52-b810-3518ddbf585d.JPG] I call this function each time the QSliderRange change : void Controller::updateBackground(QQuickItem* item){ if(item){ if(QGraphicsScene *scene = item->findChild<QGraphicsScene *>()){ for(QGraphicsItem *it : scene->items()){ if(QtCharts::QChart *chart = dynamic_cast<QtCharts::QChart *>(it)){ // Customize plot area background QImage background(item->width(), item->height(), QImage::Format_ARGB32); if (background.isNull()) continue; background.fill(QColor(Qt::white)); QPainter painter(&background); int margin = (item->height()-chart->plotArea().height()) /2; int hRed = (10- _pData->threshHigh)/10 * chart->plotArea().height(); int hGreen = _pData->threshLow/10 * chart->plotArea().height(); int hYellow = chart->plotArea().height() - hRed - hGreen; // Red zone painter.fillRect(0, 0, item->width(), margin + hRed, QBrush(QColor(255,50,50,128))); // Yellow zone painter.fillRect(0,margin+hRed, item->width(), hYellow, QBrush(QColor(235,220,50,128))); // Green zone painter.fillRect(0,margin+hRed+hYellow, item->width(), margin+hGreen, QBrush(QColor(100,150,100,128))); chart->setPlotAreaBackgroundBrush(background); chart->setPlotAreaBackgroundVisible(true); } } } } } By passing the ChartView as parameter : sThresh.first.onValueChanged: controller.updateBackground(chart) sThresh.second.onValueChanged: controller.updateBackground(chart)
  • 0 Votes
    1 Posts
    444 Views
    No one has replied
  • QT quick TreeView and TreeViewStyle

    Unsolved
    1
    0 Votes
    1 Posts
    212 Views
    No one has replied
  • Compare Items of listview and aray

    Unsolved
    14
    0 Votes
    14 Posts
    2k Views
    S
    @Psyduck0205 I too facing similar issue. I need to access list of qvariant from squish? Can you help me ?
  • Random Text

    Unsolved
    2
    0 Votes
    2 Posts
    347 Views
    SGaistS
    Hi, You can take a look at the fortune example for a way of generating random text.
  • Add client certificate to QQuickWebEngineProfile clientCertificateStore in QML

    Unsolved
    14
    0 Votes
    14 Posts
    1k Views
    N
    ugh. I guess this feature has just been broken for over a year with misleading documentation. no big deal. thanks for your help everybody. https://bugreports.qt.io/browse/QTBUG-86132
  • Use a shader to hide part of an image

    Solved shadereffect
    2
    0 Votes
    2 Posts
    463 Views
    P
    Ok, I figured it out ! The shader does not change the Image, it just draws something else on the shader item's position. The solution was pretty darn simple: make the Image invisible and let the shadereffect item handle all the rendering part.
  • Qt shut down when I click Design mode

    Unsolved
    1
    0 Votes
    1 Posts
    156 Views
    No one has replied
  • QList<customObj *> in QML not working on raspberry

    Unsolved
    4
    0 Votes
    4 Posts
    335 Views
    P
    Yes QList<QObject*> is working. But why QList<SENSOR*> work on my Desktop and not work on raspberry???
  • WebEngineView cannot run WebGL

    Solved
    5
    0 Votes
    5 Posts
    2k Views
    raven-worxR
    @DiegOne hmm, still rather strange
  • Animation of PathView delegate

    Unsolved
    3
    0 Votes
    3 Posts
    334 Views
    M
    @fcarney I removed the Component but the error is the same: QML PropertyAnimation: Cannot animate non-existent property "y" But now the Item should have the y property, right?
  • Qt/QML 5.15.5 How to set logical DPI to 96

    Unsolved
    2
    0 Votes
    2 Posts
    753 Views
    V
    It would appear to be that there is an environment variable that will do this, not perfectly, but certainly vastly better than previously. Sadly, the very limited documentation on it implies it is only something for development purposes, presenting a clumsy way to look at your app using different DPI settings. The following, placed in main.cpp, worked. Yes, it could be set in the environment as well, having first tested it as a .profile entry. qputenv("QT_FONT_DPI", QByteArray("96")); 96 DPI is just a reference value on which Qt should be doing the appropriate scaling internally. It could be almost anything and still achieve the same results. Virtual pixels are obviously used for GUI components, why not consistently for the fonts. Though we are seeing what appears like a font substitution as well. Best guess is that Arial is being substituted for Liberation Sans, even though Liberation Sans is installed on all systems and is specifically loaded by the application. For example, the zero is much narrower on my system than on the target, as is the case of the zero in Arial as opposed to Liberation Sans.