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. Organizing the visible Elements, e.g. an Error Message
Qt 6.11 is out! See what's new in the release blog

Organizing the visible Elements, e.g. an Error Message

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qml
2 Posts 2 Posters 1.2k 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I would like to show an error message in a program, for this I created a file ErrorScreen.qml.

        import QtQuick 2.0
        import QtQuick.Controls 2.2
    
        Item {
            width: Math.floor(parent.width*0.2)
            height: Math.floor(parent.width*0.2)
            anchors.horizontalCenter: parent.horizontalCenter
            property string headlineText
            property string infoText
            Rectangle {
                id: rectangle
                color: "#efefef"
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                anchors.top: parent.top
                Text {
                    id: headline
                    x: 111
                    y: 90
                    text: headlineText
                    font.bold: true
                    font.pixelSize: 24
                }
                Text {
                    id: info
                    x: 111
                    y: 130
                    text: infoText
                    font.bold: false
                    font.pixelSize: 18
                }
            }
        }
    

    Now I am not sure what the best practice is to display this error message. Or even more generally, I don't know what the best practice is to swith between different contents which I placed into seperate qml files. Here my thoughts: I could dynamically create it in QML like this

                        var component;
                        var sprite;
                        component = Qt.createComponent("ErrorScreen.qml");
                        sprite = component.createObject(root, {"headlineText": "Warning", "infoText": "InfoText"});
    

    But then I would have to deal with deleting it upon acknowledgement. So maybe it is better to just have something like this in my main.qml

        ApplicationWindow {
    
            visible: true
            id: root
            objectName: "MainWindow"
            width: 800
            height: 600
    
            menuBar: MenuBar {
            }
            LeftBar{
            }
            MainScreen{
            }
            ErrorScreen {
                visible: false
            }
        }
    

    and just set the ErrorScreen to visible once I need it. Here, the LeftBar would be an area at the left side of my application where I place some buttons, and in MainScreen, depending on the state of the application I would display the content of different qml files, like a WelcomeScreen, a WorkScreen... However, if I do it like this, e.g. a button on the WorkScreen should make the ErrorScreen visible, so I wonder what is the best way to access the different Screens which are not in the same scope (e.g. accessing ErrorButton from WorkScreen which is a child of MainScreen or the other way round).

    1 Reply Last reply
    0
    • ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      hi,

      @maxwell31 said in Organizing the visible Elements, e.g. an Error Message:

      e.g. accessing ErrorButton from WorkScreen which is a child of MainScreen or the other way round).

      you can alias your WorkScreen in MainScreen to be alble to access it from outside :

      // MainScreen.qml

      Item{
        property alias myWorkScreen : ws
       ...
          WorkScreen{
             id: ws
          }
      }
      

      // WorkScreen.qml

      Item{
      ...
       signal error()
      Button{onClicked:error()}
      }
      

      Now in you main, you can create a function and connect it to your workScreen signal

      ApplicationWindow {
      
             visible: true
             id: root
             objectName: "MainWindow"
             width: 800
             height: 600
      
                     function workScreenErrorHandler(){
                         console.log("ws err")
                    }
      
             Component.onCompleted:{ //connecting the signal
                 mainScreen.myWorkScreen.error.connect(workScreenErrorHandler)
              }
      
             menuBar: MenuBar {
             }
             LeftBar{
             }
             MainScreen{
               id:mainScreen
      
             }
             ErrorScreen {
                 visible: false
             }
         }
      

      Hope this can help
      LA

      1 Reply Last reply
      1

      • Login

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