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. How to call message dialog from qt c++ ?
QtWS25 Last Chance

How to call message dialog from qt c++ ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmldir
9 Posts 4 Posters 1.7k Views
  • 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.
  • A Offline
    A Offline
    Ahti
    wrote on 19 Jul 2019, 14:52 last edited by
    #1

    What is the proper way to call a message dialog from c++ ? here is my code so far:

    main.qml:

    Window {
        visible: true
        minimumWidth: 350
        minimumHeight: 500 
    
        MessageDialog {
            id: err_msg
            title: "Error"
        }
    }
    

    example.cpp:

    void Example::some_func(){
            // call message dialog here with error message.
    }
    

    Thanks

    what is a signature ?? Lol

    O 1 Reply Last reply 19 Jul 2019, 16:27
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 19 Jul 2019, 15:43 last edited by
      #2

      Hi,

      Did you already read the C++ QML Integration guide ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 19 Jul 2019, 16:08
      1
      • S SGaist
        19 Jul 2019, 15:43

        Hi,

        Did you already read the C++ QML Integration guide ?

        A Offline
        A Offline
        Ahti
        wrote on 19 Jul 2019, 16:08 last edited by
        #3

        @SGaist yes in fact Example is already registered to QML but I don't understand how would I show message dialog once I encounter an error in c++.

        what is a signature ?? Lol

        1 Reply Last reply
        0
        • A Ahti
          19 Jul 2019, 14:52

          What is the proper way to call a message dialog from c++ ? here is my code so far:

          main.qml:

          Window {
              visible: true
              minimumWidth: 350
              minimumHeight: 500 
          
              MessageDialog {
                  id: err_msg
                  title: "Error"
              }
          }
          

          example.cpp:

          void Example::some_func(){
                  // call message dialog here with error message.
          }
          

          Thanks

          O Offline
          O Offline
          ODБOï
          wrote on 19 Jul 2019, 16:27 last edited by ODБOï
          #4

          @Ahti hi
          use signal /slot mechanism

          Window {
              visible: true
              minimumWidth: 350
              minimumHeight: 500 
          
            Example{
              onErrorSignal : err_msg.show()
            }
           
          
              MessageDialog {
                  id: err_msg
                  title: "Error"
              }
          }
          //example.cpp:
          
          void Example::some_func(){
                  // call message dialog here with error message., emit a signal
              emit errorSignal();
                 
          }
          
          A 1 Reply Last reply 19 Jul 2019, 18:37
          3
          • O ODБOï
            19 Jul 2019, 16:27

            @Ahti hi
            use signal /slot mechanism

            Window {
                visible: true
                minimumWidth: 350
                minimumHeight: 500 
            
              Example{
                onErrorSignal : err_msg.show()
              }
             
            
                MessageDialog {
                    id: err_msg
                    title: "Error"
                }
            }
            //example.cpp:
            
            void Example::some_func(){
                    // call message dialog here with error message., emit a signal
                emit errorSignal();
                   
            }
            
            A Offline
            A Offline
            Ahti
            wrote on 19 Jul 2019, 18:37 last edited by Ahti
            #5

            @LeLev This doesn't work:

             Example{
                onErrorSignal : err_msg.show()
              }
            

            mainly because I have set Example as property with rootContext() like this:

            main.cpp:

            Example e;    
            engine.rootContext()->setContextProperty("example", &e);
            

            what is a signature ?? Lol

            O 1 Reply Last reply 19 Jul 2019, 19:03
            0
            • A Ahti
              19 Jul 2019, 18:37

              @LeLev This doesn't work:

               Example{
                  onErrorSignal : err_msg.show()
                }
              

              mainly because I have set Example as property with rootContext() like this:

              main.cpp:

              Example e;    
              engine.rootContext()->setContextProperty("example", &e);
              
              O Offline
              O Offline
              ODБOï
              wrote on 19 Jul 2019, 19:03 last edited by ODБOï
              #6

              @Ahti you can une QML Connections type like this

              Connections {
                      target: example
                       onErrorSignal : err_msg.show()
                  }
              

              https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html

              1 Reply Last reply
              2
              • Pradeep P NP Offline
                Pradeep P NP Offline
                Pradeep P N
                wrote on 22 Jul 2019, 04:47 last edited by Pradeep P N
                #7

                Hi @Ahti

                You can also expose the function as Q_INVOKABLE or use it as public slots: with return type as bool.
                And use that function in QML .

                public:
                  Q_INVOKABLE bool some_func();
                
                // OR
                
                public slots:
                  bool some_func();
                
                    MessageDialog {
                        id: err_msg
                
                        title: "Error"
                        visible: example.some_func();
                    }
                

                Hope it was of help.
                All the best.

                Pradeep Nimbalkar.
                Upvote the answer(s) that helped you to solve the issue...
                Keep code clean.

                O 1 Reply Last reply 22 Jul 2019, 08:57
                1
                • Pradeep P NP Pradeep P N
                  22 Jul 2019, 04:47

                  Hi @Ahti

                  You can also expose the function as Q_INVOKABLE or use it as public slots: with return type as bool.
                  And use that function in QML .

                  public:
                    Q_INVOKABLE bool some_func();
                  
                  // OR
                  
                  public slots:
                    bool some_func();
                  
                      MessageDialog {
                          id: err_msg
                  
                          title: "Error"
                          visible: example.some_func();
                      }
                  

                  Hope it was of help.
                  All the best.

                  O Offline
                  O Offline
                  ODБOï
                  wrote on 22 Jul 2019, 08:57 last edited by ODБOï
                  #8

                  @Pradeep-P-N hi, you should tag the OP, not me

                  Pradeep P NP 1 Reply Last reply 22 Jul 2019, 12:58
                  0
                  • O ODБOï
                    22 Jul 2019, 08:57

                    @Pradeep-P-N hi, you should tag the OP, not me

                    Pradeep P NP Offline
                    Pradeep P NP Offline
                    Pradeep P N
                    wrote on 22 Jul 2019, 12:58 last edited by
                    #9

                    @LeLev the reply was from Mobile, so it got missed.

                    Pradeep Nimbalkar.
                    Upvote the answer(s) that helped you to solve the issue...
                    Keep code clean.

                    1 Reply Last reply
                    0

                    1/9

                    19 Jul 2019, 14:52

                    • Login

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