PDA

View Full Version : How to pass clicked event from button.qml to main source cpp to pop up a new window ?



duc_bkav
10th November 2011, 08:59
Hi everybody :) ,
I'm using QML to design a GUI for my application.
My mainwindow link to mainwindow.qml file. On this mainwindow, i've design button but i don't know how to pass clicked event from this button to main source code cpp to pop up another windows ( I intend to swtich the link of mainwindow.qml to another .qml file )

Can somebody help me ? :(

gouyoku
10th November 2011, 14:40
I'm not good at explaining so I'll just post some code. Marked the lines you should pay attention to with commends. Hope it helps ;)

mainwindow.h

#include <QMainWindow>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDebug>

class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
view = new QDeclarativeView(this);
view->rootContext()->setContextProperty("mainwindow",this); //this
view->setSource(QUrl("mainwindow.qml"));
this->setCentralWidget(view);
}
private:
QDeclarativeView *view;
public slots:
void click() { //this
qDebug() << "click";
}
};
mainwindow.qml

import QtQuick 1.0

Rectangle {
width: 100
height: 62
color: "red"

MouseArea {
anchors.fill: parent
onClicked: mainwindow.click() //this
}
}

duc_bkav
11th November 2011, 05:35
Thanks gouyoku so much, your post is very helpful for me ... sincerely ^^:)

mforce2
9th September 2014, 23:55
This indeed did the trick for me too but don't forget the Q_OBJECT keyword as I did at first.