Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How to let column's width fit to the window's width in QTableView?

    General and Desktop
    2
    5
    43859
    Loading More Posts
    • 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.
    • P
      pico last edited by

      Hi, Sir:
      I apply QTableView and QStandardItemModel to setup a data viewer.
      However, I failed to let the width perfect.
      Could you teach me what I do wrong??

      The codes are below:
      @
      TableViewTest::TableViewTest(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::TableViewTest)
      {
      ui->setupUi(this);
      mQStandardItemModel = new QStandardItemModel(this);
      QString a[10] = { "aaa","bbb", "afa","bfb","ada","bdb","aea","beb","aga","bgb",};
      QStringList * mStrList = new QStringList[100];

      QTime time = QTime::currentTime();
      qsrand((uint)time.msec());
      int ch;
      for(int i = 0; i < 100; i++){
          mStrList[i] << QString("%1").arg((qrand() % 987)%4);
          for(int j = 0; j < 10; j++){
              ch = (qrand() % 10);
              mStrList[i] << QString("%1-%2").arg(a[ch]).arg('a' + j + 1);//a[j];
          }
      }
      
      setParsedData(mStrList,100);
      

      }@

      ========================================================
      @
      int TableViewTest::setParsedData(const QStringList *a, const int b)
      {
      QList <QStandardItem *> col;
      for (int i = 0; i < b; i++){//i+=30){
      QStandardItem * row = new QStandardItem(QIcon(icon), a[i].at(1));
      col.append(new QStandardItem(a[i].at(2)));
      mQStandardItemModel->appendRow(row);
      }
      }
      mQStandardItemModel->appendColumn(col);
      ui->tableView->setModel(mQStandardItemModel);
      return 0;
      }
      @

      In ui file,
      @
      void setupUi(QWidget *TableViewTest)
      {
      TableViewTest->resize(400, 300);
      horizontalLayout = new QHBoxLayout(TableViewTest);
      horizontalLayout->setSpacing(6);
      horizontalLayout->setContentsMargins(11, 11, 11, 11);
      horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
      tableView = new QTableView(TableViewTest);
      tableView->setObjectName(QString::fromUtf8("tableView"));
      tableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
      tableView->setShowGrid(false);
      tableView->setGridStyle(Qt::NoPen);
      tableView->setWordWrap(false);
      tableView->setCornerButtonEnabled(false);
      tableView->horizontalHeader()->setHighlightSections(false);
      tableView->verticalHeader()->setVisible(false);
      tableView->verticalHeader()->setHighlightSections(false);

          horizontalLayout->addWidget(tableView);
      

      @

      1 Reply Last reply Reply Quote 0
      • G
        giesbert last edited by

        did you try to call

        @
        ui->TableViewTest->resizeRowToContents();
        ui->TableViewTest->resizeColumnsToContents();
        @

        after filling in the data?

        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 Reply Quote 0
        • G
          giesbert last edited by

          additionally, you can automate this:

          @
          ui->TableViewTest->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
          @

          see

          • "QHeaderView":http://doc.qt.nokia.com/4.7/qheaderview.html#ResizeMode-enum
          • "QTableView":http://doc.qt.nokia.com/4.7/qtableview.html#resizeColumnsToContents

          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 Reply Quote 0
          • P
            pico last edited by

            Hi, Gerolf:
            Yes, I did try that already. But, it doesn't work as what I want.
            There are two columns in my window.
            And, the contents in each column are too long to apply your way.
            (If I use that, the column one will fill all window)
            So, could you please share me another possibility?

            Thanks a lot.

            BR,
            Pico

            1 Reply Last reply Reply Quote 0
            • P
              pico last edited by

              Hi, Gerolf:
              The problem is solved!!
              I try another way, and I use
              @
              ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);@
              Then, it works...

              So, thanks for your reminding. I check QHeaderView and get the idea.

              Thanks a lot.

              BR,
              Pico

              1 Reply Last reply Reply Quote 0
              • First post
                Last post