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. Problem with TableView and alternatingRowColors

Problem with TableView and alternatingRowColors

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 2 Posters 1.5k 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.
  • F Offline
    F Offline
    fermatqt
    wrote on last edited by
    #1

    hi!

    i have this table view with this model:

        ListModel {
            id: libraryModel
            ListElement {
                npu: "A Masterpiece"
                conto: "Gabriel"
            }
            ListElement {
                npu: "Brilliance"
                conto: "Jens"
            }
            ListElement {
                npu: "Outstanding"
                conto: "Frederik"
            }
        }
    
        Rectangle {
            id: rectangle1
            x: 0
            y: 70
            width: 900
            height: 830
            color: "#ffffff"
    
            TableView {
                id: lw
                x: 0
                y: 0
                width: 900
                height: 816
                alternatingRowColors: true
                TableViewColumn {
                    role: "npu"
                    title: "NPU"
                    width: 400
                }
                TableViewColumn {
                    role: "conto"
                    title: "CONTO"
                    width: 400
                }
                model: libraryModel
            }
    

    the problem is that I do not see the rows with alternating colors.
    They are always white.
    can you tell me where I'm wrong?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,
      using

      Window {
          id: rectangle1
          width: 900
          height: 830
          color: "#ffffff"
          visible: true
      
          ListModel {
              id: libraryModel
              ListElement {
                  npu: "A Masterpiece"
                  conto: "Gabriel"
              }
              ListElement {
                  npu: "Brilliance"
                  conto: "Jens"
              }
              ListElement {
                  npu: "Outstanding"
                  conto: "Frederik"
              }
          }
      
          TableView {
              id: lw
              anchors.fill: parent
              alternatingRowColors: true
              TableViewColumn {
                  role: "npu"
                  title: "NPU"
                  width: rectangle1.width / 2
              }
              TableViewColumn {
                  role: "conto"
                  title: "CONTO"
                  width: rectangle1.width / 2
              }
              model: libraryModel
          }
      }
      

      I get the right behavior. What OS/Qt version are you using ?

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

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fermatqt
        wrote on last edited by
        #3

        hi!

        i'm using linux with qt 5.6.
        i have this structure:
        main.qml

        import QtQuick 2.4
        import QtQuick.Controls 1.3
        import QtQuick.Dialogs 1.2
        
        ApplicationWindow {
            visible: true
            width: 900
            height: 900
            minimumWidth: 900
            minimumHeight: 900
            maximumWidth: 900
            maximumHeight: 900
            title: qsTr("Cimoda Admin")
        
            menuBar: MenuBar {
                Menu {
                    title: qsTr("File")
                    MenuItem {
                        text: qsTr("&Open")
                        onTriggered: console.log("Open action triggered");
                    }
                    MenuItem {
                        text: qsTr("Exit")
                        shortcut: "Ctrl+Q"
                        onTriggered: Qt.quit();
                    }
                }
            }
        
            MainForm {
                anchors.fill: parent
            }
        }
        

        MainForm.ui.qml

        import QtQuick 2.4
        import QtQuick.Controls 1.3
        import QtQuick.Layouts 1.1
        
        Item {
            width: 900
            height: 900
            id: root
        
            TabView {
                id: tv
                width: parent.width
                height: parent.height
        
                Tab {
                    id: tabClienti
                    title: "Clienti"
                    source: "Clienti.qml"
                }
            }
        
        }
        

        Clienti.qml

        import QtQuick 2.1
        import QtQuick.Controls 1.0
        import QtQuick.Dialogs 1.2
        import "script.js" as Script
        
        Item {
        
            id: root
        
            Rectangle {
                x: 0
                y: 0
                width: parent.width
                height: 70
                TextField {
                    id: txtCerca
                    x: 8
                    y: 22
                    width: 164
                    height: 26
                    anchors.verticalCenter: parent.verticalCenter
                    placeholderText: qsTr("CONTO")
                    font.capitalization: Font.AllUppercase
                    text: "CLFD"
                }
        
                Button {
                    x: 800
                    y: 21
                    text: qsTr("Cerca")
                    anchors.verticalCenter: parent.verticalCenter
                    onClicked: {
                        Script.cercaConto(txtCerca.text)
                    }
                }
            }
        
            Rectangle {
                x: 0
                y: 70
                width: parent.width
                height: 820
                TableView {
                    id: lw
                    x: 0
                    y: 0
                    width: parent.width
                    height: parent.height
                    alternatingRowColors: true
                    TableViewColumn {
                        role: "npu"
                        title: "NPU"
                        width: root.width / 2
                    }
                    TableViewColumn {
                        role: "conto"
                        title: "CONTO"
                        width: root.width / 2
                    }
                    onDoubleClicked: {
                        var comp = Qt.createComponent("Cliente.qml");
                        var wind = comp.createObject(root);
                        wind.show(clientiModel.get(row).npu);
                    }
                    model: clientiModel
                }
        
            }
        
            ListModel { id: clientiModel }
        
            MessageDialog {
                id: messageDialog
                function show(title, text) {
                    messageDialog.title = title;
                    messageDialog.text = text;
                    messageDialog.open();
                }
            }
        }
        

        the table is totally white, no borders or colored stripes.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fermatqt
          wrote on last edited by
          #4

          i also tried this in another window.
          whit the same result.

          import QtQuick 2.3
          import QtQuick.Controls 1.2
          
          ApplicationWindow {
              visible: true
              width: 800
              height: 800
              minimumWidth: 800
              minimumHeight: 800
              maximumWidth: 800
              maximumHeight: 800
              title: qsTr("Cliente")
          
              Rectangle {
          
                  id: rectangle1
                  width: parent.width
                  height: parent.height
                  color: "#ffffff"
                  visible: true
          
                  ListModel {
                      id: libraryModel
                      ListElement {
                          npu: "A Masterpiece"
                          conto: "Gabriel"
                      }
                      ListElement {
                          npu: "Brilliance"
                          conto: "Jens"
                      }
                      ListElement {
                          npu: "Outstanding"
                          conto: "Frederik"
                      }
                  }
          
                  TableView {
                      id: lw
                      anchors.fill: parent
                      alternatingRowColors: true
                      TableViewColumn {
                          role: "npu"
                          title: "NPU"
                          width: rectangle1.width / 2
                      }
                      TableViewColumn {
                          role: "conto"
                          title: "CONTO"
                          width: rectangle1.width / 2
                      }
                      model: libraryModel
                  }
              }
          }
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You can try by setting a TableViewStyle so you ensure that you have two different colors.

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

            F 1 Reply Last reply
            0
            • SGaistS SGaist

              You can try by setting a TableViewStyle so you ensure that you have two different colors.

              F Offline
              F Offline
              fermatqt
              wrote on last edited by fermatqt
              #6

              @SGaist

              ok this seems to work:

                      TableView {
                          id: lw
                          x: 0
                          y: 0
                          width: parent.width
                          height: parent.height
                          TableViewColumn {
                              role: "npu"
                              title: "NPU"
                              width: clientiRoot.width / 2
                              movable: false
                          }
                          TableViewColumn {
                              role: "conto"
                              title: "CONTO"
                              width: clientiRoot.width / 2
                              movable: false
                          }
                          onDoubleClicked: {
                              var choosed = clientiModel.get(row).npu;
                              var comp = Qt.createComponent("Cliente.qml");
                              var wind = comp.createObject(clientiRoot, {"npu": choosed});
                              wind.show();
                          }
                          model: clientiModel
                          style: TableViewStyle {
                                  backgroundColor: "#fff"
                                  alternateBackgroundColor: "#ccc"
                          }
                      }
              

              thanks!!!

              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