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. qtreeview item text too long

qtreeview item text too long

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 3.4k 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by user4592357
    #1

    in my QTreeView i have an item whose text is long so currently my item takes up height of three normal rows.

    i've tried setting setWordWrap(false), setUniformRowHeights(true) and setTextElideMode(Qt::ElideRight) to the tree view but none worked.

    when i widen the column, the text appears in three rows. what i need is the text to appear in one row, and i wanna have the text elided from right. how can i do that?

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

      Hi,

      Can you give a minimal compilable example that shows that behaviour ?
      What version of Qt are you using ?
      On what platform ?

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

      U 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

        Can you give a minimal compilable example that shows that behaviour ?
        What version of Qt are you using ?
        On what platform ?

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #3

        @SGaist
        right now i'm on mobile so i'm afraid i can't provide an example. will do when i get to my computer.
        i'm using v5.11.1, on linux.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Can you give a minimal compilable example that shows that behaviour ?
          What version of Qt are you using ?
          On what platform ?

          U Offline
          U Offline
          user4592357
          wrote on last edited by
          #4

          @SGaist
          sorry but i cannot write an example that shows the behavior.
          but in my actual code i have a tree view two modes.
          when i have this structure:

          "small text"
          |__"long text"
          

          the row heights are okay. but when i have

          "long text"
          |__"short text"
          

          both rows have the height of three normal rows (i've set setUniformRowHeights(true), when i remove it, only first row is tall).

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Summoning @Christian-Ehrlicher as I think this one is a bug he sqashed

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @VRonin: According from the description it could be but we can't check... so @user4592357 either post some code to reproduce the problem or test with 5.12.3

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • U Offline
                U Offline
                user4592357
                wrote on last edited by
                #7

                i finally understood what the problem is. my item had newlines in it.
                but if i remove the newlines in my items then i will have two problems:

                1. for every item i'll have to do it
                2. i'm using these node texts in sql queries. so when i removed the newlines, the text didn't match the text in the database.
                  what can i do here?
                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8
                  class NoNewLinesDelegate : public QStyledItemDelegate {
                      Q_OBJECT
                      Q_DISABLE_COPY(NoNewLinesDelegate)
                  public:
                      QString displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE
                      {
                          QString baseText = QStyledItemDelegate::displayText(value, locale);
                          return baseText.replace(m_newLineRegExp,QStringLiteral(" "));
                      }
                      explicit NoNewLinesDelegate(QObject* parent = Q_NULLPTR)
                          : QStyledItemDelegate(parent)
                          , m_newLineRegExp(QStringLiteral("[\r\n]+"))
                      {}
                  private:
                      const QRegularExpression m_newLineRegExp;
                  };
                  

                  Now you just need to call treeView->setItemDelegate(new NoNewLinesDelegate(treeView));

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  U 1 Reply Last reply
                  3
                  • VRoninV VRonin
                    class NoNewLinesDelegate : public QStyledItemDelegate {
                        Q_OBJECT
                        Q_DISABLE_COPY(NoNewLinesDelegate)
                    public:
                        QString displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE
                        {
                            QString baseText = QStyledItemDelegate::displayText(value, locale);
                            return baseText.replace(m_newLineRegExp,QStringLiteral(" "));
                        }
                        explicit NoNewLinesDelegate(QObject* parent = Q_NULLPTR)
                            : QStyledItemDelegate(parent)
                            , m_newLineRegExp(QStringLiteral("[\r\n]+"))
                        {}
                    private:
                        const QRegularExpression m_newLineRegExp;
                    };
                    

                    Now you just need to call treeView->setItemDelegate(new NoNewLinesDelegate(treeView));

                    U Offline
                    U Offline
                    user4592357
                    wrote on last edited by
                    #9

                    @VRonin
                    thanks but actually my bigger concern was the problem #2

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      Yes, the point of my solution is that you don't have to change the data at all, you just change how the data is displayed.
                      Leave the newlines in the model and implement the solution above

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      U 1 Reply Last reply
                      2
                      • VRoninV VRonin

                        Yes, the point of my solution is that you don't have to change the data at all, you just change how the data is displayed.
                        Leave the newlines in the model and implement the solution above

                        U Offline
                        U Offline
                        user4592357
                        wrote on last edited by
                        #11

                        @VRonin
                        yes i just realized it's the displayText() method. thank you. and also can you please format the code so it's better readable? :)

                        1 Reply Last reply
                        0
                        • U Offline
                          U Offline
                          user4592357
                          wrote on last edited by
                          #12

                          @VRonin
                          i tried to set this delegate to my tree view but the items are still displayed as before. in displayText(), i printed both before and after versions of text, and they're correct. i think the reason it's still the same is that the text is too long (200 chars)?

                          JonBJ 1 Reply Last reply
                          0
                          • U user4592357

                            @VRonin
                            i tried to set this delegate to my tree view but the items are still displayed as before. in displayText(), i printed both before and after versions of text, and they're correct. i think the reason it's still the same is that the text is too long (200 chars)?

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

                            @user4592357
                            So if it is indeed the case that the text is too long for display, amend @VRonin's code which currently removes any newline to also limit the output to whatever length you consider acceptable.

                            U 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @user4592357
                              So if it is indeed the case that the text is too long for display, amend @VRonin's code which currently removes any newline to also limit the output to whatever length you consider acceptable.

                              U Offline
                              U Offline
                              user4592357
                              wrote on last edited by
                              #14

                              @JonB
                              i can't, because what if the column is resized to view the whole text?

                              JonBJ 1 Reply Last reply
                              0
                              • U user4592357

                                @JonB
                                i can't, because what if the column is resized to view the whole text?

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

                                @user4592357
                                I don't know, have you tried it? Does the resize for text not respect your shortened text returned from displayText()?

                                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