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



  • 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);
    

    @



  • did you try to call

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

    after filling in the data?



  • additionally, you can automate this:

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

    see



  • 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



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.