Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Modify labels at QTreeView
Forum Updated to NodeBB v4.3 + New Features

Modify labels at QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 604 Views 1 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.
  • G Offline
    G Offline
    guille31794
    wrote on last edited by
    #1

    Hi all,

    I'm doing an application that can handle dynamic translations at runtime. So everything works fine except for the headers in my QTreeView. I'd like to have the opportunity to access them and modify it text accordingly with the language selected but I can't find how.

    QPointer<QFileSystemModel> model = new QFileSystemModel;
        QPointer<NoIconOrExtensionFileDelegate> delegate = new NoIconOrExtensionFileDelegate;
        QString home = QDir::homePath();
        QDir registryFolder{home};
    
        // If registry directory doesn't exist, is created
        if(!registryFolder.exists(home))
        {
            registryFolder.mkdir(home);
        }
        
        model->setRootPath(home);
        ui->registryTreeView->setModel(model);
        ui->registryTreeView->setItemDelegate(delegate);
        ui->registryTreeView->setRootIndex(model->index(home));
        // Hide size and type colums
        ui->registryTreeView->hideColumn(1);
        ui->registryTreeView->hideColumn(2);
        ui->registryTreeView->setColumnWidth(0, 450);
        ui->registryTreeView->setSelectionBehavior (QAbstractItemView::SelectRows);
       
        mJsonOperator.setPath(home);
    

    In that piece of code I tried to mess a bit with the header labels with:

        model->setHeaderData(0, Qt::Horizontal, "Test");
        model->setHeaderData(1, Qt::Horizontal, "Test2");
        model->setHeaderData(4, Qt::Horizontal, "Test3");
        model->setHeaderData(5, Qt::Horizontal, "Test4");
    

    But it doesn't seem to work for me modifying anything. Any help is welcome. Thanks in advance.

    Regards.

    JonBJ 1 Reply Last reply
    0
    • G guille31794

      Hi all,

      I'm doing an application that can handle dynamic translations at runtime. So everything works fine except for the headers in my QTreeView. I'd like to have the opportunity to access them and modify it text accordingly with the language selected but I can't find how.

      QPointer<QFileSystemModel> model = new QFileSystemModel;
          QPointer<NoIconOrExtensionFileDelegate> delegate = new NoIconOrExtensionFileDelegate;
          QString home = QDir::homePath();
          QDir registryFolder{home};
      
          // If registry directory doesn't exist, is created
          if(!registryFolder.exists(home))
          {
              registryFolder.mkdir(home);
          }
          
          model->setRootPath(home);
          ui->registryTreeView->setModel(model);
          ui->registryTreeView->setItemDelegate(delegate);
          ui->registryTreeView->setRootIndex(model->index(home));
          // Hide size and type colums
          ui->registryTreeView->hideColumn(1);
          ui->registryTreeView->hideColumn(2);
          ui->registryTreeView->setColumnWidth(0, 450);
          ui->registryTreeView->setSelectionBehavior (QAbstractItemView::SelectRows);
         
          mJsonOperator.setPath(home);
      

      In that piece of code I tried to mess a bit with the header labels with:

          model->setHeaderData(0, Qt::Horizontal, "Test");
          model->setHeaderData(1, Qt::Horizontal, "Test2");
          model->setHeaderData(4, Qt::Horizontal, "Test3");
          model->setHeaderData(5, Qt::Horizontal, "Test4");
      

      But it doesn't seem to work for me modifying anything. Any help is welcome. Thanks in advance.

      Regards.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @guille31794 If you want translation why aren't you using tr("Test") etc.?

      G 1 Reply Last reply
      0
      • JonBJ JonB

        @guille31794 If you want translation why aren't you using tr("Test") etc.?

        G Offline
        G Offline
        guille31794
        wrote on last edited by
        #3

        @JonB because that setHeaderData method always return false. Doesn't do anything, and the labels that are set by default aren't translatable, or at least they don't appear in my updated translation files.

        JonBJ 1 Reply Last reply
        0
        • G guille31794

          @JonB because that setHeaderData method always return false. Doesn't do anything, and the labels that are set by default aren't translatable, or at least they don't appear in my updated translation files.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @guille31794
          Then I don't know, but maybe QFileSystemModel ignores/does not implement any setHeaderData(). Try overriding QVariant QFileSystemModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const to return what you want instead and verify that works?

          I don't know much about translation, but did you enter the translations for these texts in whatever file you have to do that?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            guille31794
            wrote on last edited by
            #5

            I didn't have much time, but perhaps somebody can figure how to translate my current chunk of code to use a QTreeWidget. Maybe that way (inheriting from QTreeView) I can do what I want with my header labels.

            JonBJ 1 Reply Last reply
            0
            • G guille31794

              I didn't have much time, but perhaps somebody can figure how to translate my current chunk of code to use a QTreeWidget. Maybe that way (inheriting from QTreeView) I can do what I want with my header labels.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @guille31794 said in Modify labels at QTreeView:

              @JonB because that setHeaderData method always return false

              It isn't an issue of either QTreeView or QTreeWidget, it's because you are using QFileSystemModel.

              First get it working with your texts without translation. I suggested to you that QFileSystemModel::setHeader() won't do anything, and that you should instead subclass and do your work in what headerData() returns. I see QTreeView / QFileSystemModel set header labels says the same. Did you try this like I suggested? Or don't you have enough time? Once that is done I would hope that from there return tr("Test1") does the translation?

              SGaistS 1 Reply Last reply
              0
              • JonBJ JonB

                @guille31794 said in Modify labels at QTreeView:

                @JonB because that setHeaderData method always return false

                It isn't an issue of either QTreeView or QTreeWidget, it's because you are using QFileSystemModel.

                First get it working with your texts without translation. I suggested to you that QFileSystemModel::setHeader() won't do anything, and that you should instead subclass and do your work in what headerData() returns. I see QTreeView / QFileSystemModel set header labels says the same. Did you try this like I suggested? Or don't you have enough time? Once that is done I would hope that from there return tr("Test1") does the translation?

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                QFileSystemModel implements headerData and returns specific values for each columns hence your calls to setHeaderData have no effects.

                Either make a subclass of QFileSystemModel or make a custom QIdentityProxyModel.

                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

                • Login

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