Results 1 to 4 of 4

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

  1. #1
    Join Date
    Nov 2011
    Posts
    17
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy How to pass clicked event from button.qml to main source cpp to pop up a new window ?

    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 ?

  2. #2
    Join Date
    Nov 2011
    Posts
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to pass clicked event from button.qml to main source cpp to pop up a new wind

    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
    Qt Code:
    1. #include <QMainWindow>
    2. #include <QDeclarativeView>
    3. #include <QDeclarativeContext>
    4. #include <QDebug>
    5.  
    6. class MainWindow : public QMainWindow {
    7. Q_OBJECT
    8. public:
    9. MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
    10. view = new QDeclarativeView(this);
    11. view->rootContext()->setContextProperty("mainwindow",this); //this
    12. view->setSource(QUrl("mainwindow.qml"));
    13. this->setCentralWidget(view);
    14. }
    15. private:
    16. QDeclarativeView *view;
    17. public slots:
    18. void click() { //this
    19. qDebug() << "click";
    20. }
    21. };
    To copy to clipboard, switch view to plain text mode 
    mainwindow.qml
    Qt Code:
    1. import QtQuick 1.0
    2.  
    3. Rectangle {
    4. width: 100
    5. height: 62
    6. color: "red"
    7.  
    8. MouseArea {
    9. anchors.fill: parent
    10. onClicked: mainwindow.click() //this
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to gouyoku for this useful post:

    duc_bkav (11th November 2011), mforce2 (9th September 2014)

  4. #3
    Join Date
    Nov 2011
    Posts
    17
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to pass clicked event from button.qml to main source cpp to pop up a new wind

    Thanks gouyoku so much, your post is very helpful for me ... sincerely ^^

  5. #4
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to pass clicked event from button.qml to main source cpp to pop up a new wind

    This indeed did the trick for me too but don't forget the Q_OBJECT keyword as I did at first.

Similar Threads

  1. Replies: 6
    Last Post: 9th November 2011, 04:31
  2. Replies: 10
    Last Post: 16th July 2011, 12:29
  3. Replies: 3
    Last Post: 23rd December 2010, 06:55
  4. Raise key pressed event when button is clicked?
    By newstead in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2009, 13:12
  5. Replies: 11
    Last Post: 11th August 2008, 09:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.