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. Text message not displaying in qml

Text message not displaying in qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 4 Posters 1.2k Views 2 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.
  • A Offline
    A Offline
    arlyn123
    wrote on last edited by
    #1

    I have a sudoku grid in which I check if there is a number in the same row or column using this javascrispt function

    function isValidMove(row, column, value) {
    
                            for (var i = 0; i < gridSize; ++i) {
                                if (get(row, i) === value || get(i, column) === value) {
                                    return false;
                                }
                            }
                            return true;
                        }
    

    When this function checks if the grid has a number on the same row or column it should display a text message near the board to inform the user about that. But the text is not displaying. What should be the problem? Why is the text not displaying?
    Here is the complete code of my TableView:

    TableView {
                    anchors.fill: parent
                    clip: true
    
                    model: SudokuGrid {
                        id: grid
    
                        property int gridSize: 9
                        function isValidMove(row, column, value) {
    
                            for (var i = 0; i < gridSize; ++i) {
                                if (get(row, i) === value || get(i, column) === value) {
                                    return false;
                                }
                            }
                            return true;
                        }
                    }
    
                    delegate: Rectangle {
                        required property var model
                        implicitWidth: 50
                        implicitHeight: 50
    
                        TextField {
                            anchors.fill: parent
                            text: model.display !== undefined ? model.display.toString() : ""
                            readOnly: model.display !== undefined && model.display !== 0
                            horizontalAlignment: TextInput.AlignHCenter
                            verticalAlignment: TextInput.AlignVCenter
                            background: Rectangle {
                                color: model.row % 2 ? "lightpink" : "lightblue"
                                border {
                                    width: 1
                                    color: "white"
                                }
                            }
                            color: "black"
                            validator: IntValidator {
                                bottom: 1
                                top: 9
                            }
    
                            onEditingFinished: {
                                if (text !== "" && model.display === 0) {
                                    if (validator.validate(text, 0).valid) {
    
                                        if (!grid.isValidMove(model.row, text) && !grid.isValidMove(model.column, text)) {
                                            model.text = text;
                                        } else {
                                            errorMessage.text = "Invalid input. Number already exists in the same row or column.";
                                        }
                                    } else {
                                        errorMessage.text = "Invalid input. Please enter a number from 1 to 9.";
                                    }
                                }
                            }
    
                            TableView.onCommit: {
                                if (text !== "" && model.display === 0) {
                                    if (validator.validate(text, 0).valid) {
    
                                        if (!grid.isValidMove(model.row, text) && !grid.isValidMove(model.column, text)) {
                                            model.display = text;
                                            errorMessage.text = "";  // Clear the error message
                                        } else {
                                            errorMessage.text = "Invalid input. Number already exists in the same row or column.";
                                        }
                                    } else {
                                        errorMessage.text = "Invalid input. Please enter a number from 1 to 9.";
                                    }
                                }
                            }
                        }
    
                        Rectangle {
                            width: 1
                            height: parent.height
                            color: model.column % 3 == 0 ? "black" : "transparent"
                        }
    
                        Rectangle {
                            width: parent.width
                            height: 1
                            color: model.row % 3 == 0 ? "black" : "transparent"
                        }
                    }
    
                    Text {
                        id: errorMessage
                        anchors {
                            left: parent.left
                            right: parent.right
                            top: grid.bottom
                            bottom: parent.bottom
                        }
                        font.pixelSize: 16
                        color: "red"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                }
    
    sierdzioS 1 Reply Last reply
    0
    • A arlyn123

      I have a sudoku grid in which I check if there is a number in the same row or column using this javascrispt function

      function isValidMove(row, column, value) {
      
                              for (var i = 0; i < gridSize; ++i) {
                                  if (get(row, i) === value || get(i, column) === value) {
                                      return false;
                                  }
                              }
                              return true;
                          }
      

      When this function checks if the grid has a number on the same row or column it should display a text message near the board to inform the user about that. But the text is not displaying. What should be the problem? Why is the text not displaying?
      Here is the complete code of my TableView:

      TableView {
                      anchors.fill: parent
                      clip: true
      
                      model: SudokuGrid {
                          id: grid
      
                          property int gridSize: 9
                          function isValidMove(row, column, value) {
      
                              for (var i = 0; i < gridSize; ++i) {
                                  if (get(row, i) === value || get(i, column) === value) {
                                      return false;
                                  }
                              }
                              return true;
                          }
                      }
      
                      delegate: Rectangle {
                          required property var model
                          implicitWidth: 50
                          implicitHeight: 50
      
                          TextField {
                              anchors.fill: parent
                              text: model.display !== undefined ? model.display.toString() : ""
                              readOnly: model.display !== undefined && model.display !== 0
                              horizontalAlignment: TextInput.AlignHCenter
                              verticalAlignment: TextInput.AlignVCenter
                              background: Rectangle {
                                  color: model.row % 2 ? "lightpink" : "lightblue"
                                  border {
                                      width: 1
                                      color: "white"
                                  }
                              }
                              color: "black"
                              validator: IntValidator {
                                  bottom: 1
                                  top: 9
                              }
      
                              onEditingFinished: {
                                  if (text !== "" && model.display === 0) {
                                      if (validator.validate(text, 0).valid) {
      
                                          if (!grid.isValidMove(model.row, text) && !grid.isValidMove(model.column, text)) {
                                              model.text = text;
                                          } else {
                                              errorMessage.text = "Invalid input. Number already exists in the same row or column.";
                                          }
                                      } else {
                                          errorMessage.text = "Invalid input. Please enter a number from 1 to 9.";
                                      }
                                  }
                              }
      
                              TableView.onCommit: {
                                  if (text !== "" && model.display === 0) {
                                      if (validator.validate(text, 0).valid) {
      
                                          if (!grid.isValidMove(model.row, text) && !grid.isValidMove(model.column, text)) {
                                              model.display = text;
                                              errorMessage.text = "";  // Clear the error message
                                          } else {
                                              errorMessage.text = "Invalid input. Number already exists in the same row or column.";
                                          }
                                      } else {
                                          errorMessage.text = "Invalid input. Please enter a number from 1 to 9.";
                                      }
                                  }
                              }
                          }
      
                          Rectangle {
                              width: 1
                              height: parent.height
                              color: model.column % 3 == 0 ? "black" : "transparent"
                          }
      
                          Rectangle {
                              width: parent.width
                              height: 1
                              color: model.row % 3 == 0 ? "black" : "transparent"
                          }
                      }
      
                      Text {
                          id: errorMessage
                          anchors {
                              left: parent.left
                              right: parent.right
                              top: grid.bottom
                              bottom: parent.bottom
                          }
                          font.pixelSize: 16
                          color: "red"
                          horizontalAlignment: Text.AlignHCenter
                          verticalAlignment: Text.AlignVCenter
                      }
                  }
      
      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @arlyn123 said in Text message not displaying in qml:

      What should be the problem? Why is the text not displaying?

      The problem is in anchors: top: grid.bottom - you are anchoring your text to a non-visual component (model), so QML engine does not know where to draw the text.

      (Z(:^

      A 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @arlyn123 said in Text message not displaying in qml:

        What should be the problem? Why is the text not displaying?

        The problem is in anchors: top: grid.bottom - you are anchoring your text to a non-visual component (model), so QML engine does not know where to draw the text.

        A Offline
        A Offline
        arlyn123
        wrote on last edited by
        #3

        @sierdzio I changed top: grid.bottom with top: parent.bottom and I deleted the bottom anchor. But the text is still not displaying when I enter a number in the same row or column in the grid. What could be the problem?

        B 1 Reply Last reply
        0
        • A arlyn123

          @sierdzio I changed top: grid.bottom with top: parent.bottom and I deleted the bottom anchor. But the text is still not displaying when I enter a number in the same row or column in the grid. What could be the problem?

          B Offline
          B Offline
          Bob64
          wrote on last edited by Bob64
          #4

          @arlyn123 if errorMessage's top is anchored to its parent's bottom, it is going to be squashed right at the bottom of its parent with no height.

          How do you actually want this to be laid out?

          The "parent" here is the TableView. Do you want this message to be inside the bounds of the TableView or do you intend it to be beneath it? If the latter, you need to introduce another higher level item in which they are both laid out.

          A 1 Reply Last reply
          1
          • B Bob64

            @arlyn123 if errorMessage's top is anchored to its parent's bottom, it is going to be squashed right at the bottom of its parent with no height.

            How do you actually want this to be laid out?

            The "parent" here is the TableView. Do you want this message to be inside the bounds of the TableView or do you intend it to be beneath it? If the latter, you need to introduce another higher level item in which they are both laid out.

            A Offline
            A Offline
            arlyn123
            wrote on last edited by
            #5

            @Bob64 I want the text message to be displayed at the bottom of the table view

            B 1 Reply Last reply
            0
            • A arlyn123

              @Bob64 I want the text message to be displayed at the bottom of the table view

              B Offline
              B Offline
              Bob64
              wrote on last edited by
              #6

              @arlyn123 that is ambiguous. Which of the two options I mentioned do you want?

              A 1 Reply Last reply
              0
              • B Bob64

                @arlyn123 that is ambiguous. Which of the two options I mentioned do you want?

                A Offline
                A Offline
                arlyn123
                wrote on last edited by
                #7

                @Bob64 beneath the TableView

                B 1 Reply Last reply
                0
                • A arlyn123

                  @Bob64 beneath the TableView

                  B Offline
                  B Offline
                  Bob64
                  wrote on last edited by Bob64
                  #8

                  @arlyn123 OK, you just need to move your error message up a level so that it is a sibling of the TableView. I don't know how your code is organised outside of the TableView so the following isn't necessarily the most appropriate way to do it but it should give you some idea:

                  // This item is just a container for the combination of the TableView and the error message
                  Item {
                      anchors.fill: parent
                  
                      TableView {
                          id: table
                          anchors.top: parent.top
                          anchors.left: parent.left
                          anchors.right: parent.right
                          anchors.bottom: errorMessage.top
                  
                          ...
                      }
                  
                      Text {
                          id: errorMessage
                          anchors.bottom: parent.bottom
                          anchors.left: parent.left
                          anchors.right: parent.right
                          height: ??? // Whatever height you want this
                      }
                  }
                  

                  The point here is that you have got a top level item that fills the space available to it, you have the text anchored to the bottom of this with a height defined by you, and then the table is anchored at the top to the container parent, and at the bottom to the top of the text. Therefore the table is filling the remainder of the vertical space in the container item.

                  GrecKoG 1 Reply Last reply
                  0
                  • B Bob64

                    @arlyn123 OK, you just need to move your error message up a level so that it is a sibling of the TableView. I don't know how your code is organised outside of the TableView so the following isn't necessarily the most appropriate way to do it but it should give you some idea:

                    // This item is just a container for the combination of the TableView and the error message
                    Item {
                        anchors.fill: parent
                    
                        TableView {
                            id: table
                            anchors.top: parent.top
                            anchors.left: parent.left
                            anchors.right: parent.right
                            anchors.bottom: errorMessage.top
                    
                            ...
                        }
                    
                        Text {
                            id: errorMessage
                            anchors.bottom: parent.bottom
                            anchors.left: parent.left
                            anchors.right: parent.right
                            height: ??? // Whatever height you want this
                        }
                    }
                    

                    The point here is that you have got a top level item that fills the space available to it, you have the text anchored to the bottom of this with a height defined by you, and then the table is anchored at the top to the container parent, and at the bottom to the top of the text. Therefore the table is filling the remainder of the vertical space in the container item.

                    GrecKoG Offline
                    GrecKoG Offline
                    GrecKo
                    Qt Champions 2018
                    wrote on last edited by GrecKo
                    #9

                    ColumnLayout would be more sensible here.

                    ColumnLayout {
                        anchors.fill: parent
                        TableView {
                            Layout.fillWidth: true
                            Layout.fillHeight: true
                        }
                        Label {
                            Layout.fillWidth: true
                            // set Layout.preferredWidth if you always want to allocated some space for it. without it it won't take any space when there is no text.
                        }
                    }
                    
                    B 1 Reply Last reply
                    1
                    • GrecKoG GrecKo

                      ColumnLayout would be more sensible here.

                      ColumnLayout {
                          anchors.fill: parent
                          TableView {
                              Layout.fillWidth: true
                              Layout.fillHeight: true
                          }
                          Label {
                              Layout.fillWidth: true
                              // set Layout.preferredWidth if you always want to allocated some space for it. without it it won't take any space when there is no text.
                          }
                      }
                      
                      B Offline
                      B Offline
                      Bob64
                      wrote on last edited by
                      #10

                      @GrecKo agreed that that is a cleaner way to do it, but it doesn't help the OP understand where they went wrong with anchors.

                      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