Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. onClicked and onReleased is not working while onPressed and onDoubleClicked is working fine in MouseArea of ListView
Forum Updated to NodeBB v4.3 + New Features

onClicked and onReleased is not working while onPressed and onDoubleClicked is working fine in MouseArea of ListView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 298 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Himanshu Rohilla
    wrote on last edited by
    #1

    Hello Everyone,

    I created a sample application. This application is loading a QMainWindow. I set a QGraphicsView as a central widget of QMainWindow. QGraphicsView'scene is adding a widget i.e. QQuickWidget. This QQuickWidget is loading a QML file.

    This QML file loading a Rectangle QML type as a root. In Rectangle, I am showing a ListView which is showing some data. Model and delegate are declared in QML file.

    Now problem is, I am unable to get onClicked event in MouseArea of ListView. onDoubleClicked event is working fine.

    I want to get an onClicked event. Please help me to get this event. I don't know what is missing here.

    Sample application source code is as follows :-

    QML Source Code :-

    import QtQuick 6.5
    import QtQuick.Controls 6.5
    
    Rectangle {
        id: root
    
        visible: true
        width: 400
        height: 300
    
        ListModel {
            id: model
    
            ListElement {
                name: "Bill Smith"
                number: "555 3264"
            }
            ListElement {
                name: "John Brown"
                number: "555 8426"
            }
            ListElement {
                name: "Sam Wise"
                number: "555 0473"
            }
        }
    
        ListView {
            id: patientList
    
            visible: true
    
            spacing: 15
    
            focus: true
            width: parent.width
    
            height: 100
    
            model: model
    
            delegate: Text {
                    text: model.name + ": " + model.number
            }
    
            MouseArea {
                id: listViewMouseArea
    
                hoverEnabled: true
                anchors.fill: parent
    
                onClicked: {
                    console.log("In onClicked MouseArea of ListView");
                }
                onDoubleClicked: {
                    console.log("In doubleClicked MouseArea of ListView");
                }
            }
        }
    }
    

    MainWindow.cpp

    #include "mainwindow.h"
    
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QQuickWidget>
    #include <QGraphicsLinearLayout>
    #include <QQmlApplicationEngine>
    #include <QGraphicsProxyWidget>
    
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow{parent}
    {
    
        QQmlApplicationEngine* s_engine = new QQmlApplicationEngine;
    
        QQuickWidget* quick_item = new QQuickWidget(s_engine,nullptr);
        quick_item->setAttribute(Qt::WA_AlwaysStackOnTop);
        quick_item->setAttribute(Qt::WA_TranslucentBackground);
        quick_item->setAttribute(Qt::WA_PaintUnclipped);
        quick_item->setClearColor(Qt::transparent);
        quick_item->setResizeMode(QQuickWidget::SizeRootObjectToView);
    
        quick_item->setSource(QUrl::fromLocalFile("C:\\Users\\rohillh\\Documents\\QtSamples\\QML\\Main.qml"));
    
        auto lObj = quick_item->rootObject();
        if (!lObj) {
            qWarning() << quick_item->errors();
            qCritical() << "cannot create QML component";
        }Q_ASSERT(lObj != nullptr);
    
        QGraphicsScene* scene = new QGraphicsScene;
        scene->addWidget(quick_item);
    
    
        QGraphicsView* view = new QGraphicsView(scene,this);
        view->setMouseTracking(true);
        view->resize(600, 400);
        view->setBackgroundBrush(Qt::yellow);
        view->show();
    
        setCentralWidget(view);
        resize(600,400);
    
        centralWidget()->show();
    
    }
    

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = nullptr);
    
    signals:
    };
    
    #endif // MAINWINDOW_H
    
    

    main.cpp

    #include <QApplication>
    #include <QtQuickWidgets/QQuickWidget>
    #include <QUrl>
    #include <QtWidgets/QWidget>
    
    #include "mainwindow.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        MainWindow win;
        win.setGeometry(100,100,800,600);
        win.show();
    
    
        return app.exec();
    }
    

    Thanks in advance :)

    HImanshu Rohilla

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      Which Qt Version are you using?

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved