Adding a QDialog to a QML Application?



  • So using Qt 5.2 I created a Qt Quick Application project, which comes with qtquick2applicationviewer .h and .cpp, a main.cpp, and QML files.

    I want to add a .cpp and .h file to control system notifications based on things that happen in the main QML application. I was following the example here http://doc.qt.digia.com/4.6/desktop-systray.html but that is a QWidget in itself, not a QML view. I thought I could just create a C++ class that extends QDialog that i could call to display system notifications, but it seems it just doesn't work that way?

    Here's my current code:

    @#ifndef NOTIFICATIONS_H
    #define NOTIFICATIONS_H

    #include <QtGui>
    #include <QSystemTrayIcon>
    #include <QObject>
    #include <QString>
    #include <QDialog>

    class notifications : public QDialog
    {
    Q_OBJECT

    public:
    explicit notifications();
    ~notifications();

    private:
    QAction *quitAction;
    QSystemTrayIcon *trayIcon;
    QMenu *trayIconMenu;
    void createActions();
    void createTrayIcon();
    void createTrayIconMenu();
    };

    #endif // NOTIFICATIONS_H@

    @#include "notifications.h"
    #include <QSystemTrayIcon>
    #include <QDialog>
    #include <QMenu>

    notifications::notifications()
    {
    createActions();
    createTrayIcon();
    createTrayIconMenu();
    }

    notifications::~notifications()
    {
    }

    void notifications::createActions()
    {
    quitAction = new QAction("&Quit", this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    }

    void notifications::createTrayIcon()
    {
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(quitAction);
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
    }@

    Now all Qt Creator says is "QWidget: Cannot create a QWidget without QApplication." Does that mean I have to implement my entire QML application as a QApplication in Qt instead?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.