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. How to set column width using QStandardItem or QStandardItemModel?

How to set column width using QStandardItem or QStandardItemModel?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 10.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.
  • N Offline
    N Offline
    neetaj
    wrote on last edited by
    #1

    I am inserting qstring in QStandardItem using setData. Inserting this QStandardItem object in QStandardItemModel object at given row & column. Consider i have multiple rows & columns. I want to set width for each column then how shall i set it, so that string will display in single line instead of multiple rows.

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      How item is wrapped and elided is a function of a view, not model.

      For example QTableView (and thus QTableWidget) has a method that sets if the text should be wrapped or single line: setWordWrap(bool).
      To resize a column to fit the text you can either calculate the required size and resize that column via its header:
      tableWidget->horizontalHeader()->resizeSection(columnIndex, 42);
      or simply call resizeColumnsToContents() on the view to let it do all that heavy lifting for you.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        neetaj
        wrote on last edited by
        #3

        @Chris-Kawa said:

        resizeColumnsToContents

        Thanks Chris for your reply. But i am looking for QListView. How i can set column width for that or is there any way so that list will be automatically resized as per maximum length of string?

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          QListView doesn't have columns as it displays only one column of a model at a time so I'm not sure what you're asking...

          Do you mean the width of the widget? In that case that is governed by the layout it is in and you can provide hints to it like minimum width or size policy.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            neetaj
            wrote on last edited by Chris Kawa
            #5

            Just to understand more in details please refer the following code snippet.

            QStringList NameList;
            NameList << "aaaaaaaaaaaa" << "bbbbbb 11111 22222" << "nnnnnn *****";
            QStandardItemModel* m_Model = new QStandardItemModel(3, 3);
             
            for(row = 0; row < NameList.size(); row++)
                {
                    QStandardItem* iconItem = new QStandardItem();
                    QString iconPath = "../test.png";
                    iconItem->setData(QVariant(QPixmap(iconPath)), Qt::DecorationRole);
                    m_Model->setItem(row, 0, iconItem);
             
                    QStandardItem* nameItem = new QStandardItem();
                    nameItem->setData(QVariant(NameList.at(row)));
                    m_Model->setItem(row, 1, nameItem);
             
                    QStandardItem* dateItem = new QStandardItem();
                    dateItem->setData(QVariant(QDate::currentDate().toString()));
                    dateItem->setData(QVariant(Qt::AlignRight));
                    m_Model->setItem(row, 2, dateItem);
             
                } // END OF FOR LOOPn
             
                m_Model->setHeaderData(0, Qt::Horizontal, "Icon");
                m_Model->setHeaderData(1, Qt::Horizontal, "Name");
                m_Model->setHeaderData(2, Qt::Horizontal, "Date");
            
                m_Model->setHeaderData(0, Qt::Horizontal, QVariant(Qt::AlignLeft | Qt::AlignVCenter), Qt::TextAlignmentRole);
                m_Model->setHeaderData(2, Qt::Horizontal, Qt::AlignRight, Qt::TextAlignmentRole);
            
                NameList->setColumnWidth(1, 500);
              
                myListView->SetModel(m_Model); // we have our own framework, in that APIs are developed using Qt. here myListView is object of class in our framework which is similar to QListView
            

            Expected output :

            Icon Name Date
            Icon aaaaaaaaaaaa 12-08-2015
            Icon bbbbbb 11111 22222 12-08-2015
            Icon nnnnnn ***** 12-08-2015

            Actual output :

            Icon Name Date
            Icon aaaaaaaaa aa
            aa 12-08-2015
            Icon bbbbbb 11111
            22222 12-08-2015
            Icon nnnnnn ***** 12-08-2015

            1 Reply Last reply
            0
            • N Offline
              N Offline
              neetaj
              wrote on last edited by
              #6

              @neetaj said:

              bbbbbb 11111 22222

              in above example i want to show "bbbbbb 11111 22222" string on single line, but if you observe actual output it is showing on 2 lines. Thats why I to want to set width of column2 to some fixed width or it should increase as per contents maximum size.

              1 Reply Last reply
              0
              • Chris KawaC Online
                Chris KawaC Online
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #7

                // here myListView is object of class in our framework which is similar to QListView

                Well there is no way for me (or anyone here?) to know api of your framework.

                The comment is not really true because, as I said, Qt's QListView only displays a single column. It really more resembles QTreeView with just top level items.

                As I mentioned earlier, Qt's views, including QListView have setWordWrap method that controls how text wraps. Maybe your api has something similar but since it's your framework and your type I can only guess.

                Btw. please surround your code with ``` so it is easier to read (I did that for you for now).

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  neetaj
                  wrote on last edited by
                  #8

                  Thanks Chris. I'll try with similar kind of APIs in our framework.... :)

                  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