Results 1 to 2 of 2

Thread: HOW CAN i RESIZE THE COLUMN WIDTH OF ONE OF THE COLUMNS IN A QTABLEVIEW

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default HOW CAN i RESIZE THE COLUMN WIDTH OF ONE OF THE COLUMNS IN A QTABLEVIEW

    Hi ladies and gents, I have a QTableView with 4 columns and I would like to permanently resize the second column to 50% the entire width of the QTableView. I thought this would be something easy to figure out but it has proven otherwise. I have attached a screenshot of what I am trying to achieve below. As can be seen in the screenshot below, the second column is 3-times longer than the first and the third column.Selection_001.png
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. ui->tableView->setColumnWidth(1, 900);
    7. model = new QStandardItemModel(74, 4, ui->innertabWidget);
    8. this->setWindowTitle("Tender Bot");
    9. QFile sitesFile("websites.txt");
    10. if(!sitesFile.open(QIODevice::ReadOnly | QIODevice::Text))
    11. {
    12. qDebug() << "Something went wrong, file did not open" <<endl;
    13. }
    14. else
    15. {
    16. QTextStream out(&sitesFile);
    17. int count = 0;
    18. while(!out.atEnd())
    19. {
    20. QModelIndex index = model->index(count, 0, QModelIndex());
    21. model->setData(index, sitesFile.readLine(0).simplified());
    22. count++;
    23. }
    24. }
    25. model->setHeaderData(0, Qt::Horizontal, "Websites");
    26. model->setHeaderData(1, Qt::Horizontal, "Downloaded");
    27. model->setHeaderData(2, Qt::Horizontal, "Date");
    28. model->setHeaderData(3, Qt::Horizontal, "Buttons");
    29. ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    30. ui->tableView->setModel(model);
    31. }
    To copy to clipboard, switch view to plain text mode 
    I thought line 6 in the code snippet above would achieve my desired outcome but it doesn't work.

    Please assist.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: HOW CAN i RESIZE THE COLUMN WIDTH OF ONE OF THE COLUMNS IN A QTABLEVIEW

    Methods to set the sizes of table elements generally don't work unless the table is visible (and maybe has content). So calling these methods in the constructor have no effect since the table will not become visible until the showEvent() of the MainWindow has occurred. Try implementing MainWindow:: showEvent() and insert that sizing code there. You'll also be able to retrieve the actual size of the table at that point and set the column width to the true 50% you are aiming for.

    AND PLEASE DON'T USE ALL CAPS WHEN POSTING TITLES. IT IS ANNOYING AS WELL AS BEING HARD TO READ. It doesn't get your question answered any faster either. Thanks.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QTableview set columns maximun width
    By danalex07 in forum Newbie
    Replies: 2
    Last Post: 6th January 2016, 20:04
  2. How to resize column width
    By anbu01 in forum Newbie
    Replies: 0
    Last Post: 11th August 2014, 14:05
  3. Width QTableView for QCombobox with multiple columns
    By visor_ua in forum Qt Programming
    Replies: 7
    Last Post: 21st June 2011, 10:54
  4. Don't resize columns - QTableView
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2009, 16:59
  5. set the width of the columns of QTableView
    By zhanglr in forum Qt Programming
    Replies: 6
    Last Post: 31st July 2008, 15:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.