Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableWidget setColumnWidth didn't work



  • ui->table_widget->setColumnCount(4);
    ui->table_widget->setColumnWidth(0, 134);
    ui->table_widget->setColumnWidth(1, 134);
    ui->table_widget->setColumnWidth(2, 60);
    //ui->table_widget->setColumnWidth(3, 134);
    ui->table_widget->horizontalHeader()->setStretchLastSection(true);
    ui->table_widget->horizontalHeader()->setVisible(false);
    //ui->table_widget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->table_widget->verticalHeader()->setHidden(true);
    ui->table_widget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->table_widget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->table_widget->setSelectionBehavior(QAbstractItemView::SelectRows);
    

    My Widget display as this
    2452fb99-fdfd-4c1e-a8fa-3a6169387637-image.png



  • What does ui->table_widget->horizontalHeader()->sectionResizeMode(); returns? it should be Fixed for you to be able to set the column width manually



  • @VRonin
    ui->table_widget->setColumnCount(4);
    ui->table_widget->setColumnWidth(0, 134);
    ui->table_widget->setColumnWidth(1, 210);
    ui->table_widget->setColumnWidth(2, 60);
    ui->table_widget->setColumnWidth(3, 134);
    //ui->table_widget->horizontalHeader()->setStretchLastSection(true);
    ui->table_widget->horizontalHeader()->setVisible(false);
    ui->table_widget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->table_widget->verticalHeader()->setHidden(true);
    ui->table_widget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->table_widget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->table_widget->setSelectionBehavior(QAbstractItemView::SelectRows);

    now, I wrote it as this, and display as this.
    294a2ba9-df48-4c26-8be2-0b9fe9815f34-image.png



  • ui->table_widget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); overwrites all the previous calls to ui->table_widget->setColumnWidth().
    You should call ui->table_widget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed); before calling setColumnWidth()



  • @VRonin
    ui->table_widget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->table_widget->setColumnCount(4); //缩写 内容 位置 操作
    ui->table_widget->setColumnWidth(0, 134);
    ui->table_widget->setColumnWidth(1, 210);
    ui->table_widget->setColumnWidth(2, 60);
    ui->table_widget->setColumnWidth(3, 134);
    //ui->table_widget->horizontalHeader()->setStretchLastSection(true);

    3392269a-67a5-479b-9cce-b23a4451db9b-image.png

    my aim is to let the four columns be full of the row and display as the ratio I set。
    How could I achieve ui->table_widget->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->table_widget->setColumnCount(4); //缩写 内容 位置 操作
    ui->table_widget->setColumnWidth(0, 134);
    ui->table_widget->setColumnWidth(1, 210);
    ui->table_widget->setColumnWidth(2, 60);
    ui->table_widget->setColumnWidth(3, 134);
    //ui->table_widget->horizontalHeader()->setStretchLastSection(true);

    3392269a-67a5-479b-9cce-b23a4451db9b-image.png

    my aim is to let the four columns be full of the row and display as the ratio I set。
    How could I achieve it



  • //1
            ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
            ui->tableWidget->setAlternatingRowColors(true);
            ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
            ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);
        //  ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
            ui->tableWidget->setShowGrid(false);
            ui->tableWidget->verticalHeader()->setVisible(false);
            ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
        //2    
            ui->tableWidget->clear();
            ui->tableWidget->setRowCount(0);
            ui->tableWidget->setColumnCount(4);
            ui->tableWidget->setColumnWidth(0, 134);
            ui->tableWidget->setColumnWidth(1, 210);
            ui->tableWidget->setColumnWidth(2, 60);
            ui->tableWidget->setColumnWidth(3, 134);
        //3    
            ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "1" << "2" << "3" << "4");
        //4    
            ui->tableWidget->setRowCount(1);
            ui->tableWidget->setItem(0, 1, new QTableWidgetItem("A"));
            ui->tableWidget->setItem(0, 2, new QTableWidgetItem("B"));
            ui->tableWidget->setItem(0, 3, new QTableWidgetItem("C"));
            ui->tableWidget->setItem(0, 4, new QTableWidgetItem("D"));
        
    


  • @A-A-SEZEN said in QTableWidget setColumnWidth didn't work:

    erticalHeader(

    Hi,
    Thanks for your answer.
    I just do like what you said, but it still doesn't work.

    ui->tableWidget->setAlternatingRowColors(true);
    ui->tableWidget->setShowGrid(false);

    And I think those code didn't effect the result. so I didn't add it.
    Should I wrot them all?



  • @chris_rookie

    Try

    ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeMode::ResizeToContents);
    

    But then you are not able to resize the width manually. It automatically resizes to its content.
    "aaaaaa....." should be "aaaaaaaaaaaaaaaaaaaaaaaaaaa" then.
    (I dont know if can work with this or if you need to resize manually or programmatically)



  • @Pl45m4 said in QTableWidget setColumnWidth didn't work:

    manually

    Thank you.
    I tried
    ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeMode::ResizeToContents);
    It didnt work.
    I'll try (I dont know if can work with this or if you need to resize manually or programmatically)



  • ui->table_widget->resizeColumnToContents(1);

    I found this in my code. I was so stupid.
    After I removed it, It works.
    Thank you all very much And sorry about my mistake.



  • @chris_rookie
    Hi,
    I answered your question before I fully understood it.
    I always add elements to help others, if possible.
    I wanted to have a message with QTableWidget that I shared the data demonstration with side features.


Log in to reply