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 4.1k 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.
  • 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