Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. wordWrap(true) problem in listView
Qt 6.12 LTS is out! See what's new in the release blog

wordWrap(true) problem in listView

Scheduled Pinned Locked Moved Solved General and Desktop
listviewqt5gui
13 Posts 4 Posters 9.9k 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
    alex_malyu
    wrote on last edited by
    #2

    ui->listView->setWordWrap( true );

    wordWrap returns current state and does not take any parameters

    1 Reply Last reply
    1
    • ShawnyS Offline
      ShawnyS Offline
      Shawny
      wrote on last edited by
      #3

      @alex_malyu I tried using ui->listView->setWordWrap( true ); and it threw argument error. So i tried changing it to ui->listView->setWordWrap( ); but still it doesn't work. Can you elaborate a little how exactly i should use it?

      A 1 Reply Last reply
      0
      • ShawnyS Shawny

        @alex_malyu I tried using ui->listView->setWordWrap( true ); and it threw argument error. So i tried changing it to ui->listView->setWordWrap( ); but still it doesn't work. Can you elaborate a little how exactly i should use it?

        A Offline
        A Offline
        alex_malyu
        wrote on last edited by alex_malyu
        #4

        @Shawny said:

        and it threw argument error.

        Show an error.
        look at qlistview.h, which you pointed at

        setWordWrap Function is declared as
        void setWordWrap( bool on );
        Which means it require boolean value true or false.

        So the call has to be made as
        ui->listView->setWordWrap( true );

        If you get an error it should be related to different problem.

        1 Reply Last reply
        0
        • ShawnyS Offline
          ShawnyS Offline
          Shawny
          wrote on last edited by Shawny
          #5

          listView is QListView and the way i used it is shown below in a function. If i use ui->listView->wordWrap(); in place of ui->listView->wordWrap(true); then code doesn't throws error but word wrapping functionality wont work either.

          // when send button is pressed
          void MainWindow::on_pushButton_clicked()
          {
          
              QStandardItem* rowlist = new QStandardItem(QString(ui->textEdit->toPlainText()));
              model->appendRow(rowlist);
              ui->listView->setModel(model);
              ui->listView->wordWrap(true);
          }
          

          Error log looks like:

          /home/neo/Qt/chatty/mainwindow.cpp:-1: In member function 'void MainWindow::on_pushButton_clicked()':
          /home/neo/Qt/chatty/mainwindow.cpp:29: error: no matching function for call to 'QListView::wordWrap(bool)'
               ui->listView->wordWrap(true);
                                          ^
          /home/neo/Qt/chatty/mainwindow.cpp:29: candidate is:
          /home/neo/Qt/5.6/gcc_64/include/QtWidgets/QListView:1: In file included from ../5.6/gcc_64/include/QtWidgets/QListView:1:0,
          /home/neo/Qt/build-chatty-Desktop_Qt_5_6_0_GCC_64bit-Debug/ui_mainwindow.h:17: from ./ui_mainwindow.h:17,
          /home/neo/Qt/chatty/mainwindow.cpp:2: from ../chatty/mainwindow.cpp:2:
          /home/neo/Qt/5.6/gcc_64/include/QtWidgets/qlistview.h:117: bool QListView::wordWrap() const
               bool wordWrap() const;
                    ^
          /home/neo/Qt/5.6/gcc_64/include/QtWidgets/qlistview.h:117: note:   candidate expects 0 arguments, 1 provided
          
          1 Reply Last reply
          0
          • A Offline
            A Offline
            alex_malyu
            wrote on last edited by alex_malyu
            #6

            @Shawny said:

            /home/neo/Qt/chatty/mainwindow.cpp:29: error: no matching function for call to 'QListView::wordWrap(bool)'
            ui->listView->wordWrap(true);

            You still are calling wrong function.
            It has to be
            ui->listView->setWordWrap(true);

            Just replace

            ui->listView->wordWrap(true);

            with

            ui->listView->setWordWrap(true);

            1 Reply Last reply
            1
            • ShawnyS Offline
              ShawnyS Offline
              Shawny
              wrote on last edited by Shawny
              #7

              Sorry I didn't properly took notice of it. But I did changed it now and now code runs successfully but word wrapping is still not happening. Dunno whats wrong.

              Do I need to put some extra declarations to make this work?

              ? 1 Reply Last reply
              0
              • ShawnyS Shawny

                Sorry I didn't properly took notice of it. But I did changed it now and now code runs successfully but word wrapping is still not happening. Dunno whats wrong.

                Do I need to put some extra declarations to make this work?

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #8

                Hi @Shawny!
                The documentation says

                Please note that even if wrapping is enabled, the cell will not be expanded to make room for the text. It will print ellipsis for text that cannot be shown, according to the view's textElideMode.

                1 Reply Last reply
                0
                • ShawnyS Offline
                  ShawnyS Offline
                  Shawny
                  wrote on last edited by
                  #9

                  ohh okay so any idea how I can perform text wrap in listView apart from this method?

                  Currently I came across many opinions on internet one of which suggests to use item delegate to perform this action.

                  But this seems to be very complex method just to get text wrap done. I think I have no other alternative. Let me know if you have any other thing in mind or maybe some useful references. Thank you.

                  mrjjM 1 Reply Last reply
                  0
                  • ShawnyS Shawny

                    ohh okay so any idea how I can perform text wrap in listView apart from this method?

                    Currently I came across many opinions on internet one of which suggests to use item delegate to perform this action.

                    But this seems to be very complex method just to get text wrap done. I think I have no other alternative. Let me know if you have any other thing in mind or maybe some useful references. Thank you.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @Shawny
                    hi
                    The reason it dont word wrap is that the default row height is like 1 line so it
                    don't try to wordwrap as not enough space in the height.

                    Delegates can be complex but in this case you do it with one function, which
                    might be enough for what you want.

                    class MyDelegate : public QStyledItemDelegate {
                     public:
                      MyDelegate(QObject* parent)
                        : QStyledItemDelegate(parent) {}
                    
                      QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {    
                        QFontMetrics fm(option.font);
                        const QAbstractItemModel* model = index.model();
                        QString Text = model->data(index, Qt::DisplayRole).toString();
                        QRect neededsize = fm.boundingRect( option.rect, Qt::TextWordWrap,Text );
                        return QSize(option.rect.width(), neededsize.height());
                      }
                    };
                    
                    and you also need
                    ui->listView->setWordWrap(true);
                    ui->listView->setItemDelegate(new MyDelegate(this));
                    

                    The concept is that it check how high the row should be for the text and expand cell.
                    it will still show ... for last word sometimes though.

                    1 Reply Last reply
                    0
                    • ShawnyS Offline
                      ShawnyS Offline
                      Shawny
                      wrote on last edited by
                      #11

                      Works great! Thank you for this. But this syntax is super confusing. I will have to read some documentation on delegates to know how it works. However, text wrap is working in listView \m/

                      mrjjM 1 Reply Last reply
                      0
                      • ShawnyS Shawny

                        Works great! Thank you for this. But this syntax is super confusing. I will have to read some documentation on delegates to know how it works. However, text wrap is working in listView \m/

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @Shawny
                        good to hear :)

                        Its a good idea to read about delegates as they are used for most operations outside
                        of the normal settings.

                        1 Reply Last reply
                        0
                        • ShawnyS Offline
                          ShawnyS Offline
                          Shawny
                          wrote on last edited by
                          #13

                          Understood. Thank you.

                          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
                          • Unsolved