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. QTableView::resizeRowsToContents

QTableView::resizeRowsToContents

Scheduled Pinned Locked Moved General and Desktop
8 Posts 8 Posters 15.8k 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.
  • M Offline
    M Offline
    MartinM
    wrote on last edited by
    #1

    Hi,

    I am new to Qt programming, so this might be a stupid question, but I simply cannot get it to work.

    I have a QTableSubview which contains data from a custom model derived from QAbstractTableModel. The data in the last column might be empty or contain a text with (possibly multiple) line feeds.

    When I try to use resizeRowsToContents it seems only the first column of every row is considered for the new row height, meaning I always get a one-text-line-high row.

    I created a short example (using QtCreator's Gui Application) showing the problem.

    The Model:
    @
    class MyModel : public QAbstractTableModel
    {
    Q_OBJECT
    public:
    int rowCount (const QModelIndex& /* parent / = QModelIndex()) const {return 3;}
    int columnCount(const QModelIndex& /
    parent */ = QModelIndex()) const {return 5;}

    QVariant data (const QModelIndex& index, int role = Qt::DisplayRole) const
    {
        if (!index.isValid() || role != Qt::DisplayRole)
            return QVariant();
    
        if (index.row() > index.column())
            return QVariant("One line");
        else if (index.row() == index.column())
            return QVariant("Two\nlines");
        else
            return QVariant("Three\nlines\nhere");
    };
    

    };
    @

    In the Dialog's constructor:
    @
    MyModel* aModel = new MyModel();

    // Create the table for the contents
    QTableView* aTableView = new QTableView(this);
    aTableView->setModel(aModel);
    
    aTableView->resizeRowsToContents();
    
    QDialogButtonBox* aButtonBox = new QDialogButtonBox(Qt::Vertical);
    aButtonBox->addButton(QDialogButtonBox::Ok);
    connect(aButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
    
    QHBoxLayout* aMainLayout = new QHBoxLayout();
    aMainLayout->addWidget(aTableView);
    aMainLayout->addWidget(aButtonBox);
    
    setLayout(aMainLayout);
    

    @

    What I would expect is, that all rows are high enough to show three lines of text, what I get is a first row, that shows two lines of text and two more lines that show only one line.

    Would you be able to help me out on this?

    Thanks in advance,
    Martin

    1 Reply Last reply
    0
    • D Offline
      D Offline
      doumdi
      wrote on last edited by
      #2

      I have a similar problem with row heights not being adjusted properly. I have set a timer at 1Hz to call resizeRowToContents(index) or resizeRowsToContents() periodically and this works. If you find how to solve your problem, I would be interested to know.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daviddoria
        wrote on last edited by
        #3

        I guess you are just supposed to call resizeRowsToContents() every time you change the item height? Can anyone confirm that this is correct?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          resizeRowsToContents does it one time.
          If you want to do it automatically, you have to do:

          @
          pView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
          @

          written from brain, no check done

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • C Offline
            C Offline
            confused
            wrote on last edited by
            #5

            Gerolf that worked perfectly for me thanks!

            does anyone know if its possible to keep the text in multiple lines when editing it? When I double click a cell to edit it the text all goes into 1 line so some of the text isn't visible or the edit box expands over the cells to the right. Its not a big deal if this isn't do able or if it is really complicated though.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              By default, a QLineEdit is used as the editor for an item view for QStrings. You'll have to convince the item view to use a QTextEdit instead. There are multiple ways to do that, but one is to provide your own [[doc:QItemDelegateFactory]] to the delate that handles your view.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                trebborx
                wrote on last edited by
                #7

                Perfect,
                [quote author="Gerolf" date="1337851944"]resizeRowsToContents does it one time.
                If you want to do it automatically, you have to do:

                @
                pView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
                @

                written from brain, no check done[/quote]

                works for me as well when my QSQlQueryModel updates !
                I use it together with pView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents); to resize my columns automatically as well.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  maranath
                  wrote on last edited by
                  #8

                  QTimer::singleShot(1, ui->tableView, SLOT(resizeRowsToContents()));

                  1 Reply Last reply
                  -1

                  • Login

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